0

I have a program that uses the following code (Simplified)

        using (PowerShell powerInstance = PowerShell.Create()) 
        {

            powerInstance.AddScript("Set-ExecutionPolicy Unrestricted");


            powerInstance.AddScript("Connect-AzureRmAccount");
            powerInstance.Invoke();
        }

This works fine when and a login popup is shown to enter credentials.

Now I want this program to run after I publish my Web app to azure to do this I added a post publish event in my seperate web project like so

  <Target Name="PostAzurePublish" AfterTargets="MSDeployPublish">
   <Exec Command="C:\MyProgram.exe"></Exec>  
  </Target>

The issue here is when I publish the my web App the program that runs the powershell commands runs but hangs indefinitely and the Login popup is not shown. So I am assuming it is just waiting for credentials to be supplied but beacuse the login dialog is never shown it hangs.

Is there a simple way I can get the Login dialog to show in this scenario?

user2945722
  • 1,293
  • 1
  • 16
  • 35
  • `Connect-AzureRmAccount` does require credentials, yes. If you want a non-interactive login then you'll have to store credentials as securely as possible with your project and pass them to the -Credentials parameter. The cmdlet does support other ways to authenticate however: https://learn.microsoft.com/en-us/powershell/module/azurerm.profile/connect-azurermaccount?view=azurermps-6.11.0 – Robin Oct 26 '18 at 18:50

1 Answers1

0

You can use Connect-AzureRmAccount PowerShell command without an interactive login. All you have to use is Service Principal for the log-in credentials.

$applicationId = "<service prinicple application id>";
$securePassword = "<service prinicple password>" | ConvertTo-SecureString -AsPlainText -Force
$credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $applicationId, $securePassword
Connect-AzureRmAccount -ServicePrincipal -Credential $credential -TenantId "<your tenantid>"

See my detailed answer here

Jayendran
  • 9,638
  • 8
  • 60
  • 103