question

rtdh avatar image
0 Votes"
rtdh asked SamWu-MSFT answered

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

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


windows-server-powershellwindows-server-iis
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

LimitlessTechnology-2700 avatar image
0 Votes"
LimitlessTechnology-2700 answered

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://docs.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.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

SamWu-MSFT avatar image
0 Votes"
SamWu-MSFT answered

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.



capture.png (15.8 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.