Powershell获取powershell远程作业的输出 [英] Powershell getting the output of powershell remote job
问题描述
您好,
我正在运行以下powershell脚本以在多台服务器计算机上运行可执行文件。此可执行文件大约需要1分钟才能完成,并将结果输出到控制台窗口:
$ servers ='Srv001','Srv002';
$ ErrorActionPreference =STOP;
$ credentials = Get-Credential'adm\runner';
foreach($ srv in $ servers){
$ session = New-PSSession -ComputerName $ srv -Credential $ credentials;
Invoke-Command -Session $ session -ScriptBlock {
Write-Host('执行任务'+ $ args)-ForegroundColor Green;
PowerShell.exe -Command&cmd.exe / C C:\ Meriner.exe;
写主机'---测试完成---'-ForegroundColor Green;
} -ArgumentList $ srv; #-AsJob
}
删除-PSSession *;
现在 - 一切正常,但在当前的例子中,我需要等待2分钟,因为我在2台服务器上运行它。为了解决这个问题,我添加了-AsJob切换到Invoke-Command cmdlet(在代码中注释掉了部分),但现在我丢失了结果视图,因为现在它们作为作业在后台运行而不会将结果输出到控制台。
有什么想法我怎么能得到这个?我有2个新对象 - 2个工作,但这些似乎没有输出或其他东西....
谢谢!
我尝试过:
请参阅问题描述中的代码。
服务器= 'Srv001', 'Srv002';
ErrorActionPreference = STOP;
< blockquote> credentials = Get-Credential'adm\runner';
foreach(
Hello,
I am running the following powershell script to run executable on multiple server machines. This executable takes about 1 minute to complete and output results to console window:
$servers = 'Srv001', 'Srv002'; $ErrorActionPreference = "STOP"; $credentials = Get-Credential 'adm\runner'; foreach($srv in $servers){ $session = New-PSSession -ComputerName $srv -Credential $credentials; Invoke-Command -Session $session -ScriptBlock { Write-Host ('Executing task on ' + $args) -ForegroundColor Green; PowerShell.exe -Command "& ""cmd.exe /C C:\Miner.exe"""; Write-Host '---TEST COMPLETE---' -ForegroundColor Green; } -ArgumentList $srv; #-AsJob } Remove-PSSession *;
now - this all works fine, but in current example I need to wait 2 minutes as I am running this on 2 servers. To solve this I've added "-AsJob" switch to Invoke-Command cmdlet (commented out part in code), but now I lost the view of results, because now they run in the background as jobs and dont output the results into console.
Any thoughts how I could get this back? I have 2 new objects though - 2 jobs, but these dont seem to have a property of "output" or something....
Thanks!
What I have tried:
See the code in problem description.
servers = 'Srv001', 'Srv002';
ErrorActionPreference = "STOP";
credentials = Get-Credential 'adm\runner'; foreach(
这篇关于Powershell获取powershell远程作业的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!