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

rtdh 1 Reputation point
2021-09-17T12:37:15.657+00:00

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);

        runspace.Open();
        Pipeline pipeline = runspace.CreatePipeline();

        string sScriptCommand = "Connect-AzAccount";
        pipeline.Commands.AddScript(sScriptCommand); 

        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.

Thanks
David

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
    2021-09-17T16:52:04.913+00:00

    Hello,

    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:

    https://learn.microsoft.com/en-us/powershell/module/az.accounts/connect-azaccount?view=azps-6.4.0&viewFallbackFrom=azps-5.1.0

    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
    2021-09-20T02:43:08.163+00:00

    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.

    133289-capture.png


    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