Patch/Invoke-AutoUpdate.ps1

# <#
# .SYNOPSIS

# .DESCRIPTION

# .EXAMPLE

# .NOTES
# #>
# function Invoke-AutoUpdate {
# [alias("Automagic")]
# [cmdletbinding(SupportsShouldProcess)]
# param(
# # After the download completes all patches and extensions will be installed
# [switch] $autoInstall,
# # Skip the 45s delay between Sync and Patch Data update request. Usefull when installing small patches in a batch.
# [Parameter(DontShow)]
# [switch] $noSleep
# )

# # Panic mode)
# $ErrorActionPreference = "Stop"

# #Region Preparation
# # Loading functions
# . (Join-Path $PSScriptRoot "Cmdlets\Misc\Save-DownloadedFile.ps1")

# # Reading config file
# if (!$PatchHelperConfig) {
# throw "Patch Helper is not configured. Run configure procedure."
# }

# # Fetching certificate
# $cert = Get-ChildItem -Path "Cert:\CurrentUser\My" | Where-Object -Property Issuer -EQ -Value "CN=ADS Russia LLC"
# if (!$cert) {
# $cert = Get-ChildItem -Path "Cert:\LocalMachine\My" | Where-Object -Property Issuer -EQ -Value "CN=ADS Russia LLC"
# if (!$cert) { throw "Certificate not found." }
# }
# $cert | Write-Verbose
# #endregion

# #Region Patches update
# # Checking installed patches
# Write-Host "Getting patches"
# $webSession = $null
# foreach ($i in 1..2) {
# $latestPatch = Get-PatchInfo -instanceName $PatchHelperConfig.DefaultInstance -level $i
# if ($latestPatch) { $latestPatch = $latestPatch.Number } else { $latestPatch = 0 }

# # Downloading updates
# # if($i -eq 1) { $latestPatch = 200 } else { $latestPatch = 60 } # debug
# if ($i -eq 1) { $layer = "dms" } else {
# if ($PatchHelperConfig.LayerName) { $layer = $PatchHelperConfig.LayerName }
# else { break; }
# }
# Write-Host "For layer $i latest patch detected is $latestPatch. Fetching updates."

# $checkUrl = "$($PatchHelperConfig.ReportURL)/patches/$layer`?fromNumber=$latestPatch"
# Write-Verbose $checkUrl

# $available = Invoke-WebRequest -Uri $checkUrl -Certificate $cert -SessionVariable 'webSession'
# if ($available.Content) {
# $patchList = $available.Content | ConvertFrom-Json
# if ($patchList) {
# if ($patchList -isnot [array]) { $patchList = @($patchList) }
# $i = 0; $cnt = $patchList.Count;
# Write-Progress -Activity "Downloading patch" -PercentComplete 0
# $patchList | ForEach-Object {
# $downloadUrl = "$($PatchHelperConfig.ReportURL)/patches/a/($($_.ID))"
# Write-Verbose $downloadUrl
# Write-Progress -Activity "Downloading patch" -Status ($_.Name) -PercentComplete (($i++) / $cnt * 100)
# if ($PSCmdlet.ShouldProcess("$($_.ID)", "Download patch for layer $layer")) {
# $ProgressPreferenceBack = $ProgressPreference; $ProgressPreference = "SilentlyContinue";
# Invoke-WebRequest -Uri $downloadUrl -Certificate $cert -WebSession $webSession `
# | Save-DownloadedFile -Directory $PatchHelperConfig.DefaultPatchFolder
# $ProgressPreference = $ProgressPreferenceBack;
# }
# }
# Write-Progress -Activity "Downloading patch" -Completed
# }
# }
# }

# # Install downloaded patches
# if ($autoInstall) {
# foreach ($i in 1..2) {
# if (($i -eq 2) -and !$PatchHelperConfig.LayerName) { break; }
# Write-Host "Installing patches for layer $i."

# if ($PSCmdlet.ShouldProcess("$($PatchHelperConfig.DefaultPatchFolder)", "Install-PatchMnogo -instanceName $($PatchHelperConfig.DefaultInstance) -folder ""$($PatchHelperConfig.DefaultPatchFolder)"" -level $i")) {
# Install-PatchMnogo -instanceName $PatchHelperConfig.DefaultInstance -folder $PatchHelperConfig.DefaultPatchFolder -level $i -noSleep:$noSleep
# }
# }
# }
# #endregion

# #Region Extensions ipdate
# # Download extensions
# if ($PatchHelperConfig.apps) {
# Write-Host "Getting Extensions"
# $webSession = $null
# foreach ($i in 1..2) {
# $latestPatch = Get-PatchInfo -instanceName $PatchHelperConfig.DefaultInstance -level $i
# if ($latestPatch) { $latestPatch = $latestPatch.Number } else { $latestPatch = 0 }

# # Downloading updates
# # if($i -eq 1) { $latestPatch = 200 } else { $latestPatch = 60 } # debug
# if ($i -eq 1) { $layer = "dms" } else {
# if ($PatchHelperConfig.LayerName) { $layer = $PatchHelperConfig.LayerName }
# else { break; }
# }
# Write-Host "For layer $i latest patch detected is $latestPatch. Fetching updates."
# $checkUrl = "$($PatchHelperConfig.ReportURL)/extensions/$layer`?filter=$($PatchHelperConfig.apps -join ",")&latestPatch=$latestPatch"
# Write-Verbose $checkUrl

# $available = Invoke-WebRequest -Uri $checkUrl -Certificate $cert -SessionVariable 'webSession'
# if ($available.Content) {
# $patchList = $available.Content | ConvertFrom-Json
# if ($patchList) {
# if ($patchList -isnot [array]) { $patchList = @($patchList) }
# $i = 0; $cnt = $patchList.Count;
# Write-Progress -Activity "Downloading extension" -PercentComplete 0
# $patchList | ForEach-Object {
# $downloadUrl = "$($PatchHelperConfig.ReportURL)/extensions/a/($($_.ID))"
# Write-Verbose $downloadUrl
# Write-Progress -Activity "Downloading extension" -Status ($_.Name) -PercentComplete (($i++) / $cnt * 100)
# if ($PSCmdlet.ShouldProcess("$($_.ID)", "Download extension for layer $layer")) {
# $ProgressPreferenceBack = $ProgressPreference; $ProgressPreference = "SilentlyContinue";
# Invoke-WebRequest -Uri $downloadUrl -Certificate $cert -WebSession $webSession `
# | Save-DownloadedFile -Directory $PatchHelperConfig.DefaultPatchFolder
# $ProgressPreference = $ProgressPreferenceBack;
# }
# }
# Write-Progress -Activity "Downloading extension" -Completed
# }
# }
# }

# # Upgrade extensions
# if ($autoInstall) {
# Write-Host "Upgrading extensions"

# if ($PSCmdlet.ShouldProcess("$($PatchHelperConfig.DefaultPatchFolder)", "Upgrade-Extension -instanceName $($PatchHelperConfig.DefaultInstance) -path $($PatchHelperConfig.DefaultPatchFolder)")) {
# Upgrade-Extension -instanceName $PatchHelperConfig.DefaultInstance -path $PatchHelperConfig.DefaultPatchFolder
# }
# }
# }
# #endregion
# }

# Export-ModuleMember -Alias "Automagic" -Function "Invoke-AutoUpdate"