Quantcast
Channel: PowershellTaskForce » Products
Viewing all articles
Browse latest Browse all 12

Install MSU files with PowerShell

$
0
0

I have been working with fixing som stuff with IE 11 installation with SCCM in a OSD TS.

My solution to install some needed KB files was to fix a PowerShell script that will look in the folder \source\ and x86 for x86 computers and x64 for x64 computers for all .msu files.

Then take the KB name and check if the KB is installed or not.

Skärmklipp2

Skärmklipp6

The x86 folder:
Skärmklipp5

If the KB is not installed the script will install it.

And log everything to a log file.

Skärmklipp8

Skärmklipp7

 

I use c:\windows\temp as log folder because it did work best for me during the installation.

Function Add-MyLog
{
	<#
	.SYNOPSIS
		Adding events to a log file
	
	.DESCRIPTION
		A detailed description of the Add-MyLog function.
	
	.PARAMETER Path
		Path to the log file
	
	.PARAMETER Event
		What was done
	
	.NOTES
		Author:          Fredrik Wall
        Email:           fredrik@poweradmin.se
        Created:         2011-03-05
        Updated:         2014-08-27
	#>
	
	[CmdletBinding()]
	param
	(
		$LogFile,
		$Event
	)
	
	if (!(Test-Path $LogFile))
	{
		"Date;Event" | Out-File $LogFile
	}
	
	$myTime = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
	$myLogLine = $myTime + ";" + $Event
	$myLogLine | Out-File $LogFile -Append
}

$scriptDir = split-path -parent $MyInvocation.MyCommand.Path
$scriptName = split-path -leaf $MyInvocation.MyCommand.Path
$architecture = $env:PROCESSOR_ARCHITECTURE

Switch ($architecture)
{
	"x86" { $sourceRoot = "$scriptDir\Source\x86" }
	"AMD64" { $sourceRoot = "$scriptDir\Source\x64" }
	Default { }
}

$LogFile = "c:\Windows\Temp" + "\" + "$ScriptName.log"

Add-MyLog -Event "Starting installation" -LogFile $LogFile
Add-MyLog -Event "ScriptDir: $scriptDir" -LogFile $LogFile
Add-MyLog -Event "SourceRoot: $sourceRoot" -LogFile $LogFile
Add-MyLog -Event "ScriptName: $scriptName" -LogFile $LogFile

$myPreReq = Get-ChildItem $sourceRoot | where { $_.Extension -match "msu" }
foreach ($myHotfix in $myPreReq)
{
	
	$myPreReqHotfix = (($myHotfix.Name).Split("-"))[1]
	if (!(Get-HotFix $myPreReqHotfix -ErrorAction SilentlyContinue))
	{
		try
		{
			Add-MyLog -Event "$myPreReqHotfix : <false>" -LogFile $LogFile
			Add-MyLog -Event "$myPreReqHotfix : Installing" -LogFile $LogFile
			start-process wusa.exe -argumentlist "$($myHotfix.FullName) /quiet /norestart" -wait
			#CMD /C "wusa.exe  $($myHotfix.FullName) /quiet /norestart"
			if ($lastexitcode -eq "-2145124329")
			{
				Add-MyLog -Event "$myPreReqHotfix : ERROR INSTALLING! : $($_.Exception.Message) $($lastexitcode)" -LogFile $LogFile
			}
			else
			{
				Add-MyLog -Event "$myPreReqHotfix : <true>" -LogFile $LogFile
			}
			
		}
		catch
		{
			Add-MyLog -Event "$myPreReqHotfix : ERROR INSTALLING! : $($_.Exception.Message)" -LogFile $LogFile
		}
	}
	else
	{
		Add-MyLog -Event "$myPreReqHotfix : <true>" -LogFile $LogFile
	}
}

Add-MyLog -Event "Finnishing installation" -LogFile $LogFile

This script will work to install all .msu files not just for IE 11.
But the script is just designed for one OS.

Thanks to Micke (“The Deployment Bunny“) for haveing a nice blog with lots of nice stuff.
This script has some influences from his scripts.


Viewing all articles
Browse latest Browse all 12

Trending Articles