Skip to content

Instantly share code, notes, and snippets.

@LeeSartorelli
Created May 31, 2018 03:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LeeSartorelli/ae35371cc180f7a8771e6c9bc5058f4a to your computer and use it in GitHub Desktop.
Save LeeSartorelli/ae35371cc180f7a8771e6c9bc5058f4a to your computer and use it in GitHub Desktop.
Installs an MSI remotely on multiple computers.
<#
.SYNOPSIS
Install software
.DESCRIPTION
This script installs software on a list of servers, using powershell remoting. It requires an .msi installer.
The script loops through each server from the list. The target file will be copied and installed using msiexec (silently). EVERY action will be recorded in a file specified after /L switch.
Then the script verifies that the application is installed and is the correct version
.NOTES
Author: Lee Sartorelli
Created: 31/5/2018
Powershell version 3 is required to pass variables to the remote session.
#>
#Requires -Version 3
#Modify variables as required for the environment
$serverList = Get-Content C:\temp\list.txt #List of the servers - one per row. No semicolons, etc
$sourceFile = "\\servername\C$\temp\installer.msi" #Full path to installer file - must be a UNC path
$msiName = "installer.msi" #Name of the msi file
$targetPath = "C:\temp" #Destination path to copy installer to
$msiArgs = "/i $targetPath\installer.msi AGREETOLICENSE=Yes /quiet" #Install arguments for msiexec
$logPath = "\\servername\C$\temp" #Path for msi log. Should be a UNC path or logs will be saved locally on each server
$appName = "Softwarename" #App name as it appears in Registry
$appVersion = "1.0.2.0" #Version of the app being installed, as it appears in the registry
foreach ($s in $serverList)
{
try
{
#Create remote session
$session = New-PSSession -ComputerName $s
#Copy file to server
Write-Output "Copying install file to $s"
Invoke-Command -Session $session -ScriptBlock {
Copy-Item $using:sourceFile $using:targetPath
}
#Install Application
Write-Output "Starting installation on $s"
$return=Invoke-Command -Session $session -ScriptBlock {
$p = Start-Process "msiexec" -ArgumentList "$using:msiArgs /L* $using:logPath\$using:s.log" -Wait -Passthru
}
#Check registry for installed and correct version
Invoke-Command -Session $session -ScriptBlock {
$regPath = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\*";
$installedApp = Get-ItemProperty $regPath | where {$_.DisplayName -eq $using:appName -and $_.DisplayVersion -eq $using:appVersion};
If ($installedApp)
{
Write-Host "$using:appName successfully installed on $using:s" -ForegroundColor Green
}
Else
{
Write-Host "Something went wrong. Check the install log for more information: $using:logPath\$using:s.log" -ForegroundColor Red
}
}
}
catch
{
Write-Host "Unable to install $appName on $s" -ForegroundColor Red
Write-Output $_
}
finally
{
#Clean up
Invoke-Command -Session $session -ScriptBlock {
Remove-Item -Path $using:targetPath\$using:msiName
}
Remove-PSSession $session
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment