Author Posts

July 7, 2017 at 12:20 pm

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
}

July 7, 2017 at 1:05 pm

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.

July 7, 2017 at 1:24 pm

And the top of the page says...
"Effective Thursday, June 30, 2016 ... Java applet-based webpages at https://www.aviationweather.gov/adds will no longer be publicly accessible."

July 7, 2017 at 1:28 pm

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