我正在尝试编写一个PowerShell脚本,该脚本将列出命令列表,然后在远程计算机上运行它们,我有以下代码:

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

它不会丢任何错误,但实际上也没有运行命令(例如停止服务ServiceName)。 $命令是从称为脚本的文本文件作为参数中读取的,我知道该脚本的其余部分有效,因为我一直在使用它来执行使用Invoke-expression的本地命令。

任何帮助都是很棒的。

有帮助吗?

解决方案

正确的代码将是

$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