Merge pull request #467 from al-cheb/al-cheb/ref_azure_module_installation

Add Az 3.5.0 PowerShell module to Windows Server 2016/2019
This commit is contained in:
Alejandro Pauly
2020-02-28 10:37:17 -05:00
committed by GitHub
2 changed files with 132 additions and 236 deletions

View File

@@ -3,195 +3,66 @@
## Desc: Install Azure PowerShell modules
################################################################################
Add-Type -AssemblyName System.IO.Compression.FileSystem
function Download-Zip
{
[CmdletBinding()]
Param(
[Parameter(
Mandatory = $true
)]
[string]
$BlobUri
)
Write-Host "Downloading the zip from blob: '$BlobUri'"
$fileName = "azureps_" + "$(Get-Random)" + ".zip"
$targetLocation = Join-Path $ENV:Temp -ChildPath $fileName
Write-Host "Download target location: '$targetLocation'"
$webClient = New-Object Net.WebClient
$null = $webClient.DownloadFileAsync($BlobUri, $targetLocation)
while ($webClient.IsBusy) { }
Write-Host "Download complete. Target Location: '$targetLocation'"
return $targetLocation
}
function Extract-Zip
{
[CmdletBinding()]
Param(
[Parameter(
Mandatory = $true
)]
[string]
$ZipFilePath,
[Parameter(
Mandatory = $true
)]
[string]
$TargetLocation
)
Write-Host "Expanding the Zip File: '$ZipFilePath'. Target: '$TargetLocation'"
$null = [System.IO.Compression.ZipFile]::ExtractToDirectory($ZipFilePath, $TargetLocation)
Write-Host "Extraction completed successfully."
}
Set-PSRepository -InstallationPolicy Trusted -Name PSGallery
# We try to detect the whether Azure PowerShell is installed using .msi file. If it is installed, we find it's version, then it needs to be uninstalled manually (because the uninstallation requires the PowerShell session to be closed)
$regKey = "HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*"
$installedApplications = Get-ItemProperty -Path $regKey
$SdkVersion = ($installedApplications | Where-Object { $_.DisplayName -and $_.DisplayName.toLower().Contains("microsoft azure powershell") } | Select-Object -First 1).DisplayVersion
if($SdkVersion -eq $null)
{
Write-Host "No .msi Installation Present"
}
else
{
Write-Host "An Azure PowerShell Installation through installer has been detected. Please close this powershell session and manually uninstall the Azure PowerShell from the Add or Remove Programs in the Control Panel. Then, rerun this script from an Admin PowerShell"
throw "An Azure PowerShell Installation through installer has been detected. Please close this powershell session and manually uninstall the Azure PowerShell from the Add or Remove Programs in the Control Panel. Then, rerun this script from an Admin PowerShell"
}
# We will try to uninstall any installation of Azure PowerShell
$modules = Get-Module -Name Azure -ListAvailable
Write-Host "The Azure Modules initially present are:"
$modules | Select-Object Name,Version,Path | Format-Table
foreach($module in $modules)
{
# add logging for telling what module we are working on now
if(Test-Path -LiteralPath $module.Path)
{
try
{
Uninstall-Module -Name Azure -RequiredVersion $module.Version.tostring() -Force
}
catch
{
Write-Host "The Uninstallation of Azure Module version: $($module.Version.tostring()) failed with the error: $($_.Exception.Message) . Please Check if there isn't any other PowerShell session open."
throw $_.Exception.Message
}
}
}
$modules = Get-Module -Name AzureRM -ListAvailable
Write-Host "The AzureRM Modules initially present are:"
$modules | Select-Object Name,Version,Path | Format-Table
foreach($module in $modules)
{
# add logging for telling what module we are working on now
if(Test-Path -LiteralPath $module.Path)
{
try
{
Uninstall-Module -Name AzureRM -RequiredVersion $module.Version.tostring() -Force
}
catch
{
Write-Host "The Uninstallation of AzureRM Module version: $($module.Version.tostring()) failed with the error: $($_.Exception.Message) . Please Check if there isn't any other PowerShell session open."
throw $_.Exception.Message
}
}
}
#after this, the only installations available through a Get-Module cmdlet should be nothing
$modules = Get-Module -Name Azure -ListAvailable
foreach($module in $modules)
{
Write-Host "Module found: $($module.Name) Module Version: $($module.Version)"
if($module.Version.ToString() -ne " ")
{
Write-Host "Another installation of Azure module is detected with version $($module.Version.ToString()) at path: $($module.Path)"
throw "Azure module uninstallation unsuccessful"
}
}
$modules = Get-Module -Name AzureRM -ListAvailable
foreach($module in $modules)
{
write-host "Module found: $($module.Name) Module Version: $($module.Version)"
if($module.Version.ToString() -ne " ")
{
Write-Host "Another installation of AzureRM module is detected with version $($module.Version.ToString()) at path: $($module.Path)"
throw "AzureRM module uninstallation unsuccessful"
}
}
#### NOW The correct Modules need to be saved in C:\Modules
if($(Test-Path -LiteralPath "C:\Modules") -eq $true)
$installPSModulePath = 'C:\Modules'
if(-not (Test-Path -LiteralPath $installPSModulePath))
{
Write-Host "C:\Modules directory is already present. Beginning to clear it up completely"
Remove-Item -Path "C:\Modules" -Recurse -Force
Write-Host "Creating '$installPSModulePath' folder to store PowerShell Azure modules"
$null = New-Item -Path $installPSModulePath -ItemType Directory
}
mkdir "C:\Modules"
# Powershell Azure modules to install
$psAzureModulesToInstall = @{
"azurerm" = @(
"2.1.0"
"3.8.0"
"4.2.1"
"5.1.1"
"6.7.0"
"6.13.1"
)
$directoryListing = Get-ChildItem -Path "C:\Modules"
"azure" = @(
"2.1.0"
"3.8.0"
"4.2.1"
"5.1.1"
"5.3.0"
)
if($directoryListing.Length -gt 0)
"az" = @(
"1.0.0"
"1.6.0"
"2.3.2"
"2.6.0"
"3.1.0"
"3.5.0"
)
}
# Download Azure PowerShell modules
foreach($psmoduleName in $psAzureModulesToInstall.Keys)
{
Write-Host "C:\Modules was not deleted properly. It still has the following contents:"
$directoryListing
}
else {
Write-Host "The Directory is clean. There are no contents present in it"
}
# Download and unzip the stored AzurePSModules from the vstsagentools public blob
$extractLocation = "C:\Modules"
$azurePsUri = @(
"https://vstsagenttools.blob.core.windows.net/tools/azurepowershellmodules/AzurePSModules.M157.20190808.27979.zip",
"https://vstsagenttools.blob.core.windows.net/tools/azurepowershellmodules/AzureRmPSModules.M157.20190808.27379.zip",
"https://vstsagenttools.blob.core.windows.net/tools/azurepowershellmodules/AzPSModules.M163.20191211.17769.zip"
)
$azureRMModulePath = "C:\Modules\azurerm_2.1.0"
$azureModulePath = "C:\Modules\azure_2.1.0"
$finalPath = ""
$environmentPSModulePath = [Environment]::GetEnvironmentVariable("PSModulePath", "Machine")
$existingPaths = $environmentPSModulePath -split ';' -replace '\\$',''
if ($existingPaths -notcontains $azureRMModulePath) {
$finalPath = $azureRMModulePath
}
if ($existingPaths -notcontains $azureModulePath) {
if($finalPath -ne "") {
$finalPath = $finalPath + ";" + $azureModulePath
}
else {
$finalPath = $azureModulePath
Write-Host "Installing '$psmoduleName' to the '$installPSModulePath' path:"
$psmoduleVersions = $psAzureModulesToInstall[$psmoduleName]
foreach($psmoduleVersion in $psmoduleVersions)
{
$psmodulePath = Join-Path $installPSModulePath "${psmoduleName}_${psmoduleVersion}"
Write-Host " - $psmoduleVersion [$psmodulePath]"
try
{
Save-Module -Path $psmodulePath -Name $psmoduleName -RequiredVersion $psmoduleVersion -Force -ErrorAction Stop
}
catch
{
Write-Host "Error: $_"
exit 1
}
}
}
if($finalPath -ne "") {
[Environment]::SetEnvironmentVariable("PSModulePath", $finalPath + ";" + $env:PSModulePath, "Machine")
}
$env:PSModulePath = $env:PSModulePath.TrimStart(';')
foreach ($uri in $azurePsUri)
{
$targetFile = Download-Zip -BlobUri $uri
Extract-Zip -ZipFilePath $targetFile -TargetLocation $extractLocation
}
# Add AzureRM and Azure modules to the PSModulePath
$finalModulePath = '{0};{1};{2}' -f "${installPSModulePath}\azurerm_2.1.0", "${installPSModulePath}\azure_2.1.0", $env:PSModulePath
[Environment]::SetEnvironmentVariable("PSModulePath", $finalModulePath, "Machine")

View File

@@ -5,67 +5,92 @@
Import-Module -Name ImageHelpers -Force
$DefaultModule = Get-Module -Name AzureRM -ListAvailable | Select-Object -First 1
$env:PSModulePath = $env:PSModulePath + ";C:\Modules"
$azureModules = Get-Module -Name Azure -ListAvailable | Select-Object Name,Version,Path | Format-Table | Out-String
Write-Host "The Azure Modules finally present are:"
$azureModules
if( ($azureModules -match "2.1.0") -and ($azureModules -match "3.8.0") -and ($azureModules -match "4.2.1") -and ($azureModules -match "5.1.1"))
{
Write-Host "Required Azure modules are present"
}
else {
Write-Host "One or more required Azure modules are not present"
throw "One or more required Azure modules are not present."
}
$azureRMModules = Get-Module -Name AzureRM -ListAvailable | Select-Object Name,Version,Path | Format-Table | Out-String
Write-Host "The AzureRM Modules finally present are:"
$azureRMModules
if( ($azureRMModules -match "2.1.0") -and ($azureRMModules -match "3.8.0") -and ($azureRMModules -match "4.2.1") -and ($azureRMModules -match "5.1.1"))
{
Write-Host "Required AzureRM modules are present"
}
else {
Write-Host "One or more required AzureRM modules are not present"
throw "One or more required AzureRM modules are not present."
}
$azureModules = Get-Module -Name AzureRM -ListAvailable
# Adding description of the software to Markdown
$SoftwareName = "Azure/AzureRM Powershell modules"
$Description = @"
#### $($DefaultModule.Version)
This version is installed and is available via ``Get-Module -ListAvailable``
"@
Add-SoftwareDetailsToMarkdown -SoftwareName $SoftwareName -DescriptionMarkdown $Description
foreach( $module in $azureModules)
function Add-ModuleDescription
{
if($module.Version -ne $DefaultModule.Version)
param($DefaultModule, [String]$ModuleName)
# Adding description of the software to Markdown
$softwareName = "$moduleName PowerShell module"
if ($defaultModule)
{
$description = "#### $($defaultModule.Version)`n`nThis version is installed and is available via ``Get-Module -ListAvailable``"
}
else
{
$Description = ""
}
$CurrentModule = @"
#### $($module.Version)
Add-SoftwareDetailsToMarkdown -SoftwareName $softwareName -DescriptionMarkdown $description
if($moduleName -eq 'Az')
{
$prop = @{n="Version";e={[Version]$_.Directory.Name}},@{n="Path";e={$_.FullName}}
$azureModules = Get-ChildItem C:\Modules\az_*\Az\*\Az.psd1 | Select-Object $prop
}
else
{
$azureModules = Get-Module -Name $moduleName -ListAvailable | Sort-Object Version -Unique
}
This version is saved but not installed
_Location:_ $($module.Path)
foreach($module in $azureModules)
{
if($module.Version -ne $defaultModule.Version)
{
"@
Add-ContentToMarkdown -Content $CurrentModule
$currentModule = "#### $($module.Version)`n`nThis version is saved but not installed`n_Location:_ $($module.Path)"
Add-ContentToMarkdown -Content $currentModule
}
}
}
function Validate-AzureModule
{
param([String]$ModuleName, [String[]]$ModuleVersions)
foreach($moduleVersion in $moduleVersions)
{
$modulePath = "${installPSModulePath}\${moduleName}_${moduleVersion}"
# Import each module in PowerShell session
$job = Start-Job -ScriptBlock {
param($modulePath, $moduleName)
$env:PsModulePath = "$modulePath;$env:PsModulePath"
Import-Module -Name $moduleName
Get-Module -Name $moduleName
} -ArgumentList $modulePath, $moduleName
$isError = $job | Wait-Job | Foreach-Object ChildJobs | Where-Object {$_.Error}
if($isError)
{
Write-Host "Required '$moduleName' module '$moduleVersion' version is not present"
exit 1
}
$job | Receive-Job | Select-Object Name,Version,Path
Remove-Job $job
}
}
# Modules path
$installPSModulePath = 'C:\Modules'
# Validate Azure modules and versions
$azurermVersions = "2.1.0", "3.8.0", "4.2.1", "5.1.1", "6.7.0", "6.13.1"
Validate-AzureModule -ModuleName AzureRM -ModuleVersions $azurermVersions
$azureVersions = "2.1.0", "3.8.0", "4.2.1", "5.1.1", "5.3.0"
Validate-AzureModule -ModuleName Azure -ModuleVersions $azureVersions
$azVersions = "1.0.0", "1.6.0", "2.3.2", "2.6.0", "3.1.0", "3.5.0"
Validate-AzureModule -ModuleName Az -ModuleVersions $azVersions
# Get default modules version
$defaultAzureRMModule = Get-Module -Name AzureRM -ListAvailable
$defaultAzureModule = Get-Module -Name Azure -ListAvailable
# Add modules to the PSModulePath
$env:PSModulePath = $env:PSModulePath + ";C:\Modules"
# Adding description of the software to Markdown
Add-ModuleDescription -DefaultModule $defaultAzureRMModule -ModuleName AzureRM
Add-ModuleDescription -DefaultModule $defaultAzureModule -ModuleName Azure
Add-ModuleDescription -ModuleName Az