Retrieve two last lines of read html page (PowerShell and VBS)

Hi all.
I'd like to write a script which is kind of obvious for scriptiong experts but now for me :) The key is:

Let's assume you have a webpage http://intranet/logs.html. Page is static, although ERROR or SUCCESS are changing depending on some action name. So it looks like:
<td><img src="ok.jpg" /></td>
<td><font size="2" color="red">TEMP</font></td>

Unfortunately the name is in the second line. I need to check if TEMP item has ok.jpg or bad.jpg lamp. So what I need to do is:

Get-Web http://intranet/logs.html
and then find a string TEMP and check if line before has ok.jpg or bad.jpg.

Need to do it in VBS also. Any scripts appretiated, points will be spread evenly :)
LVL 2
Rem-8Asked:
Who is Participating?
 
jostranderConnect With a Mentor Commented:
And powershell...
# ---------------------------------------------
#	User Variables
# ---------------------------------------------

$strURL="http://intranet/logs.html"


# String to search for
$strSearch="TEMP"


# ---------------------------------------------

$req = New-Object -COMObject "MSXML2.ServerXMLHTTP.6.0"
$req.Open("GET", $strURL)
$req.Send()
$text = $req.responsetext

$arrText=$text.split("`n")

for ($i=0; $i -le $arrText.getupperbound(0); $i++) {
	$line=$arrText[$i]
	if ($line.contains($strSearch)) {
		$lastLine= $arrText[$i-1]
		$pattern = "[a-z0-9\._-]+.jpg"
		$matches = [regex]::Matches($lastLine,$pattern)
		$matches[0].value
	}
}

Open in new window

0
 
yehudahaCommented:
you need a vbs file to check the line before the last line

<td><img src="ok.jpg" /></td>

if understand i don't need to retrive 2 line only the line i wrote above.
and then what print to the screen ?
0
 
x-menIT super heroCommented:
get-whatever | select-string TEMP -Context 1,0
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
x-menIT super heroCommented:
(new-object net.webclient).DownloadString("http://intranet/logs.html") | select-string TEMP -Context 1,0
0
 
jostranderConnect With a Mentor Commented:
If I understood the question... this VBS should work :)
'---------------------------------------------
'	User Variables
'---------------------------------------------
strURL="http://intranet/logs.html"

'String to search for
strSearch="TEMP"

'---------------------------------------------

strText=WebGet(strURL)

strImage=GetImage(strText,strSearch)

msgbox strImage




Function WebGet(myURL)

	Set http = CreateObject("MSXML2.xmlhttp")
	http.open "GET",myURL,FALSE
	http.send ""
	
	WebGet=http.responseText
	
End Function



Function GetImage(myText,mySearch)
	ON ERROR RESUME NEXT
	
	arrText=split(myText,vbCrLf)
	
	for i = 0 to ubound(arrText)
		line=arrText(i)
		If instr(line,mySearch) then
			lastLine=arrText(i-1)
			Set objRegEx = New RegExp
			objRegEx.Pattern = "[a-z0-9\._-]+.jpg"
			objRegEx.IgnoreCase=True
			Set colMatches=objRegEx.Execute(lastLine)
			GetImage=colMatches.Item(0)
			Exit Function
		End If
	next
End Function

Open in new window

0
 
Rem-8Author Commented:
I needed to change regex to second "contains" and worked fine. Thanks!
0
 
jostranderCommented:
Cool, glad I could help.  What was your final expression pattern?
0
 
Rem-8Author Commented:
# ---------------------------------------------
#      User Variables
# ---------------------------------------------

$strURL="http://intanet/logs.html"


# String to search for
$strSearch="Test"
$strok="uparrow.gif"


# ---------------------------------------------

$req = New-Object -COMObject "MSXML2.ServerXMLHTTP.6.0"
$req.Open("GET", $strURL)
$req.Send()
$text = $req.responsetext

$arrText=$text.split("`n")

for ($i=0; $i -le $arrText.getupperbound(0); $i++) {
      $line=$arrText[$i]
      if ($line.contains($strSearch)) {
                      echo "Found line" . $line
            $lastLine = $arrText[$i-1]
                      echo $lastLine
            if ($lastLine.contains($strok)) {
                               echo "Server UP"
                      }
      }
}

Had to echo some debug comments as you see :)
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.

All Courses

From novice to tech pro — start learning today.