/build/static/layout/Breadcrumb_cap_w.png

KACE SMA 12 One-liners to switch BAT/CMD/PS1 files to 64-bit CMD/PS

The KACE Agent is 32-bit only; this can cause the following issues:

* 32-bit processes can't access 64-bit software registry entries (64-bit programs have access to SOFTWARE\WOW6432Node; there is no equivalent for 32-bit programs)

* Some command line arguments in Windows are 64-bit only

* Some PowerShell commands in Windows are 64-bit only


After much trial and error, and research, I have created 1-liners for both Command Prompt Batch (BAT/CMD) files and PowerShell files that will relaunch your scripts in the 64-bit version of CMD/PS.

Simply add these lines to the top of your Batch/PS script, and your batch file will relaunch in the 64-bit process.

For batch files, "@echo off" is not required, but will clean up the script's output to the screen and the KACE log files.


BATCH FILE

@echo off

IF /I "%__APPDIR__%" == "%WinDir%\SysWOW64\" (%windir%\sysnative\cmd.exe /C "%~f0" && exit /B)


POWERSHELL FILE

if ($pshome -like "*syswow64*") {& (join-path ($pshome -replace "syswow64", "sysnative")\powershell.exe) -file $myinvocation.mycommand.Definition @args; exit}



BATCH FILE EXPLANATION

IF "%__APPDIR__%" == "%WinDir%\SysWOW64\" ( - %__APPDIR__% is an undocumented environment variable. For CMD, this points to "%WinDir%\SysWOW64\" if a 32-bit CMD is running on 64-bit windows; If a 32-bit CMD is running on 32-bit windows, or 64-bit CMD is running on 64-bit windows, %__APPDIR__% will point to %WinDir%\System32\; the "/I" switch for FOR is to enable case insensitive mode

%windir%\sysnative\cmd.exe /C "%~f0" - This launches cmd.exe from the SysNative folder, which is a folder that appears only to 32-bit programs running on 64-bit Windows; this folder is an alias to %WinDir%\System32. Although others have said that simply running the 64-bit CMD from SysNative is enough to get their scripts to work, that didn't work for me. /C launches CMD and tells it to close after executing the script; %~f0 is for the full path to the script, with the script name.

&& exit /B) - After the script is finished running as 64-bit, we need to Exit to get out of the 64-bit CMD and back to the 32-bit CMD. The /B (when used inside a script) tells CMD to exit only the script (or subroutine) but not CMD. After the 64-bit CMD is finished running, this command will terminate the batch file. If we don't have this in here, then after the 64-bit CMD closes, the rest of the script (that you wanted to run in the 64-bit CMD) will run a second time under the 32-bit CMD.


POWERSHELL FILE EXPLANATION

if ($pshome -like "*syswow64*") { - $pshome contains the path to the folder the PowerShell executable is located in; if this folder path has "syswow64" in it, you're running a 32-bit PowerShell on 64-bit Windows.

& (join-path ($pshome -replace "syswow64", "sysnative")\powershell.exe) -file $myinvocation.mycommand.Definition @args; - "&" launches a program; then it launches PowerShell from the 64-bit folder; $myinvocation.mycommand.Definition is the full path to the script, with the script name; @args is for any command line arguments passed to the script.

exit} - Just like with Batch files, after the 64-bit PowerShell process exits, we need to exit the script, to avoid running the rest of the script a second time.


Comments

This post is locked

Don't be a Stranger!

Sign up today to participate, stay informed, earn points and establish a reputation for yourself!

Sign up! or login

Share

 
This website uses cookies. By continuing to use this site and/or clicking the "Accept" button you are providing consent Quest Software and its affiliates do NOT sell the Personal Data you provide to us either when you register on our websites or when you do business with us. For more information about our Privacy Policy and our data protection efforts, please visit GDPR-HQ