A while ago a customer contacted me with the request to write a script that programmatically logged on to the Azure portal, through a web-browser.
1 2 |
$SecPwd = ConvertTo-SecureString -String 'MyPassword' -AsPlainText -Force $Cred = [pscredential]::new('user@domain.com',$SecPwd) |
First, you’ll need some credentials. In this case I’ve created them quick and dirty but I recommend  you don’t put credentials plain text in a script 😉
Next up would be actually logging on:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
$username = $Cred.GetNetworkCredential().UserName $password = $Cred.GetNetworkCredential().Password $url = 'http://portal.azure.com' $ie = New-Object -com InternetExplorer.Application $ie.visible=$true $ie.navigate($Url) start-sleep -Seconds 3 $ie.Document.IHTMLDocument3_getElementById("cred_userid_inputtext").focus() $ie.Document.IHTMLDocument2_activeElement.value = $username $ie.Document.IHTMLDocument3_getElementById("cred_userid_inputtext").focus() $ie.Document.IHTMLDocument3_getElementById("cred_password_inputtext").click() start-sleep -Seconds 3 $ie.Document.IHTMLDocument3_getElementById("cred_password_inputtext").focus() $ie.Document.IHTMLDocument2_activeElement.value = $password $ie.Document.IHTMLDocument3_getElementById("cred_userid_inputtext").click() $ie.Document.IHTMLDocument3_getElementById("cred_sign_in_button").focus() $ie.Document.IHTMLDocument3_getElementById("cred_sign_in_button").click() |
After some rigorous testing I found out that it works… once. After that a cookie is saved and the script will fail.
So a quick workaround in my case was to delete the cookie(s) andthe script ran again 🙂
P.S. Yes, I know… Internet Explorer, and through a COM port. If you can improve by using another browser, please do so and post underneath this post! I would love to learn from your code 😀
[…] I posted about ‘Automatically logon to the Azure portal through PowerShell and a browser‘. Well, this works for Office365 too! Simlpy replace the URL in the post with […]
[…] I posted about ‘Automatically logon to the Azure portal through PowerShell and a browser‘. Well, this works for Office365 too! Simlpy replace the URL in the post with […]