ps/Modules/Alkami.PowerShell.Configuration/Public/Update-AWSPowershellModule.ps1
2023-05-30 22:51:22 -07:00

168 lines
7.4 KiB
PowerShell

function Update-AWSPowerShellModule {
<#
.SYNOPSIS
Updates the AWS Powershell module from a PSGet feed
.DESCRIPTION
Configures parameter driven PSGet feed as a PS Repository to download from and install AWSPowerShell via Install-Module
.PARAMETER SourceLocation
[string] The nuget URI to a PSRepository feed. Defaults to https://packagerepo.orb.alkamitech.com/nuget/SRETools
.PARAMETER FeedName
[string] The name of the PSRepository feed to be registered. Defaults to SRETools
.PARAMETER ModuleName
[string] The module name to update. Defaults to AWSPowerShell
.PARAMETER DeprecatedModuleFolder
[string] The full path for any deprecated folders which should be removed as part of uninstall. Defaults to C:\Program Files (x86)\AWS Tools\PowerShell
.PARAMETER TargetVersion
[string] When specified, targets a specific remote version of the specified module. When unspecified, the latest remote version is used. Defaults to null.
.EXAMPLE
Update-AWSPowershellModule
[Update-AWSPowershellModule] : Current Available AWSPowerShell Module Version: 4.1.14.0
[Update-AWSPowershellModule] : Repository SRETools already configured correctly
[Update-AWSPowershellModule] : Looking for Available Remote Versions
[Update-AWSPowershellModule] : No newer version of AWSPowerShell available.
.EXAMPLE
Update-AWSPowershellModule -TargetVersion 4.1.14.0
[Update-AWSPowershellModule] : Current Available AWSPowerShell Module Version: 4.1.10.0
[Update-AWSPowershellModule] : Repository SRETools already configured correctly
[Update-AWSPowershellModule] : Looking for Remote Version 4.1.14.0
[Update-AWSPowershellModule] : Uninstalling Module AWSPowerShell version 4.1.10.0
[Update-AWSPowershellModule] : Installing Module AWSPowerShell version 4.1.14.0. This may take a few minutes.
[Update-AWSPowershellModule] : Module AWSPowerShell Version 4.1.14.0 installed successfully
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $false)]
[string]$SourceLocation = "https://packagerepo.orb.alkamitech.com/nuget/SRETools",
[Parameter(Mandatory = $false)]
[string]$FeedName = "SRETools",
[Parameter(Mandatory = $false)]
[string]$ModuleName = "AWSPowerShell",
[Parameter(Mandatory = $false)]
[string]$DeprecatedModuleFolder = "C:\Program Files (x86)\AWS Tools\PowerShell",
[Parameter(Mandatory = $false)]
[string]$TargetVersion = $null
)
$logLead = (Get-LogLeadName)
if ($ModuleName -match "\*") {
Write-Warning "$logLead : Wildcard module names are not allowed in this function. Remove any wildcards and re-execute"
return
}
$currentModule = Get-Module -ListAvailable -Name $ModuleName
if ($currentModule.Count -gt 1) {
$firstMatchingModule = $currentModule | Sort-Object -Property Version -Descending | Select-Object -First 1
Write-Warning "$logLead : More than one module version found with name $ModuleName. Installation will continue comparing against version $($firstMatchingModule.Version)."
Write-Warning "$logLead : All installed module versions will be removed as part of installation, if determined to be necessary based on this comparison version."
$currentModule = $firstMatchingModule
}
if ($null -ne $currentModule) {
if (($null -ne (Get-Module -Name $ModuleName)) -or ($null -ne ([System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object {$_.Location -match $ModuleName}))) {
Write-Host "$logLead : Module $ModuleName Loaded In Context. Force removing..."
Remove-Module $ModuleName -Force -ErrorAction SilentlyContinue
if ($null -ne ([System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object {$_.Location -match $ModuleName})) {
Write-Warning "$logLead : $ModuleName assemblies are still loaded in the host. If installation fails, you must run this command from a fresh runspace."
Write-Host "$logLead : Execute this command to see the loaded location:`n`n`t[System.AppDomain]::CurrentDomain.GetAssemblies() | Where-Object {`$_.Location -match `"$ModuleName`"}"
}
}
Write-Host "$logLead : Current Available $ModuleName Module Version: $($currentModule.Version)"
if ((-Not [String]::IsNullOrEmpty($TargetVersion)) -and ($currentModule.Version.ToString() -match $TargetVersion)) {
Write-Host "$logLead : Version from arguments ($TargetVersion) matches local module version ($($currentModule.Version)). Exiting"
return
}
} else {
Write-Host "$logLead : Module $ModuleName is not Installed"
}
$currentRepo = Get-PSRepository -Name $FeedName -ErrorAction SilentlyContinue
if ($null -eq $currentRepo) {
Write-Host "$logLead : Registering Feed Name: $FeedName"
Register-PSRepository -Name $FeedName -SourceLocation $SourceLocation
} elseif ($currentRepo.SourceLocation -ne $SourceLocation) {
Write-Host "$logLead : Correcting Feed URL for Feed Name: $FeedName from $($currentRepo.SourceLocation) to $SourceLocation"
Set-PSRepository -Name $FeedName -SourceLocation $SourceLocation
} else {
Write-Host "$logLead : Repository $FeedName already configured correctly"
}
if ([String]::IsNullOrEmpty($TargetVersion)) {
Write-Host "$logLead : Looking for Available Remote Versions"
$latestRemoteModule = Find-Module -Name $ModuleName -Repository $FeedName -ErrorAction SilentlyContinue
} else {
Write-Host "$logLead : Looking for Remote Version $TargetVersion"
$latestRemoteModule = Find-Module -Name $ModuleName -Repository $FeedName -RequiredVersion $TargetVersion -ErrorAction SilentlyContinue
}
if ($null -eq $latestRemoteModule) {
Write-Warning "$logLead : Could not find module on remote repository. Verify PSRepository configuration and rerun"
return
} elseif ($null -ne $currentModule -and ($currentModule.Version.ToString() -contains $latestRemoteModule.Version)) {
Write-Host "$logLead : No newer version of $ModuleName available."
return
} elseif ($null -ne $currentModule) {
Write-Host "$logLead : Uninstalling All Versions of Local Module $ModuleName"
Uninstall-Module -Name $ModuleName -AllVersions
if (Test-Path $DeprecatedModuleFolder) {
Write-Host "$logLead : Removing Old Module Folder: $DeprecatedModuleFolder"
Remove-FileSystemItem -Path $DeprecatedModuleFolder -Recurse -Force
}
}
if ([String]::IsNullOrEmpty($TargetVersion)) {
Write-Host "$logLead : Installing Module $ModuleName version $($latestRemoteModule.Version). This may take a few minutes."
Install-Module -Name $ModuleName -Repository $FeedName -Force -SkipPublisherCheck
} else {
Write-Host "$logLead : Installing Module $ModuleName version $TargetVersion. This may take a few minutes."
Install-Module -Name $ModuleName -Repository $FeedName -Force -SkipPublisherCheck -RequiredVersion $TargetVersion
}
$newModule = Get-Module -ListAvailable -Name $ModuleName
if ($null -eq $newModule -or $newModule.Version.ToString() -notmatch $latestRemoteModule.Version) {
Write-Warning "$logLead : Installation Unsuccessful. Review logs for errors."
} else {
Write-Host "$logLead : Module $ModuleName Version $($newModule.Version) installed successfully"
}
}