Created
July 1, 2023 21:23
-
-
Save jpbruckler/cae6c42673b40f0cd6c42ee6ac2c802b to your computer and use it in GitHub Desktop.
Script to upgrade PowerShell Universal. Script assumes a service account is used.
This file contains hidden or 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
Write-Host ("Starting PowerShell Universal upgrade...") | |
$PSUSettingsPath = (Join-Path -Path $env:ProgramData -ChildPath '\PowerShellUniversal\appsettings.json') | |
if (-not (Test-Path $PSUSettingsPath)) { | |
$PSUSettingsPath = Read-Host ('Unable to find appsettings at {0}. Enter path to appsettings.json' -f $PSUSettingsPath) | |
} | |
$PSUSettings = Get-Content $PSUSettingsPath -Raw | ConvertFrom-Json -Depth 10 | |
$cred = get-credential -Message 'Enter credential for PowerShell Universal Service Account' | |
$msi = Get-Item .\PowerShellUniversal*.msi | Sort-Object -Property LastWriteTime -Descending | Select-Object -First 1 | |
$ver = ($msi.BaseName).split('.',2)[1] | |
$PSDefaultParameterValues = @{ | |
'Write-Host:ForegroundColor' = 'Yellow' | |
'Write-Host:BackgroundColor' = 'Black' | |
} | |
Write-Host ("Found PowerShell Universal installer version {0}: {1}" -f $ver, $msi.FullName) | |
Write-Host "Stopping PowerShell Universal service..." | |
try { | |
Get-Service PowerShellUniversal | Stop-Service -Force -Verbose -ErrorAction Stop | |
} | |
catch { | |
Write-Error ("Unable to stop PowerShell Universal service. Error: {0}" -f $PSItem.Exception.Message) | |
} | |
Write-Host ('Creating backup of UniversalAutomation repository ({0})' -f $PSUSettings.Data.RepositoryPath) | |
try { | |
Compress-Archive -Path $PSUSettings.Data.RepositoryPath -DestinationPath ".\PsuBackup-$(Get-Date -Format 'yyyy-MM-dd').zip" -Force -ErrorAction Stop | |
} | |
catch { | |
Write-Error ("Unable to create backup. Aborting upgrade. Error: {0}" -f $PSItem.Exception.Message) | |
} | |
$RepoFolder = $PSUSettings.Data.RepositoryPath | |
$ConnectionString = $PSUSettings.Data.ConnectionString | |
$ServiceAccount = $($cred.username) | |
$ServiceAccountPW = $($cred.getnetworkcredential().password) | |
$InstallLog = "{0}-install.log" -f $msi.FullName | |
Write-Host "Executing msiexec..." | |
msiexec /i ("{0}" -f $msi.FullName) /q /norestart /l*v $InstallLog REPOFOLDER="$RepoFolder" CONNECTIONSTRING="$ConnectionString" SERVICEACCOUNT="$ServiceAccount" SERVICEACCOUNTPASSWORD="$ServiceAccountPW" | |
Start-Sleep -Seconds 4 | |
Write-Host "Opening log file for monitoring.." | |
Get-Content -Path $InstallLog -Tail 20 -Wait |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment