Install HyperV or another feature using installshield

I am using installshield 2013, but this may apply to other versions.

My goal was to be able install the hyperV feature on a windows 2012 server using installshield.

First you follow the directions at https://helpnet.flexerasoftware.com/installshield20helplib/Content/helplibrary/CAPowerShell.htm

Add the property IS_PS_EXECUTIONPOLICY and set it to Unrestricted

InstallShield IS_PS_EXECUTIONPOLICY

Add a Predefined System Search to check for powershell

InstallShield Add System Search

Installshield system search powershell

Create a new custom action. I am using a powershell from a binary table

InstallShield Add custom action

I am being lazy so the script I am running is on my desktop
Add Install Exec Sequence: After InstallFinalize
Add Install Exec Condition: POWERSHELLVERSION

InstallShield PowerShell custom Action

Now comes the magic,
Installshield runs using 32 bit mode by default, which means it also runs the 32bit powershell verison, From what I can tell the 32bit version of PowerShell doesn’t implement a bunch of stuff, so you either have to convert your installshield project to strict 64bit or put some magic at the top of you Powershell script to switch it to 64bit

To set your installshield project to strict 64bit (see https://www.youtube.com/watch?v=yfOFvsfjSeA)

set the template x64; 1033
Installshield 64bit template

then you must tell the release to be strict about using 64bit components only

InstallShield Release Strict

The other option is to use the magic from https://www.nivot.org/blog/post/2012/12/18/Ensuring-a-PowerShell-script-will-always-run-in-a-64-bit-shell. I choose to use the magic at the top of my power shell since I may have to install 32 bit components as part of my installer, so I didn’t want to be restricted to only installing 64bit components.

# am I running in 32 bit shell?
if ($pshome -like "*syswow64*") {
    write-warning "Restarting script under 64 bit powershell"

    # relaunch this script under 64 bit shell
    # if you want powershell 2.0, add -version 2 *before* -file parameter
    & (join-path ($pshome -replace "syswow64", "sysnative") powershell.exe) -file `
        (join-path $psscriptroot $myinvocation.mycommand) @args

    # exit 32 bit script
    exit
}

# start of script for 64 bit powershell

 whoami | Add-Content -Path c:\file.txt
 echo "check admin" | Add-Content -Path c:\file.txt
 $64bit = [Environment]::Is64BitProcess

 ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
        [Security.Principal.WindowsBuiltInRole] "Administrator") | Add-Content -Path c:\file.txt

Add-Content -Path c:\file.txt -Value (’64-bit process = ‘ + $64bit);
echo "check feature" | Add-Content -Path c:\file.txt
Get-WindowsFeature | Where-Object {$_.Name -eq "Hyper-V"}  | Add-Content -Path c:\file.txt
$check = Get-WindowsFeature | Where-Object {$_.Name -eq "Hyper-V"}
If ($check.Installed -ne "True") {
        echo "notInstalled" | Add-Content -Path c:\file.txt
        #Install/Enable SNMP Services
        #Add-WindowsFeature SNMP-Services | Out-Null
        echo $check.Installed | Add-Content -Path c:\file.txt
        echo "Do Install" | Add-Content -Path c:\file.txt
        Install-WindowsFeature –Name Hyper-V -IncludeManagementTools -whatif | Out-File -Append c:\file.txt
}
else {
  echo "Installed" | Add-Content -Path c:\file.txt
}

Please excuse the c:\file.txt, those were my way of debugging to see what was going on.

I still need to work on handling the required reboot, but that is tomorrows problem

Leave a Reply

Your email address will not be published. Required fields are marked *