Invoke-webrequest

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Tony Antony Tony Antony 3 weeks ago.

  • Author
    Posts
  • #74444
    Profile photo of Tony Antony
    Tony Antony
    Participant

    Hello,

    I'm trying to create a script that gets weather information from a website. When I run this, it's giving me this error. I'm not sure what I'm doing wrong.

    This is the video I'm referencing https://www.youtube.com/watch?v=VmHDiXQTs1s

    if I add "/" in $check so that it is

    $check = Invoke-WebRequest ("http://aviationweather.gov/adds/" + $urlForms.Action) -WebSession $weather -Method Post -Body $urlForms.Fields

    it gives no error messages, but it doesn't give me any weather information.

    Invoke-WebRequest : The remote server returned an error: (404) Not Found.
    At U:\PowerShell\CheckWeather.ps1:12 char:14
    + ... $check = Invoke-WebRequest ("http://aviationweather.gov/adds" + $u ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

    This is my script so far.

    $url = Invoke-WebRequest "http://aviationweather.gov/adds" -SessionVariable weather
    
    if($url.StatusCode -eq 200) {
        Write-Host "Connected to Aviation Weather Center" -ForegroundColor Green
    
        $zipcode = Read-Host "Enter the zip code or city"
    
        $urlForms = $URL.Forms[0]
    
        $urlForms.fields["autocomplete"] = $zipcode
    
        $check = Invoke-WebRequest ("http://aviationweather.gov/adds" + $urlForms.Action) -WebSession $weather -Method Post -Body $urlForms.Fields
    }
    else {
        Write-Host "Could not connect to Aviation Weather Center" -ForegroundColor Red
    }
  • #74446
    Profile photo of Will Anderson
    Will Anderson
    Keymaster

    Hey there Tony,

    Going through the code line by line, it errored out here:

    PS C:\windows\system32> Invoke-WebRequest ("http://aviationweather.gov/adds" + $urlForms.Action)
    Invoke-WebRequest : The remote server returned an error: (404) Not Found.
    At line:1 char:1
    + Invoke-WebRequest ("http://aviationweather.gov/adds" + $urlForms.Acti ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

    This is the URL you're looking at:
    PS C:\windows\system32> ("http://aviationweather.gov/adds" + $urlForms.Action)
    http://aviationweather.gov/adds/popups/location/search

    When I hit that in the browser, it comes back with a page not found.

  • #74458
    Profile photo of Tony Antony
    Tony Antony
    Participant

    Thanks guys, I'll dig around for a solution. May be I'm not telling it to submit the zipcode

    $urlForms.Action
    /popups/location/search

    I can see that it's reading my $zipcode value

    $urlForms.Fields
    
    Key          Value
    ---          -----
    autocomplete 90210
    btnSearch    Go   

You must be logged in to reply to this topic.