简体   繁体   English

从powershell脚本执行powershell.exe(在ISE中运行,但不在脚本中运行)

[英]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.

相关问题 脚本与 ISE 一起运行,但不是 powershell.exe - Script runs with ISE but not powershell.exe 从powershell.exe向脚本传递参数 - passing parameters to script from powershell.exe 调度 powershell 脚本在服务器上运行 - 查找 powershell.exe - Scheduling powershell script to run on server - finding powershell.exe 使用脚本体作为param和其他参数运行powershell.exe - Run powershell.exe with script body as param and other params 将整个脚本作为参数传递给powershell.exe - Pass whole script as argument to powershell.exe PowerShell.exe -ExecutionPolicy 绕过 - 脚本中的 Header - PowerShell.exe -ExecutionPolicy Bypass - Header in Script 在CLI中运行脚本的Powershell.exe还是包装程序? - Powershell.exe running the script in cli, or a wrapper? PSCustomObject在ISE中有效,但在powershell.exe中无效 - PSCustomObject Works in ISE but not in powershell.exe Powershell 脚本在没有“powershell.exe -File”的情况下无法正常工作,并且在“powershell.exe -File”的情况下也无法正常工作 - Powershell script not working without "powershell.exe -File" and it's also not working properly with "powershell.exe -File" Powershell脚本可在ISE中使用,但不能在Power With Powershell中运行 - Powershell script works in ISE but not in Run With Powershell
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM