我有一个运行powershell脚本的批处理文件,我需要将两个参数传递给脚本。在我的批处理中,它看起来像这样:
powershell -executionpolicy remotesigned -File "my script.ps1" "my path1\" "my path2\somefile.txt"
现在,我的powershell脚本的参数完全搞砸了,我得到了:
我尝试用'
和`
字符封装参数,但我几乎得到了想要的东西,但是第二个引号字符仍保留在powershell脚本中。我得到的是:
powershell -executionpolicy remotesigned -File "my script.ps1" "'my path1\'" "'my path2\somefile.txt'"
我想要的是
我知道我可以删除Powershell脚本中的多余引号,但是有更好的方法吗?换句话说,我如何以使它们保持理智的方式将这些参数传递给Powershell?
测试powershell脚本:
echo $args[0]
echo $args[1]
我设法弄清楚了。问题是第一个参数中的尾部斜杠。此斜杠使我的引号转义,因此,它不能用作引号(但无论出于何种原因,powershell仍会在以后的空格处剪切字符串)。
我的批处理文件是自动生成的,但是我设法通过在第一个参数的末尾添加另一个斜杠(应该以斜杠结尾)来解决该问题。
powershell -executionpolicy remotesigned -File "my script.ps1" "my path1\\" "my path2\somefile.txt"
此问题可能并非特定于批处理文件,而且很可能还会影响从命令行运行Powershell脚本。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句