Link to home
Start Free TrialLog in
Avatar of jimmylew52
jimmylew52Flag for United States of America

asked on

Check website for specific string after login using Powershell

After I have used a script to log into a website I need to check the site for a specific string.

Any suggestion on how to do this?
SOLUTION
Avatar of Jason Ryberg
Jason Ryberg

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jimmylew52

ASKER

Created on the screen
I need to check the page I have logged into
gives me an error saying downloadfile has 2 arguments
SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This is the login script. It works fine. From here I want to check to see if the login succeeded or failed. So far  the only way I know of to tell is to look for text on the page after login.

Any suggestions would be appreciated.

# Find the username field and set the value to that of our variable
    $usernameField = $doc.getElementById('userName')
      #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')
      $passwordField.value = $pass
      write-host $pass
    # Find and click the submit button
    $submitButton = $doc.getElementById('login')
      write-host $submitButton
    $submitButton.click()
    # Wait until login is complete
    do {sleep 1} until (-not ($ie.Busy))
This is the problem with your script for me.

$ie.Navigate2('http://something')

I am already logged into the site. I need to search the site  after login.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
$doc.Document.getElementById("LoginNotice").innerHTML


The eliment ID I cannot find. As near as I can tell there is not an eliment ID like on the login page.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You will need to wrap the post-login string in a div with an id attribute, (e.g. <div id="loginstring">login successful</div>) and then get the div by ID using the Powershell method above.

Sorry, went about a mile over my head. I have no idea what it is you are trying to tell me to do.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ID= "tbam"

string I want to find = ActivityMonitor

Would this be correct?

$doc.Document.getElementById("tbam").innerHTML
if ($ie.Document.body.innerText -like 'ActivityMonitor')
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Keeps erroring because of a null value, should this work?

If ($doc.Document.getElementById("tbam").innerText -ne "null")
This does not error but it does not write to the test file either.

If ($doc.Document.getElementById("tbam").innerText -ne "null")

            {$loginSucceeded, $WebSite -join " " | out-file -filepath c:\MonitorFolder\ServersUp.txt -append}
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Here is the line I got the ID from:

<a id="tbam" href="/t2/tbam/activityMonitor.htl?method=showActivityMonitor&screenId=tbam">ActivityMonitor  </a>

Is there maybe a way to download the current page to a file and search the file for test?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
error:

