Powershell Invoke-Command with Script for Script Name

时间:2012-07-31 10:37:40

标签: powershell

我需要从本地脚本在远程计算机上执行Powershell脚本。问题是,在runitime之前我不知道远程脚本的路径或文件名。

我在本地脚本中尝试了以下行:

Invoke-Command -ComputerName $TargetServer -ScriptBlock { & ($TargetMSI) '$MSI' 'C:\Program Files (x86)\Vasanta.Int.MIS' 'Dev' }

问题是这会返回错误:'&'之后的表达式在管道元素中产生了无效的对象。

如果用硬编码的字符串文字替换$ TargetMSI,那么它可以正常工作。

有谁能告诉我需要改变什么?

1 个答案:

答案 0 :(得分:2)

在v2中调用Invoke-Command时,没有直接的方法将变量传递给scriptblock。您需要在scriptblock组合中使用-ArgumentList + param():

Invoke-Command -ScriptBlock { param ($TargetMSI, $MSI) & $TargetMSI '$MSI' } -ArgumentList $TargetMSI, $MSI

使用$ using:localvariable语法在v3中修复/改进。