2014-04-04 48 views
0

我只是希望能够从一个PS脚本传递一个参数到另一个,目前我的脚本(SCRIPT1)如下(一切都归功于用户CB):传递参数

$filepath = Resolve-Path "script2.ps1" 
start-process -FilePath powershell.exe -ArgumentList "-file `"$($filepath.path)`"" 

这通过另一个PowerShell实例成功打开另一个脚本。现在我希望能够将参数传递给'script2.ps1'脚本。我曾尝试以下但这并不不行:

script1.ps1

$name = read-host "The name" 

$filepath = Resolve-Path "script2.ps1" 
start-process -FilePath powershell.exe -ArgumentList "-file `"$($filepath.path)`"-name $name" 

script2.ps1

Param(

    [string]$name 

) 

write-host $name 

这应该简单地传过来$名字从SCRIPT1为$在script2中的名称。我认为我很接近但不够接近!

感谢您的帮助!

回答

0

我看到的唯一的问题是,你缺少在最后逃脱"空间,试试这个:

start-process -FilePath powershell.exe -ArgumentList "-file `"$($filepath.path)`" -name $name" 
+0

再次袭击!非常感谢您的帮助,非常感谢 – user3402227

+0

@ user3402227很高兴提供帮助!根据Duncan的评论......我也很好奇,知道为什么你在这种情况下使用启动过程;) –

0

有你为什么要在一个单独的实例运行第二个脚本中的具体原因电源外壳?如果没有,你会做更好的只是直接运行该脚本:

$name = read-host "The name" 
.\script2.ps1 -name $name 

这样你就不必担心逃避任何参数。

你这样做的方式强制所有的参数转换为字符串并通过Windows命令行处理进行处理。这可能是一个噩梦,以确保价值观通过一个可用的形式。如果您只是直接调用脚本,则可以传递对象作为参数,而Powershell则是关于使用对象而不是字符串。