You cannot call a method on a null-valued expression.
At C:\MonitorFolder\test6.ps1:56 char:34
+     If ($doc.Document.getElementById <<<< ('tbam').innerHTML.Contains('Activi
tyMonitor'))
    + CategoryInfo          : InvalidOperation: (getElementById:String) [], Ru
   ntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You cannot call a method on a null-valued expression.
At C:\MonitorFolder\test6.ps1:56 char:34
+     If ($doc.Document.getElementById <<<< ("tbam").textContent.contains("Acti
vityMonitor"))
    + CategoryInfo          : InvalidOperation: (getElementById:String) [], Ru
   ntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ForEach($WebSite in Get-Content C:\MonitorFolder\websites.txt){
$WebSite | % {
 try {$web = New-Object System.Net.WebClient
    $out = $web.DownloadString("$WebSite")}
      catch {$WebSite, $Down -join " " | out-file -filepath c:\MonitorFolder\ServersDown.txt -append
                  break}
   
    write-host $website

# Create the IE com object
$ie = new-object -com InternetExplorer.Application
# Navigate to the login page
$ie.navigate($WebSite)
# Wait for the page to finish loading
do {sleep 1} until (-not ($ie.Busy))
$ie.visible = $true #Uncomment this for debugging
 
# Assign the DOM to the $doc variable
$doc = $ie.document

# Login to the Web Page.
Try {
    # Find the username field and set the value to that of our variable
    $usernameField = $doc.getElementById('userName')
      #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')
      $passwordField.value = $pass
      write-host $pass
    # Find and click the submit button
    $submitButton = $doc.getElementById('login')
      #write-host $submitButton
    $submitButton.click()
    # Wait until login is complete
    do {sleep 1} until (-not ($ie.Busy))
      } catch {$null}
            
      If ($doc.Document.getElementById("tbam").textContent.contains("ActivityMonitor"))      
      

            {$loginSucceeded, $WebSite -join " " | out-file -filepath c:\MonitorFolder\ServersUp.txt -append}
            else
          {$LoginFailed, $WebSite, $Check -join " " | out-file -filepath c:\MonitorFolder\ServersDown.txt -append}
 } #catch {$LoginFailed, $WebSite, $Check -join " " | out-file -filepath c:\MonitorFolder\ServersDown.txt -append}

}
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You cannot call a method on a null-valued expression.
At C:\MonitorFolder\test6.ps1:56 char:54
+     If ($doc.getElementById("tbam").textContent.contains <<<< ("ActivityMonit
or"))
    + CategoryInfo          : InvalidOperation: (contains:String) [], RuntimeE
   xception
    + FullyQualifiedErrorId : InvokeMethodOnNull
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Get-Member : No object has been specified to the get-member cmdlet.
At C:\MonitorFolder\test6.ps1:57 char:34
+     $doc.getElementById("tbam") | gm <<<<
    + CategoryInfo          : CloseError: (:) [Get-Member], InvalidOperationEx
   ception
    + FullyQualifiedErrorId : NoObjectInGetMember,Microsoft.PowerShell.Command
   s.GetMemberCommand
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The term 'ie.Document.getElementById' is not recognized as the name of a cmdlet
, function, script file, or operable program. Check the spelling of the name, o
r if a path was included, verify that the path is correct and try again.
At C:\MonitorFolder\test6.ps1:59 char:32
+     If (ie.Document.getElementById <<<< ("tbam").textContent.contains("Activi
ty"))
    + CategoryInfo          : ObjectNotFound: (ie.Document.getElementById:Stri
   ng) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You cannot call a method on a null-valued expression.
At C:\MonitorFolder\test6.ps1:59 char:62
+     If ($ie.Document.getElementById("tbam").textContent.contains <<<< ("Activ
ityMonitor"))
    + CategoryInfo          : InvalidOperation: (contains:String) [], RuntimeE
   xception
    + FullyQualifiedErrorId : InvokeMethodOnNull
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Yes it is there
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
<html>
<head>
<body marginwidth="2" marginheight="2" leftmargin="2" topmargin="2">
<form id="menuBarForm" method="POST" action="/t2/login/chooser.jsp" name="menuBarForm">
<header id="mast">
<nav id="navbar">
<table>
<tbody>
<tr>
<td colspan="1">
T
<sup>2</sup>
Com
</td>
</tr>
<tr>
<script>
<td width="20px" align="left">
<td>
<a id="tbam" href="/t2/tbam/activityMonitor.htl?method=showActivityMonitor&screenId=tbam">ActivityMonitor  </a>
</td>
<td>
</tr>
</tbody>
</table>
</nav>
</form>
<div class="container">
<script type="text/javascript">
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<p> </p>
<blockquote>Choose an area from the menu bar.</blockquote>
<p> </p>
</div>
<div style="clear:both;"></div>
<div id="footerId">
<script language="JavaScript">
</body>
</html>
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I am not the developer. I can talk to them and see if that can be done.

It is in JAVA.
Expanded the code a little more. It looks like the <script[/> tag gets closed. I'm not a programmer so I an not that sure of what I see.

<html>
<head>
<body marginwidth="2" marginheight="2" leftmargin="2" topmargin="2">
<form id="menuBarForm" method="POST" action="/t2/login/chooser.jsp" name="menuBarForm">
<header id="mast">
<nav id="navbar">
<table>
<tbody>
<tr>
<tr>
<script>
saveUserActivityLog(logincheck,'LogIn','LogedIn','UserLogin Time');
</script>
<td width="20px" align="left">
<div id="left_menu_header">
<img id="btn" class="hideButton" src="/t2/images/nav/2.jpg" value="<" name="btn" title="Hide">
</div>
</td>
<td>
<a id="tbam" href="/t2/tbam/activityMonitor.htl?method=showActivityMonitor&screenId=tbam">ActivityMonitor  </a>
</td>
<td>
<a id="customer" href="/t2/reports/redirect-chooser.jsp?screenId=customer">Reports</a>
</td>
</tr>
</tbody>
</table>
</nav>
</form>
<div class="container">
<div style="clear:both;"></div>
<div id="footerId">
<script language="JavaScript">
</body>
</html>
Here is what I have found out:

$doc = $ie.document has to be in the script or I cannot login
$ie.document is the original page before login and that is why I get null for tbam.

Looks like I need to be able to access the page after login to be able to find tbam and get the script to work.

I tool a try with this but the page downloaded is still the page before login.

$WebSite2 = ($WebSite + "/login.htl?method=showLogin")
      write-host $WebSite2
$ie = new-object -com InternetExplorer.Application
# Navigate to the login page
$ie.navigate($WebSite2)
# Wait for the page to finish loading
do {sleep 1} until (-not ($ie.Busy))
$doc2 = $ie.document
$doc2 | out-file "C:\monitorFolder\test62.txt"
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Website I can post

https://t2libd1i174.systrends.com/t2/login/chooser.jsp

Not sure how much good it will do without login credentials. I cannot post those, might give access to confidential information.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Making some progress.

$source = $ie.Document.documentElement.outerHTML | out-file "C:\monitorFolder\test6.html"

Downloads most of the page.

Trying to get the page searched for text now.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
      If ($result -eq $True) 

Open in new window

is nonsense, because the result of the IF expression again is $true or $false ;-). The correct usage is a simple
      If ($result) 

Open in new window

I don't understand. I posted the solution and tried to award the points to the experts. I am happy with the help they provided. Did I click a wrong button?
Experts may not have provided the final answer but they did keep me encouraged to keep trying.

Thank you so much for all the effort.