我对 powershell.exe 进行了调用,如下所示,它可以从我的命令行运行:
powershell.exe -noexit -command " & 'C:\Test\test.ps1' "
但是,当我在当前用户的 runonce-key 中完全按照这种方式输入该条目时,什么也没有发生。
从 runonce 或 run 调用 powershell.exe 传递参数的正确方法是什么?
尝试使用完整路径:
c:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -noexit "C:\Test\test.ps1"
或
c:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -noexit -command " & 'C:\Test\test.ps1' "
稍微不同的方法是将所有对 powershell.exe 的调用以及所有参数放入一个简单的 *.bat 文件中,然后从 RunOnce Key 调用该 *.bat 文件。这样你就不需要摆弄参数,这有时会造成混乱。您的 PowerShell 脚本甚至可以删除该 *.bat 文件,因为它只需要一次。
使用文件参数而不是命令。 您还可以使用其他一些参数来清理内容(noprofile、sta 和/或 WindowStyle)
这是我使用的示例。
C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -noprofile -sta -WindowStyle Hidden -File "C:\Test\Test.ps1"
您可以将其包裹在
cmd
中。这样它就可以访问正常的PATH
并且可以找到powershell。
例如
cmd /c (powershell -ExecutionPolicy Bypass -file "C:\Test\test.ps1")