How can i execute Powershell Connect-AzAccount in IIS 10 Website ?

rtdh 1 Reputation point

Hi IIS experts. I've created a Blazor Server app that needs to execute azure powershell cmdlets through a c# class, see method below..

public void ConnectAzAccount()

        InitialSessionState initialState = InitialSessionState.CreateDefault();
        initialState.ExecutionPolicy = Microsoft.PowerShell.ExecutionPolicy.Bypass;
        initialState.LanguageMode = PSLanguageMode.FullLanguage;
        initialState.ImportPSModule(new string[] { "Az.Accounts" });
        Runspace runspace = RunspaceFactory.CreateRunspace(initialState);

        Pipeline pipeline = runspace.CreatePipeline();

        string sScriptCommand = "Connect-AzAccount";

        var outputCollection = pipeline.Invoke();


When i execute on my dev VM, using VS2019 & IIS express, the code above when executed will open an authentication page, which i select the azure account to login with and then I can run other cmdlets in my project without an issue.

However., when deploying the project to IIS (using folder based deployment, and setting IIS application pool (".Net CLR Version" = "No managed code") The login window never appears so cannot authenticate to azure powershell.

Any ideas would be greatly appreciated, i have even added the ApplicationPoolIdentity account to the server admin group incase this was permissions related.


Internet Information Services
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,429 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Limitless Technology 39,461 Reputation points


    I recommend that you check the article below and understand how the "Connect-AzAccount" function works, which can help you a lot in this scenario that you face:

    If the answer was helpful, please don't forget to vote positively or accept as an answer, thank you.

    0 comments No comments

  2. Sam Wu-MSFT 7,131 Reputation points Microsoft Vendor

    Hi @okabi-3745

    Please check if you have imported a powershell module into IIS, if not, please try the steps below:

    1. From the Start menu, type in Windows Features, and select Turn Windows features on or off.
    2. In the Windows Features control panel, expand Internet Information Services, expand Web Management Tools, and place checks next to IIS Management Scripts and IIS Management Service.
    3. Click OK to install.


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments