1
votes

I am working on one automated process to upload a file using azure CLI (az storage blob upload) to Azure blob and it is working fine as expected.

  • Azure CLI version: 2.17.1
  • PoweShell version: PSVersion 5.1.19041.610

When the user run’s powershell the script for the first time if there is no azure CLI installed on the user machine. we are am installing(find below code). But the requirement here is after installing azure CLI from PowerShell when we execute "az login" we are getting below error

az : The term 'az' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is 
correct and try again.
At C:\Users\v-moush\Desktop\PowerShellerrorScript.ps1:13 char:1
+ az login
+ ~~
    + CategoryInfo          : ObjectNotFound: (az:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
Function Set-AzModule(){
    $isAzureCliInstalled = Azure-CLI-Installed
    if (!$isAzureCliInstalled) {
        Write-Log “Trying to install Microsoft Azure CLI”
        Invoke-WebRequest -Uri https://aka.ms/installazurecliwindows -OutFile .\AzureCLI.msi; Start-Process msiexec.exe -Wait -ArgumentList ‘/I AzureCLI.msi /quiet’; rm .\AzureCLI.msi
        $isAzureCliInstalled = Azure-CLI-Installed;
    
        if (!$isAzureCliInstalled) {
            Write-Log -message “Unable to install Microsoft Azure CLI”
        }
        else {
            Write-Log -message “Microsoft Azure CLI installed successfully”
            az login
            ## Here the command is failing power shell not able to find the az module
        }
    }
}

Function Azure-CLI-Installed()
{
    # Check Microsoft Azure CLI is installed or not.
    return (Get-ItemProperty HKLM:\Software\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\* |
    Where { $_.DisplayName -eq “Microsoft Azure CLI” }) -ne $null
}

Query: How to execute the az login and other az commands after installing Azure CLI in the same script without restarting the Powershell script?

Note: I cannot use azCopy module.

2

2 Answers

1
votes

Fixed the issue with the following command

 $env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User") 

Reference : Reload the path in PowerShell

0
votes

I'm afraid you may have to restart your shell in order for changes to take effect.

Whatever official documents I've been through, described that you will need to reopen PowerShell to use the Azure CLI.

It's a little bit wired that you should install Azure CLI every time when running the scripts. My suggestion is install one time, and restart you power shell to run your scripts.