Azure / azure-powershell

Microsoft Azure PowerShell
Other
4.25k stars 3.85k forks source link

Can't Install Az Module in PowerShell Core #20701

Closed brentarias closed 1 year ago

brentarias commented 1 year ago

Description

My Windows 11 laptop has two versions of PowerShell installed:

Following MS instructions, I want to install the Az module in PowerShell 7.3, so I use this command:

Install-Module -Name Az -Scope CurrentUser -Repository PSGallery -Force

This gives me the following error:

Install-Package: The following commands are already available on this system:'Login-AzAccount,Logout-AzAccount,Resolve-Error,Send-Feedback'. This module 'Az.Accounts' may override the existing commands. If you still want to install this module 'Az.Accounts', use -AllowClobber parameter

This error is discussed in this SO post. The solution given there is to remove all the pre-existing Az modules from the Windows PowerShell 5.x installation. This doesn't fit my situation, as (1) I don't have any Az modules currently installed in either version of PowerShell and (2) I'm not interested in modifying my PowerShell 5.x installation.

Digging deeper, from my PowerShell Core (7.3.x) prompt I issue the following command:

PS C:\Users\BArias> Get-Module -ListAvailable Az* | Select-Object Name, Path

Name                                  Path
----                                  ----
Azure                                 C:\Program Files\WindowsPowerShell\Modules\Azure\5.1.2\Azure.psd1
Azure.AnalysisServices                C:\Program Files\WindowsPowerShell\Modules\Azure.AnalysisServices\0.5.0\Azure.An…
Azure.Storage                         C:\Program Files\WindowsPowerShell\Modules\Azure.Storage\4.2.1\Azure.Storage.psd1
AzureRM                               C:\Program Files\WindowsPowerShell\Modules\AzureRM\5.7.0\AzureRM.psd1
AzureRM.AnalysisServices              C:\Program Files\WindowsPowerShell\Modules\AzureRM.AnalysisServices\0.6.6\AzureR…
AzureRM.ApiManagement                 C:\Program Files\WindowsPowerShell\Modules\AzureRM.ApiManagement\5.1.2\AzureRM.A…
AzureRM.ApplicationInsights           C:\Program Files\WindowsPowerShell\Modules\AzureRM.ApplicationInsights\0.1.3\Azu…
AzureRM.Automation                    C:\Program Files\WindowsPowerShell\Modules\AzureRM.Automation\4.3.2\AzureRM.Auto…
AzureRM.Backup                        C:\Program Files\WindowsPowerShell\Modules\AzureRM.Backup\4.0.4\AzureRM.Backup.p…
AzureRM.Batch                         C:\Program Files\WindowsPowerShell\Modules\AzureRM.Batch\4.0.6\AzureRM.Batch.psd1
AzureRM.Billing                       C:\Program Files\WindowsPowerShell\Modules\AzureRM.Billing\0.14.1\AzureRM.Billin…
AzureRM.Cdn                           C:\Program Files\WindowsPowerShell\Modules\AzureRM.Cdn\4.2.2\AzureRM.Cdn.psd1
AzureRM.CognitiveServices             C:\Program Files\WindowsPowerShell\Modules\AzureRM.CognitiveServices\0.9.4\Azure…
AzureRM.Compute                       C:\Program Files\WindowsPowerShell\Modules\AzureRM.Compute\4.6.0\AzureRM.Compute…
AzureRM.Consumption                   C:\Program Files\WindowsPowerShell\Modules\AzureRM.Consumption\0.3.1\AzureRM.Con…
AzureRM.ContainerInstance             C:\Program Files\WindowsPowerShell\Modules\AzureRM.ContainerInstance\0.2.5\Azure…
AzureRM.ContainerRegistry             C:\Program Files\WindowsPowerShell\Modules\AzureRM.ContainerRegistry\1.0.4\Azure…
AzureRM.DataFactories                 C:\Program Files\WindowsPowerShell\Modules\AzureRM.DataFactories\4.2.2\AzureRM.D…
AzureRM.DataFactoryV2                 C:\Program Files\WindowsPowerShell\Modules\AzureRM.DataFactoryV2\0.5.3\AzureRM.D…
AzureRM.DataLakeAnalytics             C:\Program Files\WindowsPowerShell\Modules\AzureRM.DataLakeAnalytics\4.2.3\Azure…
AzureRM.DataLakeStore                 C:\Program Files\WindowsPowerShell\Modules\AzureRM.DataLakeStore\5.2.0\AzureRM.D…
AzureRM.DevTestLabs                   C:\Program Files\WindowsPowerShell\Modules\AzureRM.DevTestLabs\4.0.4\AzureRM.Dev…
AzureRM.Dns                           C:\Program Files\WindowsPowerShell\Modules\AzureRM.Dns\4.1.2\AzureRM.Dns.psd1
AzureRM.EventGrid                     C:\Program Files\WindowsPowerShell\Modules\AzureRM.EventGrid\0.3.2\AzureRM.Event…
AzureRM.EventHub                      C:\Program Files\WindowsPowerShell\Modules\AzureRM.EventHub\0.6.3\AzureRM.EventH…
AzureRM.HDInsight                     C:\Program Files\WindowsPowerShell\Modules\AzureRM.HDInsight\4.1.2\AzureRM.HDIns…
AzureRM.Insights                      C:\Program Files\WindowsPowerShell\Modules\AzureRM.Insights\4.0.4\AzureRM.Insigh…
AzureRM.IotHub                        C:\Program Files\WindowsPowerShell\Modules\AzureRM.IotHub\3.1.2\AzureRM.IotHub.p…
AzureRM.KeyVault                      C:\Program Files\WindowsPowerShell\Modules\AzureRM.KeyVault\4.3.0\AzureRM.KeyVau…
AzureRM.LogicApp                      C:\Program Files\WindowsPowerShell\Modules\AzureRM.LogicApp\4.0.3\AzureRM.LogicA…
AzureRM.MachineLearning               C:\Program Files\WindowsPowerShell\Modules\AzureRM.MachineLearning\0.17.2\AzureR…
AzureRM.MachineLearningCompute        C:\Program Files\WindowsPowerShell\Modules\AzureRM.MachineLearningCompute\0.4.2\…
AzureRM.MarketplaceOrdering           C:\Program Files\WindowsPowerShell\Modules\AzureRM.MarketplaceOrdering\0.2.1\Azu…
AzureRM.Media                         C:\Program Files\WindowsPowerShell\Modules\AzureRM.Media\0.9.2\AzureRM.Media.psd1
AzureRM.Network                       C:\Program Files\WindowsPowerShell\Modules\AzureRM.Network\5.4.2\AzureRM.Network…
AzureRM.NotificationHubs              C:\Program Files\WindowsPowerShell\Modules\AzureRM.NotificationHubs\4.1.1\AzureR…
AzureRM.OperationalInsights           C:\Program Files\WindowsPowerShell\Modules\AzureRM.OperationalInsights\4.3.2\Azu…
AzureRM.PowerBIEmbedded               C:\Program Files\WindowsPowerShell\Modules\AzureRM.PowerBIEmbedded\4.1.4\AzureRM…
AzureRM.Profile                       C:\Program Files\WindowsPowerShell\Modules\AzureRM.Profile\4.6.0\AzureRM.Profile…
AzureRM.RecoveryServices              C:\Program Files\WindowsPowerShell\Modules\AzureRM.RecoveryServices\4.1.2\AzureR…
AzureRM.RecoveryServices.Backup       C:\Program Files\WindowsPowerShell\Modules\AzureRM.RecoveryServices.Backup\4.1.2…
AzureRM.RecoveryServices.SiteRecovery C:\Program Files\WindowsPowerShell\Modules\AzureRM.RecoveryServices.SiteRecovery…
AzureRM.RedisCache                    C:\Program Files\WindowsPowerShell\Modules\AzureRM.RedisCache\4.1.2\AzureRM.Redi…
AzureRM.Relay                         C:\Program Files\WindowsPowerShell\Modules\AzureRM.Relay\0.3.3\AzureRM.Relay.psd1
AzureRM.Resources                     C:\Program Files\WindowsPowerShell\Modules\AzureRM.Resources\5.5.2\AzureRM.Resou…
AzureRM.Scheduler                     C:\Program Files\WindowsPowerShell\Modules\AzureRM.Scheduler\0.16.3\AzureRM.Sche…
AzureRM.ServerManagement              C:\Program Files\WindowsPowerShell\Modules\AzureRM.ServerManagement\4.1.2\AzureR…
AzureRM.ServiceBus                    C:\Program Files\WindowsPowerShell\Modules\AzureRM.ServiceBus\0.6.4\AzureRM.Serv…
AzureRM.ServiceFabric                 C:\Program Files\WindowsPowerShell\Modules\AzureRM.ServiceFabric\0.3.4\AzureRM.S…
AzureRM.SiteRecovery                  C:\Program Files\WindowsPowerShell\Modules\AzureRM.SiteRecovery\5.0.6\AzureRM.Si…
AzureRM.Sql                           C:\Program Files\WindowsPowerShell\Modules\AzureRM.Sql\4.4.0\AzureRM.Sql.psd1
AzureRM.Storage                       C:\Program Files\WindowsPowerShell\Modules\AzureRM.Storage\4.2.3\AzureRM.Storage…
AzureRM.StreamAnalytics               C:\Program Files\WindowsPowerShell\Modules\AzureRM.StreamAnalytics\4.0.4\AzureRM…
AzureRM.Tags                          C:\Program Files\WindowsPowerShell\Modules\AzureRM.Tags\4.0.1\AzureRM.Tags.psd1
AzureRM.TrafficManager                C:\Program Files\WindowsPowerShell\Modules\AzureRM.TrafficManager\4.0.3\AzureRM.…
AzureRM.UsageAggregates               C:\Program Files\WindowsPowerShell\Modules\AzureRM.UsageAggregates\4.0.2\AzureRM…
AzureRM.Websites                      C:\Program Files\WindowsPowerShell\Modules\AzureRM.Websites\4.2.2\AzureRM.Websit…

