使用scriptblock和argumentlist

时间:2016-10-27 00:18:11

标签: powershell powershell-v3.0 powershell-remoting

我需要从test1.ps1执行powershell文件test.ps1。我正在使用powershell的启动工作功能。在我的test1.ps1中,我有

$pathToFile = (Join-Path -Path $PSSCriptRoot -ChildPath "test.ps1")
$ar = $a, $b # all these variables have values assigned 
Start-Job -Name job2 -ScriptBlock{& $pathToFile -fName $args[0] -lName $args[1]} -ArgumentList $ar

在我的test.ps1中,我将fName和lName作为参数,如:

param([string]fName, [string]lName]

我一直收到以下错误:

  

'&'之后的表达在一个管道元素中产生了一个对象   无效。它必须导致命令名称,脚本块或a   CommandInfo对象。

问题在于Start-Job -Name job2 -ScriptBlock{& $pathToFile -fName $args[0] -lName $args[1]} -ArgumentList $ar

但我似乎找不到解决此问题的解决方案。是的,我确认$ pathToFile包含test.ps1 =>的确切路径。 C:\ FOO \ test.ps1 感谢您提供任何帮助或建议。

2 个答案:

答案 0 :(得分:2)

经过几个小时的研究,我终于成功了。

解决方案是使用GraphicsView

所以,而不是使用

$using:pathToFile

解决方案是使用它:

Start-Job -Name job2 -ScriptBlock{& $pathToFile -fName $args[0] -lName $args[1]} -ArgumentList $ar

我希望这会有助于其他人。

答案 1 :(得分:-1)

您是否尝试过invoke-expression cmdlet? 这就是我经常使用的。

Invoke-Expression [-Command] []