powershell - 通过 PowerShell 调用批处理文件时出现问题

标签 powershell batch-file

我在尝试通过 PowerShell 调用批处理文件时遇到一些问题。当我运行批处理文件本身时,一切正常;下面是我的 PS 代码

cmd.exe /c "C:\Source\BuildSource\SATCOM\EBEM\LCT GUI 02.01.04\run_ebem_lct_020104.bat"

下面是我的批处理文件

@title EBEM LCT 02.01.04
PathTemp=%Path%
# Path=C:\Program Files\jre7\bin;%Path%
Path=C:\Program Files\jre7\bin;%Path%
start javaw -classpath .\EBEM_LCT_020104.jar;comm.jar ebem_lct.system.LCT_Client Path=%PathTemp% PathTemp= exit

最后,下面是 PS 回复给我的内容

C:\Users\localadmin\Desktop\PowerShell>PathTemp=C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\HID Global\ActivClient\;C:\Program Files\HID Global\ActivClient\;C:\Program Files\Tumbleweed\Desktop Validator\;C:\Program Files\Tumbleweed\Desktop Validator\x86;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\;C:\Program Files\Microsoft SQL Server\140\Tools\Binn\;C:\Program Files\Microsoft SQL Server\140\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\DTS\Binn\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files (x86)\IVI Foundation\VISA\WinNT\Bin\;C:\Program Files\IVI Foundation\VISA\Win64\Bin\;C:\Program Files (x86)\IVI Foundation\VISA\WinNT\Bin;C:\usr\bin;C:\Program Files\PuTTY\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files\dotnet\;C:\Users\localadmin\AppData\Local\Microsoft\WindowsApps;C:\Source\TDC-Certs\TDC-Certs\OpenSSL-Win64\bin;C:\Users\localadmin\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\localadmin\.dotnet\tools
'PathTemp' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\localadmin\Desktop\PowerShell># Path=C:\Program Files\jre7\bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\HID Global\ActivClient\;C:\Program Files\HID Global\ActivClient\;C:\Program Files\Tumbleweed\Desktop Validator\;C:\Program Files\Tumbleweed\Desktop Validator\x86;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\;C:\Program Files\Microsoft SQL Server\140\Tools\Binn\;C:\Program Files\Microsoft SQL Server\140\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\DTS\Binn\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files (x86)\IVI Foundation\VISA\WinNT\Bin\;C:\Program Files\IVI Foundation\VISA\Win64\Bin\;C:\Program Files (x86)\IVI Foundation\VISA\WinNT\Bin;C:\usr\bin;C:\Program Files\PuTTY\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files\dotnet\;C:\Users\localadmin\AppData\Local\Microsoft\WindowsApps;C:\Source\TDC-Certs\TDC-Certs\OpenSSL-Win64\bin;C:\Users\localadmin\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\localadmin\.dotnet\tools
'#' is not recognized as an internal or external command,
operable program or batch file.

C:\Users\localadmin\Desktop\PowerShell>Path=C:\Program Files\jre7\bin;C:\Program Files (x86)\Common Files\Oracle\Java\javapath;C:\ProgramData\Oracle\Java\javapath;C:\Program Files (x86)\Intel\iCLS Client\;C:\Program Files\Intel\iCLS Client\;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\WINDOWS\System32\WindowsPowerShell\v1.0\;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files\Intel\Intel(R) Management Engine Components\DAL;C:\Program Files (x86)\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files\Intel\Intel(R) Management Engine Components\IPT;C:\Program Files (x86)\HID Global\ActivClient\;C:\Program Files\HID Global\ActivClient\;C:\Program Files\Tumbleweed\Desktop Validator\;C:\Program Files\Tumbleweed\Desktop Validator\x86;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\130\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\140\Tools\Binn\;C:\Program Files\Microsoft SQL Server\140\Tools\Binn\;C:\Program Files\Microsoft SQL Server\140\DTS\Binn\;C:\Program Files (x86)\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\Tools\Binn\;C:\Program Files\Microsoft SQL Server\100\DTS\Binn\;C:\Program Files\Microsoft SQL Server\Client SDK\ODBC\110\Tools\Binn\;C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\Tools\Binn\;C:\Program Files\Microsoft SQL Server\120\DTS\Binn\;C:\WINDOWS\System32\OpenSSH\;C:\Program Files (x86)\IVI Foundation\VISA\WinNT\Bin\;C:\Program Files\IVI Foundation\VISA\Win64\Bin\;C:\Program Files (x86)\IVI Foundation\VISA\WinNT\Bin;C:\usr\bin;C:\Program Files\PuTTY\;C:\Program Files\Intel\WiFi\bin\;C:\Program Files\Common Files\Intel\WirelessCommon\;C:\Program Files\dotnet\;C:\Users\localadmin\AppData\Local\Microsoft\WindowsApps;C:\Source\TDC-Certs\TDC-Certs\OpenSSL-Win64\bin;C:\Users\localadmin\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\localadmin\.dotnet\tools

最佳答案

  • 无需使用cmd/c从PowerShell调用批处理文件;只需直接调用它,在引用批处理文件路径的情况下需要使用&call operator .

  • 您的批处理文件:

    • 缺少 @echo off 以在执行命令之前抑制命令的回显
    • 错误地尝试在不使用 SET 命令的情况下设置环境变量
    • 错误地尝试使用 # 启动行注释。

总的来说,代码看起来像是从 Bash shell 脚本复制而来,并且不太适合 cmd.exe

从 PowerShell 调用:

& "C:\Source\BuildSource\SATCOM\EBEM\LCT GUI 02.01.04\run_ebem_lct_020104.bat"

修复了批处理文件内容:

@echo off
setlocal

:: Set the window title - this will revert when the batch file exits,
:: so there is little point in doing this, given that `start` below
:: launches the application *asynchronously* and the batch file therefore
:: exits quickly.
title EBEM LCT 02.01.04

:: Temporarily add a folder to the PATH.
:: Thanks to `setlocal`, there is no need to restore the previous path afterwards.
Set "Path=C:\Program Files\jre7\bin;%Path%"

:: Start the application
start javaw -classpath .\EBEM_LCT_020104.jar;comm.jar ebem_lct.system.LCT_Client

替代方案是完全不用批处理文件,全部在 PowerShell 中完成:

# Save the previous $env:Path value and temporarily prepend a new folder.
$prevPath, $env:Path = $env:Path, "C:\Program Files\jre7\bin;$env:Path"

# Invoke the (GUI) application, which launches asynchronously.
javaw -classpath .\EBEM_LCT_020104.jar;comm.jar ebem_lct.system.LCT_Client

# Restore the original $env:Path value.
$env:Path = $prevPath

关于powershell - 通过 PowerShell 调用批处理文件时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60529052/

相关文章:

batch-file - 在批处理文件中获取原始参数或命令

windows - 批量将两个整数相乘

java - 获取 HmacSHA1 Java 哈希以匹配 Powershell 哈希

arrays - 在此对象上找不到 PowerShell 属性 Count

Windows 批处理文件 - 系统找不到指定的批处理标签

java - 如何在批处理文件中传递 java -jar 参数?

batch-file - netstat bat 脚本记录所有 RDP 连接

powershell - Powershell比较对象输出每项1行

powershell - 从路径中删除最后一个扩展名

powershell - 错误 'Get-WmiObject : Invalid class “Msvm_ImageManagementService”'