Az Storage module is not installing via windows extension.

Aravind R 6 Reputation points
2023-03-14T07:45:52.71+00:00

Hi,

I have a main script that has a component to get the artifacts from the storage account. As a pre-requisite, I am installing Az Storage and Az Accounts module as mentioned below,

Install-Module -Name Az.Accounts -Repository PSGallery -AllowClobber -Force

Install-Module -Name Az.Storage -Repository PSGallery -AllowClobber -Force

My script uses "New-AzStorageContext" and "Get-AzStorageBlob" commands as below to get the artifacts from storage account.
$account = New-AzStorageContext -ConnectionString $connection_string

$blobs = Get-AzStorageBlob -Container $container_name -Context $account

On manual execution, there is no error in the script and the artifacts are downloaded into the Azure VM. But, In the Terraform provision with the windows extension. I could see the above module got installed but the "New-AzStorageContext" and "Get-AzStorageBlob" commands are not recognized and throwing error as below,

The term 'New-AzStorageContext' 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.
The term 'Get-AzStorageBlob' 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.
Azure Storage
Azure Storage
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
3,529 questions
Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
3,192 questions
Windows for business Windows Server User experience PowerShell
{count} vote

2 answers

Sort by: Most helpful
  1. Limitless Technology 44,746 Reputation points
    2023-03-14T14:35:30.53+00:00

    Hello there,

    Check the below points and make sure that it has been done.

    PowerShell script execution policy must be set to remote signed or less restrictive. Get-ExecutionPolicy -List can be used to determine the current execution policy.

    Azure PowerShell has no additional requirements when run on PowerShell 7.0.6 LTS and PowerShell 7.1.3 or higher.

    Install the latest version of PowerShell available for your operating system.

    To determine your PowerShell version, run the following command from within a PowerShell session:

    $PSVersionTable.PSVersion

    Hope this resolves your Query !!

    --If the reply is helpful, please Upvote and Accept it as an answer--

    0 comments No comments

  2. Sumarigo-MSFT 47,466 Reputation points Microsoft Employee Moderator
    2023-03-15T10:13:31.6933333+00:00

    @Aravind R Welcome to Microsoft Q&A Forum, Thank you for posting your query here!

    Sometimes this error may occur due to security policy, try to run the below cmdlets You could run set-executionpolicy remotesigned or Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned as administrator and let me know the status.

    This error suggests that the cmdlets "New-AzStorageContext" and "Get-AzStorageBlob" are not available on the system where Terraform is running.

    To use these cmdlets, you need to have the "Az.Storage" PowerShell module installed. You can install this module by running the following command in PowerShell:

    Install-Module -Name Az.Storage -AllowClobber
    

    Once the module is installed, you can import it in your Terraform provisioner using the Import-Module cmdlet. For example, you can add the following code to import the module before using the cmdlets:

    provisioner "powershell" {
      inline = [
        "Import-Module Az.Storage",
        "New-AzStorageContext -ConnectionString 'your_storage_account_connection_string'",
        "Get-AzStorageBlob -Context $ctx -Container 'your_container_name' -Blob 'your_blob_name'"
      ]
    }
    
    

    Make sure to replace the "your_storage_account_connection_string", "your_container_name", and "your_blob_name" placeholders with the appropriate values for your scenario.

    Install Terraform on Windows with Azure PowerShell

    The New-AzStorageContext cmdlet creates an Azure Storage context. The default Authentication of a Storage Context is OAuth (Azure AD), if only input Storage account name. See details of authentication of the Storage Service in https://learn.microsoft.com/rest/api/storageservices/authorization-for-the-azure-storage-services.

    Please let us know if you have any further queries. I’m happy to assist you further.   


    Please do not forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.