質問

コマンドのリストを取得し、リモートマシンで実行するPowerShellスクリプトを作成しようとしています。次のコードがあります。

foreach($command in $commands)
{
  Invoke-Command -computer "BNEBAK" -scriptblock{"$command"}
}

エラーはスローしませんが、実際にはコマンドを実行しません(例:Stop-Service ServiceName)。 $コマンドは、スクリプトが呼び出されたときに引数として渡されたテキストファイルから読み込まれます。このスクリプトの残りの部分が機能することはわかっています。

どんな助けも素晴らしいでしょう。

役に立ちましたか?

解決

正しいコードはそうでしょう

$commands = @(get-content com.txt)
for($command in $commands) { 
  $scriptblock = $ExecutionContext.InvokeCommand.NewScriptBlock($command) 
  Invoke-Command -computer $computer -scriptblock $scriptblock 
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top