I'm a totally powershell newbie, so please be indulgent 😉
I'm trying to write a script where a machine check an external website waiting for a the word: "test" to execute a download. The things goes like this:
$Word = 'test'
$WebClientObject = New-Object Net.WebClient
$comment = "http://MySite.wordpress.com/comment_section/"
$WebClientObject.Headers.Add("User-Agent", "Mozilla/5.0 (Windows NT 6.2; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/32.0.1667.0 Safari/537.36)")
While($True){
$CommentResult = $WebClientObject.DownloadString($comment)
$Found = $CommentResult.contains($Word)
If($Found) {
IEX $WebClientObject.DownloadString('http://A_Server_That_Is_Accessible/Tasks_to_do')
Task_to_do and some blabla commands
Return
}
Start-Sleep -Seconds 60
}
I'm running powershell 2.0. Windows7
What happened:
- the download is not executed over internet
- it is not a issue coming from IPs or paths
What i've done next:
- I directly write the different commands in the powershell prompt:
–> the While($True) initiate an endless loop without exiting it.
To make the trick i need to "double click return on my keyboard", the loop's ending and the "downloadstring" command is finally executed.
Still remember i'm more than a newbie, right? 🙁
- i tried "break" "return" or "exit" bu impossible to quit the
loop. - I tried to put many double carriage return in my script also without
any success.
Would someone please indicates me the way to do this simple thing…? 🙂
Thanks a lot.
Best Answer
There is at least three issues with your script:
contains
doesn't work the way you think it does. It tells whether a collection of reference values includes a single test value. In layman's terms, it operates only on elements of arrays and looks for exact match. But what you get in your$CommentResult
is one long string containing all page's HTML code. Socontains
treats is as an array with one element and because this element is not an exact match of string 'test', it returnsFalse
.if
block, you don't have to useInvoke-Expression
cmdlet (IEX
is an alias for it) to callDownloadString
method of$WebClientObject
.test
itself can occur in HTML as part of a tag, e.g.:I've fixed your script to use a regex and match anything, that happens to contain the string
test
(this includes protest, detest, latest, testament and so on) in a HTML code of web-page. Try it and modify to your needs afterwards: