Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
9.3k views
in Technique[技术] by (71.8m points)

powershell - Auto login script

I am trying to fix a script that automatically opens a page in edge and logs in. i can get it to open the page but the script errors out without entering the login info or directing to the desired final destination. the internal server page uses j_username and j_password to id the location where the credentials are entered. Im fairly new to coding and would like help to understand what im doing wrong and what i could be doing better.

Here is the code:

$ie = New-Object -ComObject 'msedge.Application'
$ie.Visible= $true # Make it visible

start microsoft-edge:http://internal URL to company

$usernmae="user"

$password="password"

While ($ie.Busy -eq $true) {Start-Sleep -Seconds 5;}

$usernamefield = $ie.document.getElementByID('j_username')
$usernamefield.value = $username

$passwordfield = $ie.document.getElementByID('j_password')
$passwordfield.value = $password

$Link = $ie.document.getElementByID('login-submit')
$Link.click()

{Start-Sleep -Seconds 10;}

start microsoft-edge:http://final destination internal URL to company

$ie.Quit()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Your problem was that the $ie object is not connected to the browser when you use start microsoft-edge:http://internal URL to company. Use the Navigate method:

$ie = New-Object -ComObject 'msedge.Application'
$ie.Visible= $true # Make it visible
$ie.Navigate("https://internal-login-page")
Do {sleep 1} While ($ie.Busy)
$usernamefield = $ie.document.getElementByID('j_username')
$usernamefield.value = "user"
$passwordfield = $ie.document.getElementByID('j_password')
$passwordfield.value = "password"
$Link = $ie.document.getElementByID('login-submit')
$Link.click()
Do {sleep 1} While ($ie.Busy)
$ie.Navigate("https://final-page")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...