Azure / azure-powershell

Microsoft Azure PowerShell
Other
4.26k stars 3.86k forks source link

Missing Install-Module after call to Clear-AzContext #26628

Open onetocny opened 1 week ago

onetocny commented 1 week ago

Description

We are the team that maintains AzurePowerShell task for Azure Pipelines. Under the hood the task is using Az.Accounts module to authenticate against Azure and then calls the client script. Recently we started to see several customers having intermittent issues with missing Install-Module command when trying to use it within the client script of the AzurePowerShell task (see the issue script section).

Install-Module: /home/vsts/work/_temp/7300057a-5dec-4a53-a079-f60c018950c0.ps1:5
Line |
   5 |  Install-Module -Name Az.ResourceGraph -Scope CurrentUser -SkipPublish …
     |  ~~~~~~~~~~~~~~
     | The term 'Install-Module' 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.

The issue occurs in approximately one quarter of all cases on Ubuntu 22.04 (see the environment data table below). We did some investigation and found out that Install-Module command becomes unavailable once the Clear-AzContext is called. Also we noticed that calling Get-Command "Install-Module" -ErrorAction SilentlyContinue mitigates the issue and Install-Module becomes available again. This approach was used as an immediate hotfix.

Issue script & Debug output

# this is done by task
$module = Get-Module -Name "Az.Accounts" -ListAvailable | Sort-Object Version -Descending | Select-Object -First 1
$module = Import-Module -Name $module.Path -Global -PassThru -Force
$null = Clear-AzContext -Scope Process
# client part
Install-Module -Name Az.ResourceGraph -Scope CurrentUser -SkipPublisherCheck -Force

Environment data

Name                           Value
----                           -----
PSVersion                      7.4.6
PSEdition                      Core
GitCommitId                    7.4.6
OS                             Ubuntu 22.04.5 LTS
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Module versions

Name              : Az.Accounts
Path              : /usr/share/az_12.1.0/Az.Accounts/3.0.4/Az.Accounts.psm1
Description       : Microsoft Azure PowerShell - Accounts credential management
                     cmdlets for Azure Resource Manager in Windows PowerShell a
                    nd PowerShell Core.

                    For more information on account credential management, plea
                    se visit the following: https://learn.microsoft.com/powersh
                    ell/azure/authenticate-azureps
Guid              : 17a2feff-488b-47f9-8729-e2cec094624c
Version           : 3.0.4
ModuleBase        : /usr/share/az_12.1.0/Az.Accounts/3.0.4
ModuleType        : Script
PrivateData       : {[PSData, System.Collections.Hashtable]}
AccessMode        : ReadWrite
ExportedAliases   : {[Add-AzAccount, Add-AzAccount], [Get-AzDomain, Get-AzDomai
                    n], [Invoke-AzRest, Invoke-AzRest], [Login-AzAccount, Login
                    -AzAccount]…}
ExportedCmdlets   : {[Add-AzEnvironment, Add-AzEnvironment], [Clear-AzConfig, C
                    lear-AzConfig], [Clear-AzContext, Clear-AzContext], [Clear-
                    AzDefault, Clear-AzDefault]…}
ExportedFunctions : {}
ExportedVariables : {}
NestedModules     : {Microsoft.Azure.PowerShell.Cmdlets.Accounts}

Error output

There are no errors in Az module.

o-l-a-v commented 4 days ago

(I'm not a maintainer, just a curious user.)

On the client part when this happens, what is the output of:

# List paths PowerShell will use to check for loading modules
[System.Environment]::GetEnvironmentVariable('PSModulePath','Process').Split(';')

# Can PowerShellGet and PSResourceGet be found?
Get-Module -ListAvailable -Name 'PowerShellGet', 'Microsoft.PowerShell.PSResourceGet' |
    Sort-Object -Property 'Name', 'Version' |
    Format-List -Property 'Name', 'Version', 'Path'

I'd also recommend using Install-PSResource ( https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.psresourceget/install-psresource ) of PSResourceGet ( https://github.com/PowerShell/PSResourceGet ), as PowerShellGet and PackageManagement has been superseded.

I think PowerShellGet v3 is meant to create backward compatible aliases to PSResourceGet ( PowerShellGet\Install-Module => Microsoft.PowerShell.PSResourceGet\Install-PSResource ), but it does not look finished / still in beta:


Edit: Maybe relevant, seems you combine PSModulePath in function Update-PSModulePathForHostedAgentLinux with :, not semicolon ;? 🤔

VeryEarly commented 2 days ago

@dolauli to reproduce