none
Powershell array import to sql table RRS feed

  • Question

  • Hello,

    I have objects array of powershell.

    $ServersObjects.DistributionPointsAll
    Output:

    SERV.bybinet.net
    SERV2.xujanet.net
    SERV3.pizdonet.net

    Also Im making query to import all array values from powershell to sql table.
    $ServersObjects = New-Object -TypeName PSObject
    Add-Member -InputObject $ServersObjects -MemberType NoteProperty -Name ON -Value $OnlineServers
    Add-Member -InputObject $ServersObjects -MemberType NoteProperty -Name OFF -Value $OfflineServers
    Add-Member -InputObject $ServersObjects -MemberType NoteProperty -Name SPARE -Value $SpareServers
    Add-Member -InputObject $ServersObjects -MemberType NoteProperty -Name DistributionPointsAll -Value $DistributionPoints
    
    $ServersObjects | ft
    $Query = @()
    foreach ($DP in $ServersObjects.DistributionPointsAll)  #$($ServersObjects.off))
        
        {
        
        
        $Query += " INSERT INTO dbo.SCCMServers (ServerName) VALUES(1,'$($DP)')"
    
        
        
        }

    Getting results on SQL table:

    values added started from 977 raw.

    How can i add these array from sql table  ServerName value 1 ?

    • Moved by Bill_Stewart Wednesday, February 14, 2018 7:24 PM This is not SQL support forum
    Thursday, January 4, 2018 11:23 AM

All replies

  • Hello,

    I have objects array of powershell.

    $ServersObjects.DistributionPointsAll
    Output:

    SERV.bybinet.net
    SERV2.xujanet.net
    SERV3.pizdonet.net

    Also Im making query to import all array values from powershell to sql table.
    $ServersObjects = New-Object -TypeName PSObject
    Add-Member -InputObject $ServersObjects -MemberType NoteProperty -Name ON -Value $OnlineServers
    Add-Member -InputObject $ServersObjects -MemberType NoteProperty -Name OFF -Value $OfflineServers
    Add-Member -InputObject $ServersObjects -MemberType NoteProperty -Name SPARE -Value $SpareServers
    Add-Member -InputObject $ServersObjects -MemberType NoteProperty -Name DistributionPointsAll -Value $DistributionPoints
    
    $ServersObjects | ft
    $Query = @()
    foreach ($DP in $ServersObjects.DistributionPointsAll)  #$($ServersObjects.off))
        
        {
        
        
        $Query += " INSERT INTO dbo.SCCMServers (ServerName) VALUES(1,'$($DP)')"
    
        
        
        }

    Getting results on SQL table:

    values added started from 977 raw.

    How can i add these array from sql table  ServerName value 1 ?

    • Merged by jrv Thursday, January 4, 2018 11:44 AM DUPLICATE
    Thursday, January 4, 2018 11:06 AM
  • foreach ($DP in $ServersObjects.DistributionPointsAll){
        $query += "INSERT INTO dbo.SCCMServers(ServerName) VALUES('$DP')"
        Write-Host $query
    }

    Start with this and be sure your objects are correct.

    Try to learn a bit of SQL.  This is not a SQL forum.  Post in T-SQL forum for help.

     


    \_(ツ)_/



    • Edited by jrv Thursday, January 4, 2018 11:17 AM
    • Marked as answer by TechSpark0 Thursday, January 4, 2018 11:22 AM
    • Marked as answer by TechSpark0 Thursday, January 4, 2018 1:00 PM
    • Unmarked as answer by TechSpark0 Thursday, January 4, 2018 1:00 PM
    Thursday, January 4, 2018 11:15 AM