Files
runner-images-sangeeth/images/win/scripts/Installers/Install-AzureModules.ps1
Amruta Kawade f6f2e3681e [Windows] Az Modules Size Reduction On Image (#2827)
* [Windows] Az Modules Size Reduction On Image

* Addressing review comments

* Downloading azure and azurerm from blob storage
2021-03-26 18:21:12 +03:00

93 lines
3.1 KiB
PowerShell

################################################################################
## File: Install-AzureModules.ps1
## Desc: Install Azure PowerShell modules
################################################################################
# The correct Modules need to be saved in C:\Modules
$installPSModulePath = $env:PSMODULES_ROOT_FOLDER
if (-not (Test-Path -LiteralPath $installPSModulePath))
{
Write-Host "Creating ${installPSModulePath} folder to store PowerShell Azure modules..."
$null = New-Item -Path $installPSModulePath -ItemType Directory
}
# Get modules content from toolset
$modules = (Get-ToolsetContent).azureModules
$psModuleMachinePath = ""
foreach ($module in $modules)
{
$moduleName = $module.name
Write-Host "Installing ${moduleName} to the ${installPSModulePath} path..."
foreach ($version in $module.versions)
{
$modulePath = Join-Path -Path $installPSModulePath -ChildPath "${moduleName}_${version}"
Write-Host " - $version [$modulePath]"
try
{
Save-Module -Path $modulePath -Name $moduleName -RequiredVersion $version -Force -ErrorAction Stop
}
catch
{
Write-Host "Error: $_"
exit 1
}
}
if($null -ne $module.url)
{
$assets = Invoke-RestMethod $module.url
}
foreach ($version in $module.zip_versions)
{
# Install modules from GH Release
if($null -ne $assets)
{
$asset = $assets | Where-Object version -eq $version `
| Select-Object -ExpandProperty files `
| Select-Object -First 1
Write-Host "Installing $($module.name) $version ..."
if ($null -ne $asset) {
Start-DownloadWithRetry -Url $asset.download_url -Name $asset.filename -DownloadPath $installPSModulePath
} else {
Write-Host "Asset was not found in versions manifest"
exit 1
}
}
# Install modules from vsts blob
else
{
$modulePath = Join-Path -Path $installPSModulePath -ChildPath "${moduleName}_${version}"
$filename = "${moduleName}_${version}.zip"
$download_url = [System.String]::Concat($module.blob_url,$filename)
Write-Host " - $version [$modulePath]"
try
{
Start-DownloadWithRetry -Url $download_url -Name $filename -DownloadPath $installPSModulePath
}
catch
{
Write-Host "Error: $_"
exit 1
}
}
}
# Append default tool version to machine path
if ($null -ne $module.default)
{
$defaultVersion = $module.default
Write-Host "Use ${moduleName} ${defaultVersion} as default version..."
$psModuleMachinePath += "${installPSModulePath}\${moduleName}_${defaultVersion};"
}
}
# Add modules to the PSModulePath
$psModuleMachinePath += $env:PSModulePath
[Environment]::SetEnvironmentVariable("PSModulePath", $psModuleMachinePath, "Machine")
Invoke-PesterTests -TestFile "PowerShellModules" -TestName "AzureModules"