-1
##this code starts job but never exits or get any output###
clear

$test = 1

$Username = Read-host "Username (Example:Hosting.com\Your Username)"

""

$Pass = Read-Host -assecurestring "Password"


$Password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pass))


$Servers = Get-Content "c:\temp\new\Servers.txt" 

foreach ($Server in $Servers)

{

  $files= "c:\temp\new\new.ps1"

  foreach ($file in $files)

  {

   xcopy $file \\$Server\C$ /Y

  }

}

$CSVContent=@()

$sb = {c:\temp\new\PsExec.exe -h "\\$Server" /accepteula -u $Username -p $password -d cmd.exe /c "echo . | Powershell.exe -executionpolicy bypass -file c:\new.ps1 " > "$Test.txt"}


foreach ($Server in $Servers) 

{

start-job -scriptblock $sb -ArgumentList $server

$test++

}

get-job | wait-job | receive-job
4

1 回答 1

0

这是我刚刚写的东西,它将执行您希望它执行的操作并将所有输出打印到本地 PC 上的文件(您从中运行脚本的位置)

$execDir = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
$Servers = Get-Content "c:\temp\servers.txt"

foreach ($Server in $Servers)

{
    #echo $Server
copy-item "C:\temp\new\new.ps1" \\$server\c$\temp\
& "$execdir\psexec.exe" \\$server powershell.exe -noprofile -executionpolicy Bypass "C:\data\new.ps1" >> "C:\temp\logs.txt"

}

如果脚本说它无法运行 powershell.exe,只需将完整路径添加到 exe 所在的位置,我不确定您是否能够使用作业,除非您使用的是 powershell v4 并使用foreach -Parallelcmdlett

http://technet.microsoft.com/en-us/library/jj713711.aspx

于 2014-06-04T03:56:03.710 回答