04 April 2013

Powershell: Installation Script Functions

Here are several snippets I have created for writing powershell application installation scripts. These make it a lot faster by standardizing the functions to use as reusable code.

Display a Balloon Tip
 Function BalloonTip($ApplicationName, $Message, $DisplayTime){  
      [system.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms') | Out-Null  
      $balloon = New-Object System.Windows.Forms.NotifyIcon  
      $path = Get-Process -id $pid | Select-Object -ExpandProperty Path  
      $icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)  
      $balloon.Icon = $icon  
      $balloon.BalloonTipIcon = 'Info'  
      $balloon.BalloonTipTitle = "{Company Name}"  
      $balloon.BalloonTipText = $ApplicationName+[char]13+[char]13+$Message  
      $balloon.Visible = $true  
      $balloon.ShowBalloonTip($DisplayTime)  
 }  

Create a log folder and set log file
 $Global:LogFolderName = ""  
 Function CreateLogFolder{  
      If((Test-Path -path c:\temp) -ne $true){  
           New-Item -ItemType directory -Path c:\temp  
      }  
      If((Test-Path -path c:\temp\$Global:LogFolderName) -ne $true){  
           New-Item -ItemType directory -Path c:\temp\$Global:LogFolderName  
      }  
      $Global:LogFile = "c:\temp\"+$Global:LogFolderName+"\"+$Global:LogFolderName+".log"  
      Write-Host $Global:LogFile  
 }  
 CreateLogFolder  

Enable/Disable File Security Checks
 #Disable File Security  
 $env:SEE_MASK_NOZONECHECKS = 1  
 #Enable File Security  
 Remove-Item env:\SEE_MASK_NOZONECHECKS  

Determine if the OS is 32-bit or 64-bit
 $Global:OS  
 Function GetOSArchitecture{  
      $Global:OS=Get-WMIObject win32_operatingsystem  
      $Global:OS.OSArchitecture  
      #Answers: 32-bit, 64-bit  
 }  

Retrieve the relative path the powershell script was executed from
 Clear-Host  
 $Global:RelativePath  
 Function GetRelativePath{  
      $Global:RelativePath=(split-path $SCRIPT:MyInvocation.MyCommand.Path -parent)+"\"  
      Write-Host $Global:RelativePath  
 }  
 GetRelativePath  

Uninstall previous versions of programs
 Function UninstallPreviousPrograms{  
      #Change '%application%' to whatever app you are calling  
      $Output = wmic product where "description like '%sketchup%'" get IdentifyingNumber  
      $Output | ForEach-Object {  
           $_ = $_.Trim()  
        if(($_ -ne "IdentifyingNumber")-and($_ -ne "")){  
          $GUID = $_  
        }  
      }  
      $Arguments = "/X"+[char]32+$GUID+[char]32+"/qb- /norestart"  
      (Start-Process -FilePath "msiexec.exe" -ArgumentList $Arguments -Wait -Passthru).ExitCode  
 }  
 UninstallPreviousPrograms  

2 comments:

  1. Thanks! The disable file security checks was exactly what I needed to run a batch uninstall across my server farm. I'm not sure why, but the syntax below didn't work for me in my powershell script. Yours did!

    [Environment]::SetEnvironmentVariable("SEE_MASK_NOZONECHECKS","1","Machine")

    ReplyDelete
  2. Oh yeah. That NOZONECHECKS is a lifesaver. Thanks!

    ReplyDelete