Skip to content

Instantly share code, notes, and snippets.

@StefanScherer
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save StefanScherer/fac6577c61679932330f to your computer and use it in GitHub Desktop.
Save StefanScherer/fac6577c61679932330f to your computer and use it in GitHub Desktop.
Fix PowerShell exit code with Hotfix of KB2552055 to be used in packer-windows autounattend.xml
@echo off
:: Windows 7 / Windows 2008 R2 require KB2552055 hotfix
:: This fixes a problem with wrong exitcode 0 instead of custom exitcode in PowerShell 2.0
setlocal
if defined ProgramFiles(x86) (
set link=http://hotfixv4.microsoft.com/Windows%%207/Windows%%20Server2008%%20R2%%20SP1/sp2/Fix373932/7600/free/438167_intl_x64_zip.exe
set msufilename=%TEMP%\Windows6.1-KB2552055-x64.msu
) else (
set link=http://hotfixv4.microsoft.com/Windows%%207/Windows%%20Server2008%%20R2%%20SP1/sp2/Fix373932/7600/free/438164_intl_i386_zip.exe
set msufilename=%TEMP%\Windows6.1-KB2552055-x86.msu
)
set zipfilename=%TEMP%\KB2552055.zip
echo Downloading Hotfix 2552055
powershell -Command "(New-Object System.Net.WebClient).DownloadFile('%link%', '%zipfilename%')" <NUL
echo Extracting Hotfix 2552055
powershell -Command "(New-Object -com Shell.Application).NameSpace('%TEMP%').CopyHere((New-Object -Com Shell.Application).NameSpace('%zipfilename%').items())" <NUL
echo Installing Hotfix 2552055
wusa %msufilename% /quiet /norestart
echo Cleanup Hotfix temp files
del /Q %msufilename%
del /Q %zipfilename%
@StefanScherer
Copy link
Author

This script is able to patch PowerShell 2.0 of Windows 7 / Windows 2008 R2 to have a correct exit code needed for Vagrant 1.6 to work properly.

Incorrect error code when you run a PowerShell script at a command prompt in Windows 7 or in Windows Server 2008 R2
http://support.microsoft.com/kb/2552055

Writing this script I learned a lot:

  • To get the download of a KB entry you will receive an email with the the download URL of the Hotfix.
  • The Exe file is a self-extracting ZIP container created with the Xceed Absolute Packager v1.1
  • It seems that is does not have any command line options to extract it in an unattended way.
  • There are two KB entries that explain command line options of Hotfix tools, but not all KB exe files support the options described (as I found out).
  • Extracting the exe with unzip from a Mac works out of the box.
  • Extracting the exe from Windows with PowerShell doesn't work.
  • Renaming the exe to have a .zip extension then works for PowerShell as well.
  • Installing a msu Update package does not work with msiexec.exe, but with wusa.exe.
  • There is no reboot needed as described in the Hotfix, probably as long as there is no running instance of PowerShell.exe.
  • That's why I use a Windows Batch script to start the whole download, extract, patch and test steps.
  • Tested on Windows 7 x64, but should also work on Windows 7 x86.
  • To make it work in packer-windows, I had to tweak it a little further and put it into the autounattend.xml.
  • It does not work from an OpenSSH session used by packer, so it moved into autounattend.xml

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment