PowerShell -Command .\Foo.ps1
Foo.ps1
:Function Foo($directory) { echo $directory } if ($args.Length -eq 0) { echo "Usage: Foo <directory>" } else { Foo($args[0]) }
尽管
Foo.ps1
位于我调用 Powershell 的目录中,但这会导致:The term '.\Foo.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
- 编辑:无法工作,因为 PowerShell 正在更改目录,因为
profile.ps1
包含cd C:\
- 编辑:无法工作,因为 PowerShell 正在更改目录,因为
然后我尝试调用它并指定脚本文件的完整路径,但无论我尝试什么,我都无法让它工作。 我相信我必须引用该路径,因为它包含空格,我需要将参数传递给脚本的文件名也是如此。
迄今为止最好的猜测:
PowerShell -Command "'C:\Dummy Directory 1\Foo.ps1' 'C:\Dummy Directory 2\File.txt'"
输出错误:
Unexpected token 'C:\Dummy Directory 2\File.txt' in expression or statement. At line:1 char:136.
最佳答案
试试这个:
powershell "C:\Dummy Directory 1\Foo.ps1 'C:\Dummy Directory 2\File.txt'"
关于powershell - 如何从命令行运行 Powershell 脚本并将目录作为参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13724940/