0

これを複数のサーバー(ほぼ40〜50台のサーバー)で並行して実行したい

$Username = "ユーザー"

$Password = "Password"

$servers = get-content "c:\temp\servers.txt"

$sb = {c:\temp\PsExec.exe -h \\$server -u $Username -p $password cmd.exe /c "echo . | Powershell.exe -executionpolicy bypass -file c:\script.ps1" > "$env:userprofile\Desktop\output.txt"} 

foreach($server in $servers)
{
    start-job -ScriptBlock $sb
}

このコードは、start-job を削除すると正常に動作しますが、次々と実行されるため、多くの時間がかかります。

PSsessionやinvoke-commandは当方の環境で制限されているため使えません。

このコードは決して終了しません。次の位置で停止します。

 + CategoryInfo          : NotSpecified: (:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError
 
PsExec v1.98 - Execute processes remotely
Copyright (C) 2001-2010 Mark Russinovich
Sysinternals - www.sysinternals.com
4

1 に答える 1

0

まず、変数をジョブに渡しません。必要なのは、ScriptBlock 内で $args 変数を使用し、必要な変数を -ArgumentList で渡すことです。

$Password = "Password"

$servers = get-content "c:\temp\servers.txt"

$sb = {
  c:\temp\PsExec.exe -h \\$args[0] -u $args[1] -p $args[2] cmd.exe /c "echo . | Powershell.exe -executionpolicy bypass -file c:\script.ps1" > "$args[3]\Desktop\output.txt"
} 

foreach($server in $servers)
{
    start-job -ScriptBlock $sb -ArgumentList $server,$Username,$password,$env:userprofile
}

おそらく環境変数を渡す必要はありませんでしたが、変数のスコープの問題があったようです。

または、ScriptBlock で Param Block を使用して変数に名前を付けることができます。これは、基本的に、名前付き変数に渡された Arguments を位置的にマップします。

$Password = "Password"

$servers = get-content "c:\temp\servers.txt"

$sb = {
  Param ($Server,$UserName,$Password,$UserProfile)

  c:\temp\PsExec.exe -h \\$Server -u $UserName -p $Password cmd.exe /c "echo . | Powershell.exe -executionpolicy bypass -file c:\script.ps1" > "$UserProfile\Desktop\output.txt"
} 

foreach($server in $servers)
{
    start-job -ScriptBlock $sb -ArgumentList $server,$Username,$password,$env:userprofile
}

これが役立つことを願っています。乾杯、クリス。

于 2014-07-14T11:14:06.003 に答える