Created
August 1, 2023 00:57
-
-
Save Firewaters/f83266d4279cfb51fcdb8e1a38d4bd03 to your computer and use it in GitHub Desktop.
Basic PowerShell Function to download and install MSI or executables on a system, this can also compare and upgrade based on file version.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Install-App { | |
<# | |
.DESCRIPTION | |
Basic PowerShell Function to download and install MSI or executables on a system, this can also compare and upgrade based on file version. | |
.PARAMETER msiURL | |
The url to the .msi file | |
.PARAMETER exeURL | |
The url to the .exe file | |
.PARAMETER InstallCheck | |
The executable to check if already installed | |
.PARAMETER VersionCheck | |
The miniumum version required, will update if older than this. | |
.PARAMETER ArgumentList | |
Arguments to pass to the installer | |
.PARAMETER additionalFiles | |
Additional files required within the directory. | |
.EXAMPLE | |
Install-App -msiURL "http://example.com/file.msi" ` | |
-InstallCheck "C:\Program Files\Application\Endpoint.exe" ` | |
-VersionMin "2.3" | |
#> | |
[CmdletBinding()] | |
[OutputType([PSCustomObject])] | |
param ( | |
[Parameter(Position = 0, Mandatory = 0)] | |
[string]$msiURL, | |
[Parameter(Position = 1, Mandatory = 0)] | |
[string]$exeURL, | |
[Parameter(Position = 2, Mandatory = 1)] | |
[string]$InstallCheck, | |
[Parameter(Position = 3, Mandatory = 0)] | |
[string]$VersionMin, | |
[Parameter(Position = 4, Mandatory = 0)] | |
[string]$ArgumentList, | |
[Parameter(Position = 5, Mandatory = 0)] | |
[array]$additionalFiles | |
) | |
$folder = [Environment]::GetFolderPath([System.Environment+SpecialFolder]::CommonApplicationData) | |
If ($msiURL) { $url = $msiURL } | |
If ($exeURL) { $url = $exeURL } | |
$fileName = ($url -split '^.*\/(.*)\.(.*)$') | |
$fileName = $fileName[1] + '-' + (Get-Date -Format M-yy) + '.' + $fileName[2] | |
If ($VersionMin) { | |
if (Test-Path $installCheck) { | |
if ($((Get-Command $installCheck).FileVersionInfo.FileVersion) -gt $versionMin) { | |
Return "Installed product is higher than required, Skipped $fileName" | |
} | |
} | |
} | |
elseif ((Test-Path $installCheck)) { | |
Return "Already Installed, Skipped $fileName" | |
} | |
[array]$FileSource = @($url) | |
if ($additionalFiles) { $FileSource = $FileSource += $additionalFiles } | |
if (-not (Test-Path "$($folder)\PSInstall\")) { | |
New-Item -Path $($folder) -Name "PSInstall" -ItemType Directory | Out-Null | |
} | |
try { | |
Import-Module BitsTransfer | |
foreach ($File in $FileSource) { | |
Start-BitsTransfer -Source $File -Destination "$($folder)\PSInstall\$($fileName)" | |
} | |
} | |
catch { | |
foreach ($File in $FileSource) { | |
Invoke-WebRequest -Uri $File -OutFile "$($folder)\PSInstall\$($fileName)" | |
} | |
} | |
foreach ($File in $FileSource) { | |
if (-not (Test-Path "$($folder)\PSInstall\$($fileName)")) { | |
throw "$($folder)\PSInstall\$($fileName) - does not exist" | |
} | |
} | |
Write-Host "Started $fileName Install on $($env:computername)" | |
If ($msiURL) { | |
$Process = Start-Process -FilePath "$env:systemroot\system32\msiexec.exe" ` | |
-ArgumentList $("/i ""$($folder)\PSInstall\$($fileName)"" $($ArgumentList) /log ""$($folder)\PSInstall\$($fileName).log"" ") ` | |
-Wait ` | |
-PassThru | |
If (@(0, 3010) -contains $Process.ExitCode) { | |
Return "Completed $fileName Install on $($env:computername)" | |
} | |
else { | |
Return "Failed to Install $fileName with Error: $($Process.ErrorCode)" | |
} | |
} | |
elseif ($exeURL) { | |
$Process = Start-Process -FilePath "$($folder)\PSInstall\$($fileName)" ` | |
-ArgumentList $("$($ArgumentList)") ` | |
-Wait ` | |
-PassThru | |
if (Test-Path $installCheck) { | |
Return "Completed $fileName Install on $($env:computername)" | |
} | |
else { | |
Return "Failed to Install $fileName" | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment