2010-11-03 108 views
2

我是新来的这些超棒的Power壳世界。我遇到了一个脚本问题,真的很赞赏你的帮助。从powershell脚本执行powershell.exe(在ISE中运行,但不是在脚本中)

我有一个脚本“cmd4.ps1”,需要运行另一个需要接收3个字符串参数的脚本“Transfer.ps1”,它需要作为与“cmd4.ps1”不同的其他进程运行。

cmd4.ps1:

 
$Script="-File """+$LocalDir+"\Remote\Transfer.ps1"" http://"+$ServerIP+"/Upload/"+$FileName+" "+$ServerIP+" "+$LocalIP 

Start-Process powershell.exe -ArgumentList $Script 

ejecution后,$Script cointain值类似于

 
-File "c:\temp re\Remote\Transfer.ps1" http://10.1.1.1/Upload/file.txt 10.1.1.1 10.1.1.10 

包含语法使用-File参数运行Powershell.exe的一个脚本,以及三个Transfer.ps1需要的参数("http://10.1.1.1/Upload/file.txt", 10.1.1.1, 10.1.1.10)。

当我在PowerShell ISE中编写这些指令时,我可以看到每个值都是正确的,并且PowerShell.exe使用正确的值执行,但一切正常!但如果我将这些指令放在“cmd4.ps1”脚本中, t工作,我的意思是不正确的参数,因为我可以看到它启动PowerShell,但它永远不会结束。

回答

1

-ArgumentList期望一个字符串参数数组而不是单个字符串。试试这个:

$ScriptArgs = @(
    '-File' 
    "$LocalDir\Remote\Transfer.ps1" 
    "http://$ServerIP/Upload/$FileName $ServerIP $LocalIP" 
) 

Start-Process powershell.exe -ArgumentList $ScriptArgs 

请注意,您可以简化args的字符串构造,如上所示。

0

为什么不把它放在cmd4.ps1中?

& "c:\temp re\Remote\Transfer.ps1" "http://10.1.1.1/Upload/file.txt" "10.1.1.1" "10.1.1.10" 
相关问题