jimmylew52
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?
Any suggestion on how to do this?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I need to check the page I have logged into
ASKER
gives me an error saying downloadfile has 2 arguments
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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('userN ame')
#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('passw ord')
$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))
Any suggestions would be appreciated.
# Find the username field and set the value to that of our variable
$usernameField = $doc.getElementById('userN
#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('passw
$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))
ASKER
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.
$ie.Navigate2('http://something')
I am already logged into the site. I need to search the site after login.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
$doc.Document.getElementBy Id("LoginN otice").in nerHTML
The eliment ID I cannot find. As near as I can tell there is not an eliment ID like on the login page.
The eliment ID I cannot find. As near as I can tell there is not an eliment ID like on the login page.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
Sorry, went about a mile over my head. I have no idea what it is you are trying to tell me to do.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ID= "tbam"
string I want to find = ActivityMonitor
Would this be correct?
$doc.Document.getElementBy Id("tbam") .innerHTML
if ($ie.Document.body.innerTe xt -like 'ActivityMonitor')
string I want to find = ActivityMonitor
Would this be correct?
$doc.Document.getElementBy
if ($ie.Document.body.innerTe
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Keeps erroring because of a null value, should this work?
If ($doc.Document.getElementB yId("tbam" ).innerTex t -ne "null")
If ($doc.Document.getElementB
ASKER
This does not error but it does not write to the test file either.
If ($doc.Document.getElementB yId("tbam" ).innerTex t -ne "null")
{$loginSucceeded, $WebSite -join " " | out-file -filepath c:\MonitorFolder\ServersUp .txt -append}
If ($doc.Document.getElementB
{$loginSucceeded, $WebSite -join " " | out-file -filepath c:\MonitorFolder\ServersUp
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Here is the line I got the ID from:
<a id="tbam" href="/t2/tbam/activityMon itor.htl?m ethod=show ActivityMo nitor&scre enId=tbam" >ActivityM onitor </a>
Is there maybe a way to download the current page to a file and search the file for test?
<a id="tbam" href="/t2/tbam/activityMon
Is there maybe a way to download the current page to a file and search the file for test?
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
error:
You cannot call a method on a null-valued expression.
At C:\MonitorFolder\test6.ps1 :56 char:34
+ If ($doc.Document.getElementB yId <<<< ('tbam').innerHTML.Contain s('Activi
tyMonitor'))
+ CategoryInfo : InvalidOperation: (getElementById:String) [], Ru
ntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\MonitorFolder\test6.ps1
+ If ($doc.Document.getElementB
tyMonitor'))
+ CategoryInfo : InvalidOperation: (getElementById:String) [], Ru
ntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
You cannot call a method on a null-valued expression.
At C:\MonitorFolder\test6.ps1 :56 char:34
+ If ($doc.Document.getElementB yId <<<< ("tbam").textContent.conta ins("Acti
vityMonitor"))
+ CategoryInfo : InvalidOperation: (getElementById:String) [], Ru
ntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
At C:\MonitorFolder\test6.ps1
+ If ($doc.Document.getElementB
vityMonitor"))
+ CategoryInfo : InvalidOperation: (getElementById:String) [], Ru
ntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
ForEach($WebSite in Get-Content C:\MonitorFolder\websites. txt){
$WebSite | % {
try {$web = New-Object System.Net.WebClient
$out = $web.DownloadString("$WebS ite")}
catch {$WebSite, $Down -join " " | out-file -filepath c:\MonitorFolder\ServersDo wn.txt -append
break}
write-host $website
# Create the IE com object
$ie = new-object -com InternetExplorer.Applicati on
# 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('userN ame')
#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('passw ord')
$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.getElementB yId("tbam" ).textCont ent.contai ns("Activi tyMonitor" ))
{$loginSucceeded, $WebSite -join " " | out-file -filepath c:\MonitorFolder\ServersUp .txt -append}
else
{$LoginFailed, $WebSite, $Check -join " " | out-file -filepath c:\MonitorFolder\ServersDo wn.txt -append}
} #catch {$LoginFailed, $WebSite, $Check -join " " | out-file -filepath c:\MonitorFolder\ServersDo wn.txt -append}
}
$WebSite | % {
try {$web = New-Object System.Net.WebClient
$out = $web.DownloadString("$WebS
catch {$WebSite, $Down -join " " | out-file -filepath c:\MonitorFolder\ServersDo
break}
write-host $website
# Create the IE com object
$ie = new-object -com InternetExplorer.Applicati
# 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('userN
#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('passw
$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.getElementB
{$loginSucceeded, $WebSite -join " " | out-file -filepath c:\MonitorFolder\ServersUp
else
{$LoginFailed, $WebSite, $Check -join " " | out-file -filepath c:\MonitorFolder\ServersDo
} #catch {$LoginFailed, $WebSite, $Check -join " " | out-file -filepath c:\MonitorFolder\ServersDo
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
You cannot call a method on a null-valued expression.
At C:\MonitorFolder\test6.ps1 :56 char:54
+ If ($doc.getElementById("tbam ").textCon tent.conta ins <<<< ("ActivityMonit
or"))
+ CategoryInfo : InvalidOperation: (contains:String) [], RuntimeE
xception
+ FullyQualifiedErrorId : InvokeMethodOnNull
At C:\MonitorFolder\test6.ps1
+ If ($doc.getElementById("tbam
or"))
+ CategoryInfo : InvalidOperation: (contains:String) [], RuntimeE
xception
+ FullyQualifiedErrorId : InvokeMethodOnNull
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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,Micros oft.PowerS hell.Comma nd
s.GetMemberCommand
At C:\MonitorFolder\test6.ps1
+ $doc.getElementById("tbam"
+ CategoryInfo : CloseError: (:) [Get-Member], InvalidOperationEx
ception
+ FullyQualifiedErrorId : NoObjectInGetMember,Micros
s.GetMemberCommand
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
The term 'ie.Document.getElementByI d' 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.getElementByI d <<<< ("tbam").textContent.conta ins("Activ i
ty"))
+ CategoryInfo : ObjectNotFound: (ie.Document.getElementByI d:Stri
ng) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
, 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
+ If (ie.Document.getElementByI
ty"))
+ CategoryInfo : ObjectNotFound: (ie.Document.getElementByI
ng) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
You cannot call a method on a null-valued expression.
At C:\MonitorFolder\test6.ps1 :59 char:62
+ If ($ie.Document.getElementBy Id("tbam") .textConte nt.contain s <<<< ("Activ
ityMonitor"))
+ CategoryInfo : InvalidOperation: (contains:String) [], RuntimeE
xception
+ FullyQualifiedErrorId : InvokeMethodOnNull
At C:\MonitorFolder\test6.ps1
+ If ($ie.Document.getElementBy
ityMonitor"))
+ CategoryInfo : InvalidOperation: (contains:String) [], RuntimeE
xception
+ FullyQualifiedErrorId : InvokeMethodOnNull
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Yes it is there
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
<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/activityMon itor.htl?m ethod=show ActivityMo nitor&scre enId=tbam" >ActivityM onitor </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>
<head>
<body marginwidth="2" marginheight="2" leftmargin="2" topmargin="2">
<form id="menuBarForm" method="POST" action="/t2/login/chooser.
<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/activityMon
</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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I am not the developer. I can talk to them and see if that can be done.
It is in JAVA.
It is in JAVA.
ASKER
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(loginc heck,'LogI n','LogedI n','UserLo gin 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/activityMon itor.htl?m ethod=show ActivityMo nitor&scre enId=tbam" >ActivityM onitor </a>
</td>
<td>
<a id="customer" href="/t2/reports/redirect -chooser.j sp?screenI d=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>
<html>
<head>
<body marginwidth="2" marginheight="2" leftmargin="2" topmargin="2">
<form id="menuBarForm" method="POST" action="/t2/login/chooser.
<header id="mast">
<nav id="navbar">
<table>
<tbody>
<tr>
<tr>
<script>
saveUserActivityLog(loginc
</script>
<td width="20px" align="left">
<div id="left_menu_header">
<img id="btn" class="hideButton" src="/t2/images/nav/2.jpg"
</div>
</td>
<td>
<a id="tbam" href="/t2/tbam/activityMon
</td>
<td>
<a id="customer" href="/t2/reports/redirect
</td>
</tr>
</tbody>
</table>
</nav>
</form>
<div class="container">
<div style="clear:both;"></div>
<div id="footerId">
<script language="JavaScript">
</body>
</html>
ASKER
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=showLog in")
write-host $WebSite2
$ie = new-object -com InternetExplorer.Applicati on
# 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.t xt"
$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=showLog
write-host $WebSite2
$ie = new-object -com InternetExplorer.Applicati
# 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.t
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Making some progress.
$source = $ie.Document.documentEleme nt.outerHT ML | out-file "C:\monitorFolder\test6.ht ml"
Downloads most of the page.
Trying to get the page searched for text now.
$source = $ie.Document.documentEleme
Downloads most of the page.
Trying to get the page searched for text now.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
If ($result -eq $True)
is nonsense, because the result of the IF expression again is $true or $false ;-). The correct usage is a simple If ($result)
ASKER
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?
ASKER
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.
Thank you so much for all the effort.
ASKER