リモートサーバー上で既存のスクリプトを実行してローカルファイル内の出力をキャプチャしようとしています。
-
22-12-2019 - |
質問
複数のサーバー(ほぼ40-50サーバー)を
にしたいです。$ username="user"
$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
}
.
スタートジョブを削除した場合は、このコードはうまく機能しますが、もう一方の後に実行されます。
環境で制限されているため、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
. 解決
あなたから始めるためにあなたの変数を仕事に渡していません。必要なのは、スクリプトブロック内で$ 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
}
.
おそらく環境変数を渡す必要がありませんでしたが、変数にスコープ問題があるように見えました。
あるいは、スクリプトブロック内のPARAMブロックを使用して、名前付き変数に渡された引数を基本的にマッピングします。
$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
}
.
これが助けを願っています。 Cheers、Chris。
所属していません StackOverflow