开发者

Executing powershell.exe from powershell script (run in ISE but not in script)

I'm new to these awesome Power shell world. 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:

$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

-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).

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 instruct开发者_如何转开发ions 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.


-ArgumentList is expecting an array of string arguments instead of a single string. 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.


Why don't you put this in cmd4.ps1?

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