2017-08-30 22 views
0

我在幾個OU中列出廣告計算機,然後在每個計算機上運行Invoke-Command,但不知何故,它不會將Get-ADComputer的結果傳遞到Invoke-Command。我究竟做錯了什麼?它只會導致在第一個OU中找到的第一臺PC。如何將Get-ADComputers傳遞到PowerShell中的Invoke-Command中

$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

你使用'$ PCName'來存儲'Get-ADComputer'的輸出,但在使用'Invoke-Command'時引用'$ name'。此外,'-Properties *'參數參數是不必要的,並且會招致一些開銷 –

+0

'$ PCName | %{Invoke-Command -computername $ _ -scriptblock {...}}'etc ... - 另外'Out-GridView'將不起作用,您應該保存'Invoke-Command'的輸出,然後將其輸入本地'Out-GridView' – Avshalom

+0

@Avshalom這是我第一次嘗試'$ PCName | %{Invoke-Command -computername $ _ -scriptblock {...}}'並得到相同的結果。只列出在第一個OU中找到的第一臺PC –

回答

1

了一些小改動更容易(不包括歐過濾器,並運行只需3臺電腦) 我跑,我的機器,對工作沒有任何問題......

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

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