Usually one can use Add-AzureRmAccount to login to PowerShell, but this must be done every time a new PS instance is started and the user needs to enter the password. Some company requires a two-factor authentication, like smart card or phone call. This is not suitable for an automatated execution, like Task Scheduler.
We can use Service Principal to automate this.
In Portal Azure Portal, create a Service Principal.
$user = "<Application ID>"
$password = ConvertTo-SecureString -String "<key>" -AsPlainText -Force
$Credential = New-Object -TypeName "System.Management.Automation.PSCredential" -ArgumentList $user, $password
Connect-AzureRmAccount -ServicePrincipal -Credential $Credential -Tenant "72f988bf-86f1-41af-91ab-2d7cd011db47"
Put Application ID, Key and Tenant ID in the PowerShell script.
Get-Credential shows how to create Credential
without prompting the user with New-Object
.