在 powershell 调用后批处理一个衬里追加第二个命令

Batch one liner append second command after powershell call

我有一个批处理文件,结构如下:

cd "C:\my\scripts\directory
powershell -f myPowershellSCript.ps1
exit %errorlevel%

这个批处理文件正在通过内部远程 shell 应用程序(对我来说主要是一个黑盒子)以非交互方式发送到另一台机器 运行。我可以执行该应用程序并在本地终端上查看它的输出。该脚本正在完成 powershell 脚本,然后直接返回到测试机器上的远程 shell,而没有 运行 批处理文件中的最后一行。我看到 cmd.exe shell 返回到远程计算机上 C:\my\scripts\directory 处的提示,然后等待。因为它是非交互式的,所以脚本永远不会完成。

我想将最后一条出口线标记到调用电源线的末尾shell,但我尝试过的所有方法(如下)均无效。我担心 powershell 将所有内容都作为输入,而不是将它们批量解释为两个单独的命令。

powershell -f SecurePaymentsTestLauncher.ps1 && exit 1
powershell -nonInteractive -f SecurePaymentsTestLauncher.ps1 && exit 1
powershell -nonInteractive -command "& 'SecurePaymentsTestLauncher.ps1'" && exit 1
powershell -nonInteractive -command "& 'SecurePaymentsTestLauncher.ps1'" ; exit 1
powershell -nonInteractive -f SecurePaymentsTestLauncher.ps1 ; exit 1

仍然产生相同的结果。远程执行没有 return。

当第一个命令是对 power 的调用时,如何将第二个命令附加到批处理文件行shell?

尝试

cd "C:\my\scripts\directory"
start powershell -f myPowershellSCript.ps1
exit %errorlevel%

在 powershell 前面使用 'start' 在批处理脚本主机之外实例化一个单独的 powershell 主机,这将允许您的 ps1 文件在您的批处理脚本直接进入 'exit %errorlevel%'

您的 PowerShell 进程是否退出状态 0? && conditional operator 只在它后面的命令退出 0 时才执行它前面的命令。如果你想 exit 1 不管 PowerShell 退出零还是非零,使用单个 &.

在您的 powershell 命令中,您可能还需要将 .ps1 脚本名称调用为 .\SecurePaymentsTestLauncher.ps1 并添加 -ExecutionPolicy RemoteSigned 参数。