从具有“路径中的非法字符”的位置在CMD.EXE中执行Powershell脚本 [英] Executing a Powershell script in CMD.EXE from a location with "Illegal characters in path"
问题描述
我尝试在cmd.exe中调用Powershell脚本,脚本位于如下所示的位置:c:Data\foo - bar\location-1 \ShellScript.ps1
I am attempting to call a Powershell script in cmd.exe and the script is in a location that looks like this: c:Data\foo - bar\location-1\ShellScript.ps1
当我调用这个脚本时,我试着用单引号和双引号括住路径,没有运气。
When I am calling this script I have tried using single and double quotes around the path with no luck.
PowerShell.exe -Filec:\Data\foo - bar\location-1\ShellScript.ps1Arg1 Arg2
PowerShell.exe -File "c:\Data\foo - bar\location-1\ShellScript.ps1" Arg1 Arg2
根据我已经阅读的内容,我假设上面的工作,但是没有工作,也没有单引号。
From what I have read I assumed that the above would work but this did not work nor did single quotes.
我很感激任何想法。
感谢
*在示例路径上编辑* Mistype。抱歉。
Thanks *Edit * Mistype on my example path. Sorry.
推荐答案
一个解决方案是移动到PowerShell v3,在这里工作正常:
One solution is to move to PowerShell v3 where this works fine:
PS> powershell.exe -file 'C:\temp\foo - bar\location-1\foo.ps1' arg1 arg2
made it!, args are arg1, arg2
如果您需要保留V2,请尝试转义空格,例如:
If you need to stay on V2 try escaping the spaces e.g.:
PS> powershell.exe -file "C:\temp\foo` -` bar\location-1\foo.ps1" arg1 arg2
从cmd.exe,这应该工作:
From cmd.exe, this should work:
C:\> PowerShell.exe -Command "& {& 'c:\Data\foo - bar\location-1\ShellScript.ps1' Arg1 Arg2}"
这篇关于从具有“路径中的非法字符”的位置在CMD.EXE中执行Powershell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!