1: #Weather Stations http://www.wunderground.com/wundermap/?
2:
3:
4: function Download-File ([string] $URL, [string] $SaveAsFile, [Switch] $WhatIf)
5: {
6: if($WhatIf)
7: {
8: $URL
9: $SaveAsFile
10: }
11: else
12: {
13: $WebDownloader = new-object System.Net.Webclient
14: $StartTime = Get-Date
15: Write ("Download started at {0}" -f $StartTime)
16: Write ("Downloading {0} bytes from {1} and saving file as {2}" -f $Length, $URL, $SaveAsFile)
17:
18: try
19: {
20: $WebDownloader.DownloadFile( $URL, $SaveAsFile);
21: }
22: catch
23: {
24: throw $_
25: }
26: finally
27: {
28: $WebDownloader.Dispose();
29: }
30:
31:
32: $EndTime = Get-Date
33: $Duration = $EndTime - $StartTime
34: Write ("Download ended at {0}" -f $EndTime)
35: Write ("Download took {0:F} seconds, which is {1:F} minutes" -f $Duration.TotalSeconds, $Duration.TotalMinutes )
36:
37: }
38: }
39:
40:
41: function Get-WeatherStationHistory
42: {
43:
44: [CmdletBinding(DefaultParameterSetName="PreviousDays")]
45: PARAM(
46:
47: [Parameter(Position=0, Mandatory=$false, ValueFromPipeline=$false, ParameterSetName="PreviousDays")]
48: [ValidateScript({([DateTime]::Now - $_) -ge "1/1/2006"})
49: [TimeSpan] $PreviousDays,
50:
51: [Parameter(Position=0, Mandatory=$false, ValueFromPipeline=$false)]
52: [ValidateScript({$_ -le [DateTime]::Now -and $_ -ge "1/1/2006"})
53: #[ValidateScript({$_ -le $LastDay })] Problem $LastDay is not defined yet
54: [DateTime] $FirstDay=[DateTime]::Now,
55:
56: [Parameter(Position=1, Mandatory=$false, ValueFromPipeline=$false, ParameterSetName="NextDays")]
57: [ValidateScript({($FirstDay + $_) -le [DateTime]::Now})
58: [TimeSpan] $NextDays,
59:
60: [Parameter(Position=1, Mandatory=$false, ValueFromPipeline=$false, ParameterSetName="LastDay")]
61: [ValidateScript({$_ -le [DateTime]::Now -and $_ -ge "1/1/2006"})
62: [ValidateScript({$_ -ge $FirstDay })]
63: [DateTime] $LastDay=[DateTime]::Now,
64:
65: [Parameter(Position=2, Mandatory=$true, ValueFromPipeline=$false)]
66: [ValidateSet("KCARIVER16", "KCAMOREN6", "KCABEAUM3", IgnoreCase = $true)]
67: [String] $WeatherStationCode = "KCAMISSI5"
68:
69:
70: )
71: Process{
72:
73: switch ($PsCmdlet.ParameterSetName)
74: {
75: "NextDays"
76: {
77: "Parameter Set NextDays"
78: Process-Downloads -FirstDay $FirstDay -NumberOfDays $NextDays -Station $WeatherStationCode
79: }
80:
81: "LastDay"
82: {
83: "Parameter Set LastDay"
84: $Days = $LastDay - $FirstDay
85: Process-Downloads -FirstDay $FirstDay -NumberOfDays $Days -Station $WeatherStationCode
86: }
87:
88: "PreviousDays"
89: {
90: "Parameter Set PreviousDays"
91: $StartDay = [DateTime]::Now - $PreviousDays
92: Process-Downloads -FirstDay $StartDay -NumberOfDays $PreviousDays -Station $WeatherStationCode
93: }
94: }
95:
96: }# End Process
97: }
98:
99:
100: function Process-Downloads([DateTime] $FirstDay, [TimeSpan] $NumberOfDays, [String]$Station)
101: {
102: $i = 0;
103: while( $FirstDay.AddDays($i) -le $FirstDay.AddDays($NumberOfDays.TotalDays))
104: {
105: $CurrentDay = $FirstDay.AddDays($i)
106: $i++
107:
108: $CurrentUrl = "http://www.wunderground.com/weatherstation/WXDailyHistory.asp?ID={0}&month={1}&day={2}&year={3}&format=1" -f $Station, $CurrentDay.Month, $CurrentDay.Day, $CurrentDay.Year
109: $FileName = "{0}_{1}{2:D2}{3:D2}.html" -f $Station, $CurrentDay.Year, $CurrentDay.Month, $CurrentDay.Day
110:
111: try
112: {
113: Download-File -URL $CurrentUrl -SaveAsFile $FileName
114: }
115: catch
116: {
117: write-warning "$_`n`n"
118: }
119: }
120: }
121:
122: