Home Forums Programming Powershell Write PowerShell Output to SQL Server Table RE: Write PowerShell Output to SQL Server Table

  • As for the number of executions you have two choices:

    1) Loop through adding multiple inserts to a single command.

    $isFirst = $true

    $commandText = "INSERT DiskSpace"

    $wmiObject = Get-WmiObject Win32_LogicalDisk -computer 'SomeComputerName'

    Foreach ($logicalDisk in $wmiObject)

    {

    if($isFirst) { $isFirst = $false } else { $commandText += "," }

    $commandText += " VALUES ('" + $logicalDisk["DeviceID"] + "')"

    }

    $command = $conn.CreateCommand()

    $command.CommandText = $commandText

    $command.ExecuteNonQuery()

    2) Loop through executing multiple commands.

    $wmiObject = Get-WmiObject Win32_LogicalDisk -computer 'SomeComputerName'

    Foreach ($logicalDisk in $wmiObject)

    {

    $commandText = "INSERT DiskSpace VALUES ('" + $logicalDisk["DeviceID"] + "')"

    $command = $conn.CreateCommand()

    $command.CommandText = $commandText

    $command.ExecuteNonQuery()

    }

    Gaz

    -- Stop your grinnin' and drop your linen...they're everywhere!!!