Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
8.8k 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 Answer

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")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

56.5k users

...