So it seems my problem is that an AzureRM installation in Windows PowerShell is bleeding over into my PowerShell Core installation. How do I prevent or undo this cross-wise installation, so that I can install the Az module in PowerShell Core?

Here are some additional findings:

# From PowerShell 5.x
PS C:\Users\BArias> Get-InstalledModule -Name AzureRM -AllVersions -OutVariable AzVersions

Version    Name                                Repository           Description
-------    ----                                ----------           -----------
5.7.0      AzureRM                             https://www.power... Azure Resource Manager Module

And then...

# From PowerShell 7.3.x
PS C:\Users\BArias> Get-InstalledModule -Name AzureRM -AllVersions -OutVariable AzVersions
Get-Package: No match was found for the specified search criteria and module names 'AzureRM'.

I get the same "No match" result for -Name Az. In other words, there should be no reason that I need to remove either Az or AzureRM from my PowerShell 7.3.x. However, as previously stated, the AzureRM installation within Windows PowerShell is cross-enabled inside of PowerShell Core as well. Microsoft provides instructions for having AzureRM and AZ co-exist. Those instructions are useless, because they fail to account for the problem I'm presenting.

So this brings me back to square one. What do I need to do to ensure that the AzureRM installation in Windows PowerShell stops impacting PowerShell Core? I want the scenario indicated in MS documentation, where AzureRM and Az can co-exist...by having the former in Windows PowerShell only, and the latter in PowerShell Core only.

