This repository has been archived by the owner on Aug 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAzureAutomationRunbook
200 lines (161 loc) · 6.8 KB
/
AzureAutomationRunbook
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<#
.DESCRIPTION
OMS weather Solution - track weather forecast and observations within MSOMS
Usage and Configuration:
There are one config region per function. This script can get data from OpenweatherMap or Norwegian YR.no (not only norwegian locations)
Edit each config area to fit your own environment.
Script is intended to run in azure atuomation. You will have to create runbook assets to use this script
If you want to run in another automation tool or on your own computer, please change the general variables
In the end of the script. Comment out the function you do not want to use.
.NOTES
Version 1.5
Martin Ehrnst /adatum.no /@ehrnst
Cameron Fuller, Catapult systems /@cfullerMVP
.CHANGELOG
30.01.2017 v.1.5:
Fixed multiple location issue for Open Weather Map.
Thanks to 'jowildes' (blog comment) pointed out that there was some incorrect bracket placements causing the trouble
Minor code changes
October 2016 v1.1
(Initial release)
#>
#region General variables
$customerId = Get-AutomationVariable -Name 'CustomerID'
$SharedKey = Get-AutomationVariable -Name 'SharedKey'
$OpenWeatherMapKey = Get-AutomationVariable -Name 'OpenWeatherMapKey'
$time = [DATETIME]::Now
#endregion
#region OpenWeathermap Config
$Citys = '3137115', '2643743', '1880252' #Get your City ID from Open Weather Map URL
$Unit = 'Metric' #chose between Metric, Imperial or Kelvin
$OpenLog = "OpenWeather" #setting log type for OpenWeatherMap
#endregion
#region YRno Config
$URLs = 'http://www.yr.no/place/Norge/Rogaland/Stavanger/Stavanger/forecast.xml',
'http://www.yr.no/place/Norge/Hordaland/Bergen/Bergen/forecast.xml',
'http://www.yr.no/place/norge/oslo/oslo/oslo/forecast.xml',
'http://www.yr.no/place/USA/New_York/New_York/forecast.xml',
'http://www.yr.no/place/Storbritannia/England/London/forecast.xml'
$YRLog = "YRno" #setting the log type for YRno
#endregion
function Get-YrWeatherData{
<#
Get-YrWeatherData
uses yr.no xml api to get loaction forcasted and observed temperature.
Result is converted to Json and originally created for OMS data collector API
Version 1 September 2016
Martin Ehrnst /Adatum.no
NOTE: YR.no does not have observations for all locations.
#>
param(
[Parameter(Mandatory = $false)]
[string[]]$locationURL
)
if (!$locationURL){
$locationURL = 'http://yr.no/place/Norway/Oslo/Oslo/Oslo/forecast.xml'} #default URL to Oslo, Norway
#Create a table to accept multiple locations
$weatherTable = @()
foreach ($url in $locationurl){
[xml]$yr = Invoke-WebRequest -Uri $URL -UseBasicParsing
[string]$locationName = $yr.weatherdata.location.name
#Getting the forcasted temperature
[int]$ForecastTemp = $yr.SelectNodes("//forecast").tabular.time.temperature.value | Select-Object -First 1
[int]$Forecastprecipitation = $yr.SelectNodes("//forecast").tabular.time.precipitation.value | Select-Object -First 1
[int]$observedtemp = $yr.SelectNodes("//observations").weatherstation.temperature.value | Select-Object -First 1
[string]$observedVindName = $yr.SelectNodes("//observations").weatherstation.windSpeed.name | Select-Object -First 1
[string]$observedVindDirectioName = $yr.SelectNodes("//observations").weatherstation.windDirection.name | Select-Object -First 1
#Output
$weatherData = @{
'LocationName' = $locationName
'ForecastedTemp' = $ForecastTemp
'Precipitation' = $Forecastprecipitation
'ObservedTemp' = $observedtemp
'WindDirection' = $observedVindDirectioName
'Wind' = $observedVindName
}
#add location weather data to our table
$weatherTable +=$weatherData
}
#Convert data to Json accepted by OMS
$weathertable | ConvertTo-Json
}
Function Get-OpenWeatherMapData {
<#
Get-OpenWeatherMapData
Uses openweathermap.com api to get weather data and inserts in to OMS log analytics
Version 1.0 January 2017
Created by Cameron Fuller & Martin Ehrnst
#>
Param ($OpenWeatherMapKey, $Citys)
$weatherTable = @()
Foreach ($city in $Citys){
$GetWeather = Invoke-RestMethod -uri "api.openweathermap.org/data/2.5/weather?id=$City&APPID=$OpenWeatherMapKey&units=$Unit"
[String]$City = $GetWeather.name
[String]$WeatherDescription = $GetWeather.weather.description
[int]$Temp = $GetWeather.main.temp
[int]$WindSpeed = $GetWeather.wind.speed
[int]$BarometricPressure = $GetWeather.main.pressure
[int]$Humidity = $GetWeather.main.humidity
#Output
$weatherData = @{
'City' = $city
'Temp' = $Temp
'Humidity' = $Humidity
'WindSpeed' = $WindSpeed
'BarometricPressure' = $BarometricPressure
'Description' = $WeatherDescription
}
#add location weather data to our table
$weatherTable +=$weatherData
}
#Convert data to Json accepted by OMS
$weathertable | ConvertTo-Json
}
#End Function
# Function to create the authorization signature - TECHNET example
Function New-Signature ($customerId, $sharedKey, $date, $contentLength, $method, $contentType, $resource)
{
$xHeaders = 'x-ms-date:' + $date
$stringToHash = $method + "`n" + $contentLength + "`n" + $contentType + "`n" + $xHeaders + "`n" + $resource
$bytesToHash = [Text.Encoding]::UTF8.GetBytes($stringToHash)
$keyBytes = [Convert]::FromBase64String($sharedKey)
$sha256 = New-Object -TypeName System.Security.Cryptography.HMACSHA256
$sha256.Key = $keyBytes
$calculatedHash = $sha256.ComputeHash($bytesToHash)
$encodedHash = [Convert]::ToBase64String($calculatedHash)
$authorization = 'SharedKey {0}:{1}' -f $customerId, $encodedHash
return $authorization
}
#Send data to OMS - a technet example
Function Send-OMSData($customerId, $sharedKey, $body, $logType)
{
$method = 'POST'
$contentType = 'application/json'
$resource = '/api/logs'
$rfc1123date = [DateTime]::UtcNow.ToString('r')
$contentLength = $body.Length
$signature = New-Signature `
-customerId $customerId `
-sharedKey $sharedKey `
-date $rfc1123date `
-contentLength $contentLength `
-fileName $fileName `
-method $method `
-contentType $contentType `
-resource $resource
$uri = 'https://' + $customerId + '.ods.opinsights.azure.com' + $resource + '?api-version=2016-04-01'
$headers = @{
'Authorization' = $signature
'Log-Type' = $logType
'x-ms-date' = $rfc1123date
'time-generated-field' = $time
}
$response = Invoke-WebRequest -Uri $uri -Method $method -ContentType $contentType -Headers $headers -Body $body -UseBasicParsing
return $response.StatusCode
}
$YRdata = Get-YrWeatherData -locationURL $URLs
Send-OMSData -customerId $customerId -sharedKey $sharedKey -body ([System.Text.Encoding]::UTF8.GetBytes($YRdata)) -logType $YRlog
$YRdata
$Opendata = Get-OpenWeatherMapData -OpenWeatherMapKey $OpenWeatherMapKey -Citys $Citys
Send-OMSData -customerId $customerId -sharedKey $sharedKey -body ([System.Text.Encoding]::UTF8.GetBytes($Opendata)) -logType $OpenLog
$Opendata