Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 10001
  • Last Modified:

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

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
jimmylew52
Asked:
jimmylew52
  • 11
  • 6
1 Solution
 
becraigCommented:
$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
 
jimmylew52Author Commented:
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
 
becraigCommented:
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
Building an Effective Phishing Protection Program

Join Director of Product Management Todd OBoyle on April 26th as he covers the key elements of a phishing protection program. Whether you’re an old hat at phishing education or considering starting a program -- we'll discuss critical components that should be in any program.

 
jimmylew52Author Commented:
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
 
jimmylew52Author Commented:
$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
 
becraigCommented:
What version of IE are you using  ?


This looks like a known issue with version of IE greater than IE8
0
 
jimmylew52Author Commented:
IE 11 - Windows sucks
0
 
jimmylew52Author Commented:
On IE 8 the web page comes up the script runs and still nothing in the web.txt file
0
 
becraigCommented:
0
 
jimmylew52Author Commented:
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
 
becraigCommented:
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
 
jimmylew52Author Commented:
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
 
jimmylew52Author Commented:
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
 
becraigCommented:
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
 
jimmylew52Author Commented:
Runs and gives me a screen shot of the login page but does not appear to login to the website
0
 
jimmylew52Author Commented:
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
 
jimmylew52Author Commented:
the link was not the solution but it did lead me to the solution.

Thanks for the help
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Worried about phishing attacks?

90% of attacks start with a phish. It’s critical that IT admins and MSSPs have the right security in place to protect their end users from these phishing attacks. Check out our latest feature brief for tips and tricks to keep your employees off a hackers line!

  • 11
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now