Powershell错误 - 空/空参数

时间:2015-06-19 13:43:12

标签: sql sql-server powershell database-connection

我有一个小的PowerShell脚本,用于从名为Hal0Test>的远程SQL数据库中获取列ServerName。从表,ServerList。但是,我无法弄清楚这个Powershell错误。

最新代码:

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 FROM 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' WHERE ServerName = '$os.PSComputerName';"
    $result = $command.ExecuteNonQuery()  
} | Export-Csv 'C:\Users\king\Desktop\HalO\output.csv' -Delimiter '|' -NoType
Write-Output "`n End of Hal0";

SQL表:

enter image description here

2 个答案:

答案 0 :(得分:3)

您将ForEach-Object循环更改为foreach循环。如果要使用后者,则需要将当前对象变量$_更改为循环变量$Server

foreach ($Server in $ServerArray) {
  $os    = Get-WmiObject -Class Win32_OperatingSystem -Computer $Server
  $disks = Get-WmiObject -Class Win32_LogicalDisk -Computer $Server | ...

  ...
}

否则您需要将循环更改回ForEach-Object循环:

$ServerArray | ForEach-Object {
  $os    = Get-WmiObject -Class Win32_OperatingSystem -Computer $_
  $disks = Get-WmiObject -Class Win32_LogicalDisk -Computer $_ | ...

  ...
}

此外,}Export-Csv之间没有管道:

    $result = $command.ExecuteNonQuery()  
} Export-Csv 'C:\Users\mdaraghmeh\Desktop\HalO\output.csv' -Delimiter '|' -NoType
 ^
here

即使有foreach循环仍然无法将其输出提供给管道。如果要将foreach与管道一起使用,则必须将输出分配给变量:

$output = foreach ($Server in $ServerArray) { ... }
$output | Export-Csv ...

或在表达式中运行:

(foreach ($Server in $ServerArray) { ... }) | Export-Csv ...

对于直接管道处理,您需要ForEach-Object循环:

$ServerArray | ForEach-Object { ... } | Export-Csv ...

答案 1 :(得分:2)

您需要使用[datatable]来存储您选择的结果:

$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 FROM ServerList"
$command.CommandText = $query
$ServerNames = $command.ExecuteReader()

$table = new-object "System.Data.DataTable"
$table.Load($ServerNames)

现在$table有您的服务器名称列表。