Solved

Log into website using powershell and output "success" for the login

Posted on 2014-03-03
17
6,384 Views
Last Modified: 2014-03-04
I want to log into a website using a username and password and report success or failure of the login.


I have found some sample scripts, they login but do not report anything.

Any suggestions?
0
Comment
Question by:jimmylew52
  • 11
  • 6
17 Comments
 
LVL 28

Expert Comment

by:becraig
ID: 39902116
$username = "username" 
$password = "password" 
$ie = New-Object -com InternetExplorer.Application 
$ie.visible=$false
$ie.navigate("Url") 
while($ie.ReadyState -ne 4) {start-sleep -m 100} 
$ie.document.getElementById("username").value= "$username" 
$ie.document.getElementById("pass").value = "$password" 
$ie.document.getElementById("loginform").submit()
start-sleep 20 
$ie.Document.body | Out-File -FilePath c:\web.txt 

Open in new window


The above script should work.
Be sure to map the values to match the values on the website.
You can parse $ie.Document.body | for any specific response you are expecting.


http://social.technet.microsoft.com/Forums/windowsserver/en-US/be3afe83-4a7e-48a0-b2e7-95fd081a7571/login-to-website-using-powershell?forum=winserverpowershell
0
 
LVL 1

Author Comment

by:jimmylew52
ID: 39902340
I tried this:

#$username = "XXXXX"
#$password = "XXXXXXX"
#$ie = New-Object -com InternetExplorer.Application
#$ie.visible=$false
#$ie.navigate("https://yippi.mycompany.com")
#while($ie.ReadyState -ne 4) {start-sleep -m 100}
#$ie.document.getElementById("User ID").value= "$username"
#$ie.document.getElementById("Password").value = "$password"
#$ie.document.getElementById("frmLogin").submit()
#start-sleep 20
#$ie.Document.body | Out-File -FilePath c:\monitorFolder\web.txt

Web page asks for User ID and Password.

The script runs but I cannot tell it logs into anything. Isn't something supposed to be added to the web.txt file?
0
 
LVL 28

Expert Comment

by:becraig
ID: 39902355
This seems to work for me and outputs the data to the web.txt.

Try changing
$ie.visible=$false
to
$ie.visible=$true

This will let you see what IE is doing.

Also I see you have all the lines commented out in your script ?
Are the # signs at the beginning of your lines here because you are posting it  ?

If you leave them there when you run your script it wont do anything.
0
 
LVL 1

Author Comment

by:jimmylew52
ID: 39902365
I get something now, no I did not have the # in front of the lines.

Cannot find an overload for "getElementById" and the argument count: "1".
At C:\MonitorFolder\test.ps1:21 char:1
+ $ie.document.getElementById("User ID").value= "$username"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodException
    + FullyQualifiedErrorId : MethodCountCouldNotFindBest
0
 
LVL 1

Author Comment

by:jimmylew52
ID: 39902367
$username = "XXXX"
$password = "XXXXXXXX"
$ie = New-Object -com InternetExplorer.Application
$ie.visible=$true
$ie.navigate("https://yippi.company.com/t2")
while($ie.ReadyState -ne 4) {start-sleep -m 100}
$ie.document.getElementById("User ID").value= "$username"
$ie.document.getElementById("Password").value = "$password"
$ie.document.getElementById("Loginform").submit()
start-sleep 20
$ie.Document.body | Out-File -FilePath c:\monitorFolder\web.txt
0
 
LVL 28

Expert Comment

by:becraig
ID: 39902372
What version of IE are you using  ?


This looks like a known issue with version of IE greater than IE8
0
 
LVL 1

Author Comment

by:jimmylew52
ID: 39902374
IE 11 - Windows sucks
0
 
LVL 1

Author Comment

by:jimmylew52
ID: 39902387
On IE 8 the web page comes up the script runs and still nothing in the web.txt file
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 28

Accepted Solution

by:
becraig earned 500 total points
ID: 39902388
0
 
LVL 1

Author Comment

by:jimmylew52
ID: 39902389
some change User ID = Login

$username = "XXXX"
$password = "XXXXXXXX"
$ie = New-Object -com InternetExplorer.Application
$ie.visible=$true
$ie.navigate("https://yippi.company.com")
while($ie.ReadyState -ne 4) {start-sleep -m 100}
$ie.document.getElementById("Login").value= "$username"
$ie.document.getElementById("Password").value = "$password"
$ie.document.getElementById("Loginform").submit()
start-sleep 20
$ie.Document.body | Out-File -FilePath c:\monitorFolder\web.txt
0
 
LVL 28

Expert Comment

by:becraig
ID: 39902391
Add the following line to the bottom of the script so we can see what the output is:


write-host "This is the site response `n" $ie.Document.body

Open in new window

0
 
LVL 1

Author Comment

by:jimmylew52
ID: 39902398
This is the site response





nothing else

Tried the other script also works I guess, gives me an ok but I need to determine that I can log into the site.
0
 
LVL 1

Author Comment

by:jimmylew52
ID: 39902401
found this script tat works great, requires powershell 3 but gives a great report.

Only problem is, no login and I am not good enough to make that change to it.

## The URI list to test
$URLListFile = "C:\MonitorFolder\WebSites.txt"  
$URLList = Get-Content $URLListFile -ErrorAction SilentlyContinue
  $Result = @()
   
   
  Foreach($Uri in $URLList) {
  $time = try{
  $request = $null
   ## Request the URI, and measure how long the response took.
  $result1 = Measure-Command { $request = Invoke-WebRequest -Uri $uri }
  $result1.TotalMilliseconds
  }  
  catch
  {
   <# If the request generated an exception (i.e.: 500 server
   error or 404 not found), we can pull the status code from the
   Exception.Response property #>
   $request = $_.Exception.Response
   $time = -1
  }  
  $result += [PSCustomObject] @{
  Time = Get-Date;
  Uri = $uri;
  StatusCode = [int] $request.StatusCode;
  StatusDescription = $request.StatusDescription;
  ResponseLength = $request.RawContentLength;
  TimeTaken =  $time;  
  }
 
}
    #Prepare email body in HTML format
if($result -ne $null)
{
    $Outputreport = "<HTML><TITLE>Website Availability Report</TITLE><BODY background-color:peachpuff><font color =""#99000"" face=""Microsoft Tai le""><H2> Website Availability Report </H2></font><Table border=1 cellpadding=0 cellspacing=0><TR bgcolor=gray align=center><TD><B>URL</B></TD><TD><B>StatusCode</B></TD><TD><B>StatusDescription</B></TD><TD><B>ResponseLength</B></TD><TD><B>TimeTaken</B></TD</TR>"
    Foreach($Entry in $Result)
    {
        if($Entry.StatusCode -ne "200")
        {
            $Outputreport += "<TR bgcolor=red>"
        }
        else
        {
            $Outputreport += "<TR>"
        }
        $Outputreport += "<TD>$($Entry.uri)</TD><TD align=center>$($Entry.StatusCode)</TD><TD align=center>$($Entry.StatusDescription)</TD><TD align=center>$($Entry.ResponseLength)</TD><TD align=center>$($Entry.timetaken)</TD></TR>"
    }
    $Outputreport += "</Table></BODY></HTML>"
}
 
$Outputreport | out-file C:\MonitorFolder\ProductionServers.htm
0
 
LVL 28

Expert Comment

by:becraig
ID: 39902433
If the link I sent you works for you, then just add:

$http_request.responsetext | out-string

$url = "https://login.yahoo.com/config/login?"
$parameters = "username=true&passwd=truer"	# your POST parameters

$http_request = New-Object -ComObject Msxml2.XMLHTTP
$http_request.open('POST', $url, $false)
$http_request.setRequestHeader("Content-type",
"application/x-www-form-urlencoded")
$http_request.setRequestHeader("Content-length", $parameters.length)
$http_request.setRequestHeader("Connection", "close")
$http_request.send($parameters)
$http_request.statusText
$Outputreport = $http_request.responsetext | out-string

$Outputreport | out-file C:\MonitorFolder\ProductionServers.htm 

Open in new window

0
 
LVL 1

Author Comment

by:jimmylew52
ID: 39903044
Runs and gives me a screen shot of the login page but does not appear to login to the website
0
 
LVL 1

Author Comment

by:jimmylew52
ID: 39904570
Here is what worked:

$username = "admin "
$pass = "XXXXXXX"
#write-host $pass
# Create the IE com object
$ie = new-object -com InternetExplorer.Application
# Navigate to the login page
$ie.navigate("https://page.company.com")
# Wait for the page to finish loading
do {sleep 1} until (-not ($ie.Busy))
$ie.visible = $true #comment this line after debugging
 
# Assign the DOM to the $doc variable
$doc = $ie.document
# Our login page remembers credentials for an hour even after
# closing a browser, so we're going to enclose the login part
# in a try-catch block, as these fields may not exist if we've already run this.
try {
    # Find the username field and set the value to that of our variable
    $usernameField = $doc.getElementById('userName')  # 'userName' Has to be the name called in the web page
      #write-host $usernameField
    $usernameField.value = $username
      write-host $username
    # Find the password field and set the value to that of the result
    # of a call to the get-password function with the paramter defined at top
    $passwordField = $doc.getElementById('password')   # 'password' Has to be the name called in the web page
      $passwordField.value = $pass
      write-host $pass
    # Find and click the submit button
    $submitButton = $doc.getElementById('login')    # 'login' Has to be the name called in the web page for the login button
      write-host $submitButton
    $submitButton.click()
    # Wait until login is complete
    do {sleep 1} until (-not ($ie.Busy))
} catch {$null}
 
# Find the hyperlink we want to click by enumerating all <a href> tags matching on classname
#$link = $doc.getelementsbytagname('a') | where{$_.className -eq 'linkLarge linkLargeColor'}
# Click the link
#$link.click()
# WAit for page to finish loading
do {sleep 1} until (-not ($ie.Busy))

All this does is login to the web page nothing more. On to the next step
0
 
LVL 1

Author Closing Comment

by:jimmylew52
ID: 39904571
the link was not the solution but it did lead me to the solution.

Thanks for the help
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Set OWA language and time zone in Exchange for individuals, all users or per database.
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now