For a bit more info, in my PowerShell Core I also issued this command:

PS C:\Users\BArias> Get-Command Login-AzAccount | Select Name, Module

Name            Module
----            ------
Login-AzAccount AzureRM.Profile

PS C:\Users\BArias> Get-InstalledModule -Name AzureRM.Profile -AllVersions -OutVariable AzVersions
Get-Package: No match was found for the specified search criteria and module names 'AzureRM.Profile'.

I just don't get it.

Issue script & Debug output

PS C:\Users\BArias> Install-MOdule -Name Az -Scope CurrentUser -Repository PSGallery -Force
Install-Package: The following commands are already available on this
system:'Login-AzAccount,Logout-AzAccount,Resolve-Error,Send-Feedback'. This module 'Az.Accounts' may override the
existing commands. If you still want to install this module 'Az.Accounts', use -AllowClobber parameter.

Environment data

PS C:\Users\BArias> $PSVersionTable

Name                           Value
----                           -----
PSVersion                      7.3.1
PSEdition                      Core
GitCommitId                    7.3.1
OS                             Microsoft Windows 10.0.22621
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Module versions

PS C:\Users\BArias> Get-Module Az*

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Script     4.6.0                 AzureRM.Profile                     {Add-AzureRmEnvironment, Clear-AzureRmContext, Cl…

Error output

PS C:\Users\BArias> Resolve-AzError
Resolve-AzError: The term 'Resolve-AzError' is not recognized as a name of a cmdlet, function, script file, or executable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
dingmeng-xue commented 1 year ago

@brentarias , Get-InstalledModule has some limitations. You'd better to use [Get-Module -ListAvailable] (https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_modules?view=powershell-7.3#how-to-find-installed-modules) to get module and its path.

You need to uninstall AzureRM from your environment if you want to use Az. If you hope AzureRM is on Windows PowerShell and Az is on PowerShell 7, you need to remove current Az and AzureRm and then install them for user scope https://learn.microsoft.com/en-us/powershell/module/powershellget/install-module?view=powershell-7.3#example-5-install-a-module-only-for-the-current-user

@mikefrobbins FYI. we may need to give more information to user.

ghost commented 1 year ago

Hi @brentarias. Thank you for opening this issue and giving us the opportunity to assist. We believe that this has been addressed. If you feel that further discussion is needed, please add a comment with the text “/unresolve” to remove the “issue-addressed” label and continue the conversation.

brentarias commented 1 year ago

@mikefrobbins Yes, I agree with @dingmeng-xue. The MS documentation makes it clear that to obtain the Az module, which can co-exist with the AzureRM module, you simply install the Az module into PowerShell Core. That is an error by omission. Instead the documentation needs to say that in order for the Az and AzureRM modules to co-exist, it is necessary first to uninstall AzureRM (to erase its "AllUsers" configuration) and then instead re-install it as "CurrentUser" within Windows PowerShell. Finally, after that is fixed, then the Az module can be installed as "CurrentUser" within PowerShell Core. Only then are both able to "co-exist".

mikefrobbins commented 1 year ago

@brentarias I'll move the following note out of the "Installation on Windows PowerShell" section of Install the Azure Az PowerShell module. The note should be in the main part of the installation instructions:

image

The link in that note takes you to the coexistence section of our troubleshooting article that includes the following information:

image

Modules installed in the all users scope of Windows PowerShell bleed over into PowerShell 7.x. This occurs because the $env:PSModulePath in PowerShell 7 contains the Windows PowerShell all users path.

$env:PSModulePath -split [System.IO.Path]::PathSeparator

See the last two entries in the following output:

#Output from PowerShell 7.x on a Windows system
C:\Users\<userid>\Documents\PowerShell\Modules
C:\Program Files\PowerShell\Modules
c:\program files\powershell\7\Modules
C:\Program Files\WindowsPowerShell\Modules
C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules
martinkunc commented 1 year ago

For me, the problem was installed PowerShell Azure (5.7.1) as a .msi package. I have uninstalled PowerShell Azure from Add/Remove programs and then I was able to install new module from cli as above.

eschrader commented 1 year ago

For me, the problem was installed PowerShell Azure (5.7.1) as a .msi package. I have uninstalled PowerShell Azure from Add/Remove programs and then I was able to install new module from cli as above.

Thanks!! This worked for me too! Microsoft Azure PowerShell - April 2018, 5.7.0.18831. Looks like I installed it about 2 years ago with the Storage Emulator and all that, so maybe came from Visual Studio or an Azure EXE.

### SOLUTION! Uninstall Microsoft Azure PowerShell - April 2018 (or a similar conflicting version of powershell) via Add/Remove programs, closed/opened a new admin session of PowerShell and was able to run Install-Module -Name Az. I was thinking this could have been where it was installed but it was under M for Microsoft so it got me!

abdulrafimk commented 1 year ago

Uninstalling Microsoft Azure Powershell 5x version worked for me too.. thanks.

Ninjanaut commented 9 months ago

The documentation is not clear what to do if you need to install PowerShell 7 with Az cmdlet. It only declaratively says some information around. I went step by step and still got the error state even though i read the warning.

First warning says that you cannot have both cmdlets Az and AzureRM installed in Windows PowerShell 5.1 at the same time. If I am installing PowerShell 7 the warning seems to be not relevant, because the important fact that AzureRM installed in Windows PowerShell 5.1 conflicts with the PowerShell7 version and you need to remove AzureRM from Windows PowerShell 5.1 before installing Az into PowerShell7. This is the key information that's completely missing.

I tried to uninstall the AzureRM from the Windows Power Shell with the following command under Admin rights uninstall-module AzureRM

and install Az cmdlet in PowerShell7 Install-Module -Name Az -Repository PSGallery -Force

but this still does not help and the following error remains

Install-Package: The following commands are already available on this
system:'Login-AzAccount,Logout-AzAccount,Resolve-Error,Send-Feedback'. This module 'Az.Accounts' may
override the existing commands. If you still want to install this module 'Az.Accounts', use
-AllowClobber parameter.

I found solution in this article https://basvo.github.io/azure/azurerm/powershell/2021/03/05/remove-azurerm-before-installing-az-module.html and the AzureRM can be uninstalled like that

uninstall-module Azure
uninstall-module AzureRM
$Modules = Get-Module -ListAvailable | Where {$_.Name -like 'AzureRM.*'}
Foreach ($Module in $Modules) {Uninstall-Module $Module}

After that the Az module can be installed without an error.

It would be useful to have any recommended instructions how to solve this situation in the docs.

asztymelski commented 7 months ago

hell yeah! thank you mister Ninjanaut!