简体   繁体   English

Powershell psexec作业

[英]Powershell psexec JOB

I tried this from another post at Stackoverflow: 我在Stackoverflow的另一篇文章中尝试了此方法:

$serverlist=$listbox1.SelectedItems

$jobs = foreach ($serverName in $serverlist) {
  Start-Job -Name "PingJob $serverName" -ArgumentList $serverName -ScriptBlock {
      param($serverName)
      if (Test-Connection -ComputerName $serverName -Count 1 -Quiet)
      {
        C:\Localbin\PsExec.exe -s \\$serverName ping cadc02 -n 30 > \\$serverName\pingresult\$serverName.txt
      }
   }
}

$jobs | Wait-Job 
$jobs | Receive-Job

It works and the output files are created, but the full data is missing. 它可以正常工作,并且创建了输出文件,但是缺少了完整的数据。 It just says 只是说

Pinging cadc02.xyz.net [192.168.100.2]

but the rest of the lines like 但是其余的几行

Reply from 192.168.200.3: bytes=32 time<1ms TTL=128
Ping statistics for 192.168.200.3:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 0ms, Maximum = 0ms, Average = 0ms

is missing. 不见了。

How about this code: 这段代码怎么样:

$serverlist=$listbox1.SelectedItems

$jobs = foreach ($serverName in $serverlist) {
  Start-Job -Name "PingJob $serverName" -ArgumentList $serverName -ScriptBlock {
      param($serverName)
      if (Test-Connection -ComputerName $serverName -Count 1 -Quiet)
      {
        Invoke-Command -ComputerName $serverName -ScriptBlock { & ping cadc02 -n 30 } > "C:\temp\$serverName.txt"
      }
   }
}

$jobs | Wait-Job 
$jobs | Receive-Job

There is one big "if" with this code, and that is it uses Powershell remoting via the Invoke-Command instead of System Internals psexec. 此代码有一个很大的“ if”,即它通过Invoke-Command而不是System Internals psexec使用Powershell远程处理。

If psexec is a requirement (you can't powershell-remote the computers for some reason) then this won't work for you. 如果需要psexec(由于某种原因,您无法对计算机进行Powershell远程安装),那么这将对您不起作用。

Also: Change the "C:\\temp\\" in the code for a local folder that works for you. 另外:在适用于您的本地文件夹的代码中更改“ C:\\ temp \\”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM