Fitzgery
Fitzgery

Reputation: 698

Issues calling a Batch File through PowerShell

I'm having some issues when trying to call a batch file through PowerShell. When I run the batch file itsel, everything works fine; below is my PS Code

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

Below is my batch file

@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

and finally, below is what PS is spitting back to me

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

Upvotes: 2

Views: 134

Answers (1)

mklement0
mklement0

Reputation: 437062

  • No need to use cmd /c to invoke a batch file from PowerShell; just invoke it directly, which in the case of a quoted batch-file path requires use of &, the call operator.

  • Your batch file:

    • is missing @echo off to suppress echoing the commands before they're being executed
    • mistakenly tries to set environment variables without the SET command
    • mistakenly tries to use # to initiate a line comment.

In general, it looks like the code was copied from a Bash shell script and poorly adapted for cmd.exe.

Invocation from PowerShell:

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

Fixed batch-file content:

@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

The alternative is to make do without a batch file altogether, and do it all in 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

Upvotes: 2

Related Questions