Azure / Azure/azure-powershell
Missing Install-Module after call to Clear-AzContext
- Dominant language
- C#
- Stars
- 4.8k
- Forks
- 4.3k
- Avg merge
- 3d 14h
- Merged PRs (30d)
- 54
Description
### Description
We are the team that maintains [AzurePowerShell task](https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/reference/azure-powershell-v5?view=azure-pipelines) 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).
```pwsh
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](https://github.com/microsoft/azure-pipelines-tasks/pull/20621).
### Issue script & Debug output
```pwsh
# 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.
Contributor guide
Research direction
Start by reproducing the issue script on Ubuntu 22.04 with PowerShell 7.4.6, focusing on the Az.Accounts Clear-AzContext entry point and the Install-Module command. Trace command availability before and after Clear-AzContext; done means the client script can invoke Install-Module reliably without the Get-Command workaround, with the observed regression covered by an appropriate test.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- azure, powershell
- Domain
- cli, cloud
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 38/100