Utility to help managed conflicting PowerShell Modules for Azure, Azure Stack Hub and Azure Stack Edge
If you manage Azure, Azure Stack Hub and Azure Stack Edge, you probably run in to module conflicts due API differences. This is how I manage all of these without removing modules and manually making changes to module paths.
Run the following in an elevated PowerShell window to setup your environment.
# URI of the Edge Diagnostics Zip
$DownloadURI = 'https://github.com/RichShillingtonMSFT/PowerShell-Module-Switcher/archive/refs/heads/main.zip'
# Download the ZIP file
Invoke-WebRequest -Uri $DownloadURI -UseBasicParsing -OutFile "$env:TEMP\PowerShell-Module-Switcher.zip"
$Directories = @(
"$env:USERPROFILE\Documents\PowerShellFunctions"
'C:\PowerShellModules'
)
foreach ($Directory in $Directories)
{
# Check for the log directory and create it if it does not exist
$PathTest = Test-Path $Directory
if (!($PathTest))
{
New-Item -Path $Directory -ItemType Directory
}
}
# Expand the ZIP file that was downloaded
Expand-Archive "$env:TEMP\PowerShell-Module-Switcher.zip" -DestinationPath "$env:TEMP" -Force
# Unblock each file that was extracted
$Items = Get-ChildItem "$env:TEMP\PowerShell-Module-Switcher-main" -Recurse -Verbose
foreach ($Item in $Items)
{
Unblock-File -Path $Item.FullName -Verbose
}
# Copy all the files to the Kusto Path
Copy-Item -Path "$env:TEMP\PowerShell-Module-Switcher-main\Load-PSModules.ps1" -Destination "$env:USERPROFILE\Documents\PowerShellFunctions" -Recurse -Verbose -Force
Copy-Item -Path "$env:TEMP\PowerShell-Module-Switcher-main\profile.ps1" -Destination "$Env:SystemRoot\System32\WindowsPowerShell\v1.0" -Recurse -Verbose -Force
# Extract PowerShell Modules
$ModuleFiles = @(
'ASE-Az.zip',
'Az.zip',
'AzureRM.zip',
'Hub-Az.zip',
'Hub-AzureRM.zip'
)
foreach ($ModuleFile in $ModuleFiles)
{
Expand-Archive "$env:TEMP\PowerShell-Module-Switcher-main\Modules\$ModuleFile" -DestinationPath "C:\PowerShellModules" -Force
}
# Unblock each file that was extracted
$Items = Get-ChildItem "C:\PowerShellModules" -Recurse -Verbose
foreach ($Item in $Items)
{
Unblock-File -Path $Item.FullName -Verbose
}
# Clean up the download and temp files
Remove-Item -Path "$env:TEMP\PowerShell-Module-Switcher-main" -Force -Recurse
Remove-Item -Path "$env:TEMP\PowerShell-Module-Switcher.zip" -Force
Exit
#
The above code performs the following:
- Create a folder called PowerShellFunctions in your Documents folder and copy Load-PSModules.ps1 to it.
- Copy profile.ps1 to C:\Windows\System32\WindowsPowerShell\v1.0.
- On the C: drive, create a folder called PowerShellModules.
- In C:\PowerShellModules create subfolders for each module type. ASE-Az, Az, AzureRM, Hub-Az, Hub-AzureRM.
- Download and extract the PowerShell modules for each type to the appropriate folder.
To set your module directories, you can run the following:
Set PowerShell to use Azure Stack Edge Az Modules
Load-PSModules -ModuleType ASE-Az
Set PowerShell to use Azure Az Modules
Load-PSModules -ModuleType Az
Set PowerShell to use Azure RM Modules
Load-PSModules -ModuleType AzureRM
Set PowerShell to use Azure Stack Hub AzureRM Modules
Load-PSModules -ModuleType Hub-AzureRM
Set PowerShell to use Azure Stack Hub Az Modules
Load-PSModules -ModuleType Hub-Az
Reset PowerShell Module Directories to Default
Load-PSModules -ModuleType Default