从具有“路径中的非法字符”的位置执行 CMD.EXE 中的 Powershell 脚本

2024-01-06

我试图在 cmd.exe 中调用 Powershell 脚本,该脚本位于如下位置:c:Data\foo - bar\location-1\ShellScript.ps1

当我调用这个脚本时,我尝试在路径周围使用单引号和双引号,但没有成功。

PowerShell.exe -File "c:\Data\foo - bar\location-1\ShellScript.ps1" Arg1 Arg2

根据我所读到的内容,我认为上面的内容可以工作,但这不起作用,单引号也不起作用。

我很欣赏任何想法。

谢谢 *编辑*我的示例路径中输入错误。对不起。


一种解决方案是迁移到 PowerShell v3,这可以正常工作:

PS> powershell.exe -file 'C:\temp\foo - bar\location-1\foo.ps1' arg1 arg2
made it!, args are arg1, arg2

如果您需要留在 V2 上,请尝试转义空格,例如:

PS> powershell.exe -file "C:\temp\foo` -` bar\location-1\foo.ps1" arg1 arg2

从 cmd.exe 中,这应该可以工作:

C:\> PowerShell.exe -Command "& {& 'c:\Data\foo - bar\location-1\ShellScript.ps1' Arg1 Arg2}"
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从具有“路径中的非法字符”的位置执行 CMD.EXE 中的 Powershell 脚本 的相关文章

随机推荐