sql - Powershell Error - Null/Empty Argument -
i have small powershell script meant column servername remote sql database called, hal0test > table, serverlist. however, can not figure out powershell error.
newest code:
write-output " `n start of hal0 `n"; $connectionstring = "server=qautilitydb01;database=hal0test;integrated security=true;" $connection = new-object system.data.sqlclient.sqlconnection $connection.connectionstring = $connectionstring $connection.open() $command = $connection.createcommand() $serverarray = [system.collections.arraylist]@() $query = "select servername serverlist" $command.commandtext = $query $servernames = $command.executereader() $table = new-object “system.data.datatable” $table.load($servernames) $serverarray = $table | select -expand servername $serverarray | foreach-object { # $server returns each server name $os = get-wmiobject -class win32_operatingsystem -computer $_ $disks = get-wmiobject -class win32_logicaldisk -computer $_ | where-object {$_.drivetype -eq 3} | foreach-object { '{0} {1:d} mb free/{2:d} mb used' -f $_.deviceid, [int]($_.freespace/1mb), [int]($_.size/1mb) } new-object -type pscustomobject -property @{ 'fqdn' = $_ 'servername' = $os.pscomputername 'operatingsystem' = $os.caption 'disks' = $disks -join ' | ' } $command.commandtext = "update serverlist set fqdn = '$_', os = '$os.caption' servername = '$os.pscomputername';" $result = $command.executenonquery() } | export-csv 'c:\users\king\desktop\halo\output.csv' -delimiter '|' -notype write-output "`n end of hal0";
sql table:
you changed foreach-object
loop foreach
loop. if want use latter need change current object variable $_
loop variable $server
:
foreach ($server in $serverarray) { $os = get-wmiobject -class win32_operatingsystem -computer $server $disks = get-wmiobject -class win32_logicaldisk -computer $server | ... ... }
otherwise need change loop foreach-object
loop:
$serverarray | foreach-object { $os = get-wmiobject -class win32_operatingsystem -computer $_ $disks = get-wmiobject -class win32_logicaldisk -computer $_ | ... ... }
also, there's no pipe between }
, export-csv
:
$result = $command.executenonquery() } export-csv 'c:\users\mdaraghmeh\desktop\halo\output.csv' -delimiter '|' -notype ^ here
and if there foreach
loop still unable feed output pipeline. if want use foreach
pipeline must assign output variable:
$output = foreach ($server in $serverarray) { ... } $output | export-csv ...
or run in expression:
(foreach ($server in $serverarray) { ... }) | export-csv ...
for direct pipeline processing need foreach-object
loop:
$serverarray | foreach-object { ... } | export-csv ...
Comments
Post a Comment