Felix Wong
Felix Wong

Reputation: 181

Check certain text from a webpage via powershell

I am trying to get the HTML code from an Intranet webpage and monitor if certain texts or titles exist. This powershell code will be used by my monitoring program to trigger alerts when the webpage is down so that I cannot see that certain texts or titles.

For now, I'm just using Write-Host to see if my piece of code works. I can now extract the HTML source to $output, and I am sure 'Create!' can be found inside. However, I'm not getting a 'YES'.

May I know if $output can be checked by using -contains?

Thank you very much for your help!

$targetUrl  = 'https://myUrl/'
$ie = New-Object -com InternetExplorer.Application 
$ie.visible=$true
$ie.navigate($targetUrl)

while($ie.Busy) {
     Start-Sleep -m 2000
}

$output = $ie.Document.body.innerHTML

if($output -contains '*Create!*')
{Write-Host 'YES'}
else
{Write-Host 'NO'}

Upvotes: 2

Views: 3828

Answers (1)

vonPryz
vonPryz

Reputation: 24081

The operator -contains is used to search collections. The IE's innerHTML is just a string:

$output = $ie.Document.body.innerHTML
$output.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object

Use pattern matching operators like, well, -like and -match.

By the way, if IE is not mandatory, try Invoke-WebRequest cmdlet.

Upvotes: 4

Related Questions