如何在PowerShell中将Get-ADComputers传递给Invoke-Command

时间:2017-08-30 16:33:10

标签: powershell

我列出了几个OU中的广告计算机,然后在每个OU上运行Invoke-Command,但不知何故,它不会将结果从Get-ADComputer传递到Invoke-Command。我究竟做错了什么?它只会产生第一个OU中找到的第一台PC。

$DesktopOUs = 'OU=aaa,DC=aaa,DC=com',
              'OU=bbb,DC=aaa,DC=com'
$PCName =  $DesktopOUs | foreach {
    Get-ADComputer -Filter * -Properties * -SearchBase $_ |
        Select-Object -ExpandProperty Name
}

Invoke-Command -ComputerName $pcname -ScriptBlock {
    $win7kb = "*KB4025341*"

    $Session = New-Object -ComObject "Microsoft.Update.Session"
    $Searcher = $Session.CreateUpdateSearcher()
    $historyCount = $Searcher.GetTotalHistoryCount()
    $Searcher.QueryHistory(0, $historyCount) |
        Where-Object {$_.title -like $win7kb} | 
        Select-Object Date,
            @{name="Operation"; expression={switch($_.operation){1 {"Installation"}; 2 {"Uninstallation"}; 3 {"Other"}}}},
            @{name="Status"; expression={switch($_.resultcode){1 {"In Progress"}; 2 {"Succeeded"}; 3 {"Succeeded With Errors"}; 4 {"Failed"}; 5 {"Aborted"}}}},
            Title
} | Out-GridView -Title "Win7"

1 个答案:

答案 0 :(得分:1)

稍微更改一下(排除过滤器,只运行3台计算机) 我在我的机器上运行它,没有任何问题......

$PCName = Get-ADComputer -Filter * -Properties * |
        Select-Object -ExpandProperty Name

$PCName[4..6] | % { Invoke-Command -ComputerName $_ -ScriptBlock {1} }