Running Universal as Azure Web App - "Az and AzureRM modules were detected on this machine"

Product: PowerShell Universal
Version: 3.9.12
Deployment: Azure Web App

I’ve been banging my head to this issue I’m facing with trying to use CosmosDB PSModule within a Universal environment.

As soon as I try to create a new CosmosDB Context, I’m faced with the following warning:

Both Az and AzureRM modules were detected on this machine. 
Az and AzureRM modules cannot be imported in the same session or used in the same script or runbook. 
If you are running PowerShell in an environment you control you can use the 'Uninstall-AzureRm' cmdlet to remove all AzureRm modules from your machine. 
If you are running in Azure Automation, take care that none of your runbooks import both Az and AzureRM modules. More information can be found here: https://aka.ms/azps-migration-guide

Upon further investigation, when I create a new Terminal for any of the Enviromnents (default and custom created ones) and run Get-Module -ListAvailable I find the following:

Directory: C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ResourceManager\AzureResourceManager


ModuleType Version    Name                                ExportedCommands                                              
---------- -------    ----                                ----------------                                              
Manifest   1.1.0      AzureRM.ApiManagement               {Add-AzureRmApiManagementRegion, Get-AzureRmApiManagementSs...
Manifest   1.0.8      AzureRM.Automation                  {Get-AzureRmAutomationJobOutputRecord, Import-AzureRmAutoma...
Binary     0.9.5      AzureRM.AzureStackAdmin             {Get-AzureRMManagedLocation, New-AzureRMManagedLocation, Re...
Manifest   0.9.6      AzureRM.AzureStackStorage           {Add-ACSFarm, Get-ACSEvent, Get-ACSEventQuery, Get-ACSFarm...}
Manifest   1.0.8      AzureRM.Backup                      {Backup-AzureRmBackupItem, Enable-AzureRmBackupContainerRer...
Manifest   1.1.0      AzureRM.Batch                       {Remove-AzureRmBatchAccount, Get-AzureRmBatchAccount, Get-A...
Manifest   1.0.2      AzureRM.Cdn                         {Get-AzureRmCdnCustomDomain, New-AzureRmCdnCustomDomain, Re...
Manifest   1.3.0      AzureRM.Compute                     {Remove-AzureRmAvailabilitySet, Get-AzureRmAvailabilitySet,...
Manifest   1.0.8      AzureRM.DataFactories               {Remove-AzureRmDataFactory, Get-AzureRmDataFactoryRun, Get-...
Manifest   1.1.0      AzureRM.DataLakeAnalytics           {Get-AzureRmDataLakeAnalyticsDataSource, Remove-AzureRmData...
Manifest   1.0.8      AzureRM.DataLakeStore               {Add-AzureRmDataLakeStoreItemContent, Export-AzureRmDataLak...
Manifest   1.0.8      AzureRM.Dns                         {Get-AzureRmDnsRecordSet, Remove-AzureRmDnsRecordSet, Set-A...
Manifest   1.1.0      AzureRM.HDInsight                   {Get-AzureRmHDInsightJob, New-AzureRmHDInsightSqoopJobDefin...
Manifest   1.0.8      AzureRM.Insights                    {Add-AzureRmMetricAlertRule, Add-AzureRmLogAlertRule, Add-A...
Manifest   1.1.7      AzureRM.KeyVault                    {Get-AzureRmKeyVault, New-AzureRmKeyVault, Remove-AzureRmKe...
Manifest   1.0.4      AzureRM.LogicApp                    {Stop-AzureRmLogicAppRun, Get-AzureRmLogicApp, Get-AzureRmL...
Manifest   1.0.9      AzureRM.Network                     {Add-AzureRmApplicationGatewayBackendAddressPool, Get-Azure...
Manifest   1.0.8      AzureRM.NotificationHubs            {Get-AzureRmNotificationHubsNamespaceAuthorizationRules, Ge...
Manifest   1.0.8      AzureRM.OperationalInsights         {Get-AzureRmOperationalInsightsSavedSearch, Get-AzureRmOper...
Manifest   1.0.8      AzureRM.Profile                     {Enable-AzureRmDataCollection, Disable-AzureRmDataCollectio...
Manifest   1.1.0      AzureRM.RecoveryServices            {Get-AzureRmRecoveryServicesBackupProperties, Get-AzureRmRe...
Manifest   1.0.0      AzureRM.RecoveryServices.Backup     {Backup-AzureRmRecoveryServicesBackupItem, Get-AzureRmRecov...
Manifest   1.1.6      AzureRM.RedisCache                  {Remove-AzureRmRedisCacheDiagnostics, Set-AzureRmRedisCache...
Manifest   1.1.0      AzureRM.Resources                   {Get-AzureRmADApplication, Get-AzureRmADGroupMember, Get-Az...
Manifest   1.1.7      AzureRM.SiteRecovery                {Stop-AzureRmSiteRecoveryJob, Get-AzureRmSiteRecoveryNetwor...
Manifest   1.0.8      AzureRM.Sql                         {Get-AzureRmSqlDatabaseImportExportStatus, New-AzureRmSqlDa...
Manifest   1.1.0      AzureRM.Storage                     {Get-AzureRmStorageAccount, Get-AzureRmStorageAccountKey, G...
Manifest   1.0.8      AzureRM.StreamAnalytics             {Get-AzureRmStreamAnalyticsFunction, Get-AzureRmStreamAnaly...
Manifest   1.0.8      AzureRM.Tags                        {Remove-AzureRmTag, Get-AzureRmTag, New-AzureRmTag}           
Manifest   1.0.8      AzureRM.TrafficManager              {Disable-AzureRmTrafficManagerEndpoint, Enable-AzureRmTraff...
Manifest   1.0.8      AzureRM.UsageAggregates             Get-UsageAggregates                                           
Manifest   1.1.0      AzureRM.Websites                    {Get-AzureRmAppServicePlanMetrics, New-AzureRmWebAppDatabas...

I have no idea why these are installed and am at a loss as to how to get rid of them, or if I’m even able to get rid of them without facing issues afterwards…

Not really a Universal issue, but I was hoping someone could help me out with this.

Is it possible to remove the AzureRM module from the $ENV:PSModulePath to prevent it from auto-loading?

C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ResourceManager\AzureResourceManager

You could try both process level and user level environment variables. Idk if you’ll be able to edit the machine level env vars from a web app context.

As far as I’m aware, it’s not possible to change that (or any) environment variables for a Static Web App.

Removing the AzureRM modules through the suggested Uninstall-AzureRM unfortunately does not work either.

I’m not sure what the best way would be to achieve this.

Perhaps I could use the Kudu debug console to try and rewrite the $env:PSModulePath value?
And if not through Kudu, maybe the Azure console itself.

something like:

[System.Environment]::SetEnvironmentVariable("PSModulePath","C:\Program Files\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules;C:\Program Files\WindowsPowerShell\Modules\;C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\ServiceManagement\;C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\Storage\;C:\Program Files\Microsoft Message Analyzer\PowerShell\","Machine")

Should do the trick, I think?

Ok, above idea does not work.
I did however find something interesting;

Within the location; C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell
There is a .ps1 file named RemoveGalleryModules.ps1 with the following content;

function Remove-Modules
{
  param([string]$basePath)
  $paths = "Azure", "AzureRM", "AzureRM.*", "Azure.Storage"
  $paths | ForEach-Object {
    $modulePath = ([System.IO.Path]::Combine($basePath, $_))
    try {
      Write-Host Removing $_
      if (Test-Path $modulePath)
      {
        Remove-Item -Recurse $modulePath -Force
      }
    }
    catch {}
  }
}

if (Test-Path 'Env:\ProgramFiles(x86)')
{
   Remove-Modules ([System.IO.Path]::Combine(${env:ProgramFiles(x86)}, "WindowsPowerShell", "Modules"))
}

if (Test-Path Env:\ProgramFiles)
{
   Remove-Modules ([System.IO.Path]::Combine($env:ProgramFiles, "WindowsPowerShell", "Modules"))
}

Looks like I can simply run that and be done.
I’ll try to find some more info on it for a bit but probably will just run it and hope for the best.

Yeah nah. Wishfull thinking…

I’ve ended up just adding this to the top of my scripts that leverage the CosmosDB module;

$env:PSModulePath = '\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules;C:\Program Files\PowerShell\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\home\site\wwwroot\Modules;D:\home\data\UniversalAutomation\Repository\Modules'

Will add it as a startup script for the new Environment I’ll end up running it all in.

Sometimes it’s not as complicated as initially thought… lol.

1 Like