2015-10-22 25 views
1

在PowerShell中,遠程運行的進程(軟件安裝):WMICLASS.Create(),返回變量?

$computers = Get-Content "C:\computer.txt" 

foreach ($computer in $computers) { 


#The location of the file 
    $Install = "\\$computer\C$\Software" 

#The Install string can have commands aswell 
    $InstallString = "$Install\IE11-Windows6.1-x64-en-us.exe $arguments" 

    ([WMICLASS]"\\$computer\ROOT\CIMV2:Win32_Process").Create($InstallString) 
#Output the install result to your Local C Drive 
    Out-File -FilePath c:\installed.txt -Append -InputObject "$computer"} 

有沒有辦法返回變量或本安裝狀態?或者等到這個過程完成了?

回答

0

本地,您可以使用Wait-Process

$InstallProcess = ([WMICLASS]"Win32_Process").Create($InstallString) 
Wait-Process -Id $InstallProcess.ProcessId 

不幸的是,Wait-Process不支持遠程處理。


這裏是一個窮人的remote equivalent using Get-Process

$InstallProcess = ([WMICLASS]"\\$computer\ROOT\CIMV2:Win32_Process").Create($InstallString) 
do { Get-Process -Id $InstallProcess.ProcessId -ComputerName $computer } while ($?) 
+0

我嘗試這一點,但有一個不過程中發現的。你確定這可以在遠程PC上工作,或者是'Wait-Process'搜索本地PC嗎? – Zeno

+0

@Zeno對不起,沒有注意到,更新回答 –

+0

也沒有運氣,這是返回'Get-Process:無法連接到遠程機器.'即使該過程確實開始 – Zeno