[英]Executing powershell.exe from powershell script (run in ISE but not in script)
I'm new to these awesome Power shell world. 我是这些功能强大的Power Shell世界的新手。 I have a problem with script and really apreciate your help.
我的脚本有问题,非常感谢您的帮助。
I have a script "cmd4.ps1" that needs to run another script "Transfer.ps1" that needs to receive 3 strings params and it needs to be run as other process thead different to "cmd4.ps1". 我有一个脚本“ cmd4.ps1”,该脚本需要运行另一个脚本“ Transfer.ps1”,该脚本需要接收3个字符串参数,并且需要作为与“ cmd4.ps1”不同的其他进程来运行。
cmd4.ps1: cmd4.ps1:
$Script="-File """+$LocalDir+"\Remote\Transfer.ps1"" http://"+$ServerIP+"/Upload/"+$FileName+" "+$ServerIP+" "+$LocalIP Start-Process powershell.exe -ArgumentList $Script
After ejecution, the $Script
cointain a value similar to 射精后,
$Script
的值类似于
-File "c:\temp re\Remote\Transfer.ps1" http://10.1.1.1/Upload/file.txt 10.1.1.1 10.1.1.10
containing the syntax to use -File
parameter to run a script of Powershell.exe, and three parameters that Transfer.ps1 needs ( "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
) 。
When I write these instructions in PowerShell ISE I can see every values are right and PowerShell.exe is executed with right values, everything work fine!, but if I put these instructions inside "cmd4.ps1" script it doesn't work, I mean something is not right with parameters because I can see it start powershell but it never ends. 当我在PowerShell ISE中编写这些指令时,我可以看到每个值都是正确的,并且以正确的值执行了PowerShell.exe,一切正常!但是,如果我将这些指令放在“ cmd4.ps1”脚本中,则无法正常工作,我表示参数不正确,因为我可以看到它以powershell启动,但从未结束。
-ArgumentList
is expecting an array of string arguments instead of a single string. -ArgumentList
需要一个字符串参数数组,而不是单个字符串。 Try this instead: 尝试以下方法:
$ScriptArgs = @(
'-File'
"$LocalDir\Remote\Transfer.ps1"
"http://$ServerIP/Upload/$FileName $ServerIP $LocalIP"
)
Start-Process powershell.exe -ArgumentList $ScriptArgs
Note that you can simplify the string construction of the args as shown above. 请注意,您可以简化args的字符串构造,如上所示。
为什么不把它放在cmd4.ps1中呢?
& "c:\temp re\Remote\Transfer.ps1" "http://10.1.1.1/Upload/file.txt" "10.1.1.1" "10.1.1.10"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.