'Can we execute az PowerShell module commands inside of a runspace in a .net console app deployed as a webjob on Azure app service?

I've a webjob running in an azure app service that uses runspace to execute azure powershell module commands. Locally it's working fine because I can install az module directly using Windows powershell but when I deploy it to Azure, it fails.

Initially I was getting following error when I was executing Connect-AzAccount -Identity

Connect-AzAccount : The term 'Connect-AzAccount' is not recognized as the name of a cmdlet

Then I tried to install az module inside of a runspace and I started getting another error:

Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

Here is how I'm trying to install az module:

    runSpace.Open();

    RunspaceInvoke scriptInvoker = new RunspaceInvoke();
    scriptInvoker.Invoke("Install-PackageProvider Nuget -MinimumVersion 2.8.5.201 -Force -Scope CurrentUser");
    scriptInvoker.Invoke("Import-PackageProvider Nuget");
    scriptInvoker.Invoke("Install-Module Az.Websites -Force -AllowClobber -Scope CurrentUser");
    scriptInvoker.Invoke("Import-Module Az.Websites");
    scriptInvoker.Invoke("Install-Module Az.Storage -Force -AllowClobber -Scope CurrentUser");
    scriptInvoker.Invoke("Import-Module Az.Storage");

So is it possible to execute az powershell module command in appservice?



Solution 1:[1]

  • There are multiple ways to address the issue, below are the few resolutions.

Fix 1: To resolve the issue run the below command.

Import-Module Az.Accounts

When you executed the above command, it will prompt you for the credentials.

Fix 2:

  • Install the Module Az. By using the below PowerShell cmdlet,
Import-Module Az
  • Connect to the AzAccount like below,
Connect-AzAccount
  • You can check the worked solution from here.

  • Also check this SO with the related discussions.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 RajkumarPalnati-MT