-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-mwp-devices.ps1
331 lines (292 loc) · 16.6 KB
/
get-mwp-devices.ps1
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
<#
.SYNOPSIS
PowerShell agent to collect MWP device data and submit to the datawarehouse via API
.DESCRIPTION
The webAPI will identify based on your tenantGUID which data sources, and time periods
are being requested. This agent will then query the data source(s), collect the
data and submit it via the WebAPI for submision to the data warehouse cache database.
.NOTES
┌─────────────────────────────────────────────────────────────────────────────────────────────┐
│ get-mwp-devices.ps1 │
├─────────────────────────────────────────────────────────────────────────────────────────────┤
│ DATE : 7.18.2019 │
│ AUTHOR : Paul Drangeid │
│ SITE : https://github.com/pdrangeid/bnwh-cache-agent │
└─────────────────────────────────────────────────────────────────────────────────────────────┘
#>
param (
[string]$subtenant,
[switch]$noui
)
$ErrorActionPreference = 'SilentlyContinue'
Remove-Variable -name apikey | Out-Null
Remove-Variable -name tenantguid | Out-Null
$global:srccmdline= $($MyInvocation.MyCommand.Name)
$scriptappname = "Blue Net get-mwp-devices"
$baseapiurl="https://api-cache.bluenetcloud.com"
$ScheduledJobName = "Blue Net Warehouse O365 Refresh"
Write-Host "`nLoading includes: $PSScriptRoot\bg-sharedfunctions.ps1"
Try{. "$PSScriptRoot\bg-sharedfunctions.ps1" | Out-Null}
Catch{
Write-Warning "I wasn't able to load the sharedfunctions includes (which should live in the same directory as $global:srccmdline). `nWe are going to bail now, sorry 'bout that!"
Write-Host "Try running them manually, and see what error message is causing this to puke: $PSScriptRoot\bg-sharedfunctions.ps1"
BREAK
}
Prepare-EventLog
Function Set-CacheSyncJob{
Get-ScheduledTask -TaskName $ScheduledJobName -ErrorAction SilentlyContinue -OutVariable task |Out-Null
if ($task -and ![string]::IsNullOrEmpty($subtenant)){
$tenantjobtaskexists = $false
Write-Host "Checking Subtentant Task Status"
$task |
ForEach-Object {
if ($_.actions.Arguments -like '*'+$subtenant+'*') {
# Subtenant already has an action in the existing Scheduled Task
$tenantjobtaskexists = $true
}
if (!$tenantjobtaskexists){
write-host "This subtenant does not yet have an action item as a part of the scheduled task"
$answer=yesorno "Would you like to schedule this subtenant refresh job to run automatically?" "Schedule data synchronization"
if ($answer -eq $true){
$Username = $env:userdomain+"\"+$Env:USERNAME
$credentials = $Host.UI.PromptForCredential("Task username and password","Provide the password for this account that will run the scheduled task",$Username,$env:userdomain)
$Password = $Credentials.GetNetworkCredential().Password
$Prog = $env:systemroot + "\system32\WindowsPowerShell\v1.0\powershell.exe"
$thisuserupn = (get-aduser ($Env:USERNAME)).userprincipalname
$Opt = '-nologo -noninteractive -noprofile -ExecutionPolicy BYPASS -file "'+$PSScriptRoot+'\get-mwp-devices.ps1" -noui -subtenant '+$subtenant
$task | ForEach-Object {
$action = $_.actions
$action += New-ScheduledTaskAction -Execute $Prog -Argument $Opt -WorkingDirectory $PSScriptRoot
Set-ScheduledTask -TaskName $ScheduledJobName -Action $action -User $Username -Password $Password
}# End ForEach-Object (updating tasks)
}# End User answered YES to adding this task
}# End subtenantjob action is missing
}# End ForEach
}# End have subtenant AND scheduled task
if (!$task) {
# task does not exist, otherwise $task contains the task object
$answer=yesorno "Would you like to schedule this agent to run automatically?" "Schedule data synchronization"
if ($answer -eq $true){
$Username = $env:userdomain+"\"+$Env:USERNAME
$credentials = $Host.UI.PromptForCredential("Task username and password","Provide the password for this account that will run the scheduled task",$Username,$env:userdomain)
$Password = $Credentials.GetNetworkCredential().Password
$Prog = $env:systemroot + "\system32\WindowsPowerShell\v1.0\powershell.exe"
$thisuserupn = (get-aduser ($Env:USERNAME)).userprincipalname
$Opt = '-nologo -noninteractive -noprofile -ExecutionPolicy BYPASS -file "'+$PSScriptRoot+'\get-mwp-devices.ps1" -noui'
if (![string]::IsNullOrEmpty($subtenant)){
$Opt=$Opt+' -subtenant '+$subtenant
}
$Action = New-ScheduledTaskAction -Execute $Prog -Argument $Opt -WorkingDirectory $PSScriptRoot
$Trigger = New-ScheduledTaskTrigger -Daily -DaysInterval 1 -At "03:30"
$Settings = New-ScheduledTaskSettingsSet -DontStopOnIdleEnd -RestartInterval (New-TimeSpan -Minutes 1) -RestartCount 1 -StartWhenAvailable
$Settings.ExecutionTimeLimit = "PT10M"
$Task=Register-ScheduledTask -Action $Action -Trigger $Trigger -Settings $Settings -TaskName $ScheduledJobName -Description "Daily sends updated mwp device data to the reporting datawarehouse via WebAPI" -User $Username -Password $Password -RunLevel Highest
#$task.triggers.Repetition.Duration ="PT22H"
#$task.triggers.Repetition.Interval ="PT12M"
$task | Set-ScheduledTask -User $Username -Password $Password
}# Yes - operater wants us to schedule this task
}# End if (task doesn't already exist)
}#End Function
Function submit-cachedata($Cachedata,[string]$DSName){
write-host "The cache data looks like this `n [$Cachedata]"
# Takes the resulting cachedata and submits it to the webAPI
Write-Host "Submitting Data for $DSName"
#write-host "******************************* the cache data is: `n"$Cachedata
$ErrorActionPreference = 'Stop'
Try{
$apibase="https://api-cache.bluenetcloud.com/api/v1/submit-data/"
$apiurlparms="?TenantGUID="+$tenantguid+"&DataSourceName="+$DSName+"&NewTimeStamp="+$querytimestamp
$apiurl=$apibase+$apiurlparms.replace('+','%2b')
$ServicePoint = [System.Net.ServicePointManager]::FindServicePoint($apiurl)
if ($DSName -notlike '*vmware*'){
#$thecontent = @{"data" = $Cachedata}
#$thecontent = $($Cachedata | ConvertTo-Json -Depth 5 -Compress)
#$thecontent = $($Cachedata | ConvertTo-Json -Compress)
#$thecontent = $(@{"data" = $Cachedata} | ConvertTo-Json -Depth 5 -Compress)
$thecontent = (@{"data" = $Cachedata} | ConvertTo-Json -Compress)
}
$ErrorActionPreference= 'silentlycontinue'
$pjmb=[math]::Round(([System.Text.Encoding]::UTF8.GetByteCount($Cachedata))*0.00000095367432,2)
write-host "Submitting $ic updates for $DSName ($([math]::Round($pjmb,2))MB)"
if ($DSName -like '*vmware*'){
$thecontent = $Cachedata
Invoke-RestMethod $apiurl -Method 'Post' -Headers @{"x-api-key"=$APIKey;"content-type" = "binary"} -Body $thecontent -ErrorVariable RestError -ErrorAction SilentlyContinue -TimeoutSec 900
}
else {
if ($Cachedata -eq "Zero") {
$thecontent = '{"result":"zero results"}'
}
Invoke-RestMethod $apiurl -Method 'Post' -Headers @{"x-api-key"=$APIKey;Accept="application/json";"content-type" = "binary"} -Body $thecontent -ErrorVariable RestError -ErrorAction SilentlyContinue -TimeoutSec 900
write-host "******************************* the body data is: `n"$thecontent
}
}
Catch{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
$httpresponse = $_.Exception.Response
$HttpStatusCode = $RestError.ErrorRecord.Exception.Response.StatusCode.value__
$HttpStatusDescription = $RestError.ErrorRecord.Exception.Response.StatusDescription
write-host "Error Message $ErrorMessage `nFailed Item:$FailedItem `nhttp Response:$httpresponse`n"
$result = $_.Exception.Response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($result)
$reader.BaseStream.Position = 0
$reader.DiscardBufferedData()
$responseBody = $reader.ReadToEnd();
Write-Host "`nFailed to submit $m to $apiURL $ErrorMessage $FailedItem" -ForegroundColor Yellow
Write-Host "HTTP Response Status Code: "$HttpStatusCode
Write-Host "HTTP Response Status Description: "$HttpStatusDescription
Write-Host "TenantName: "$TenantName
Write-Host "Result: "$responseBody
EXIT
}
}
Function get-mwp-assets([string]$objclass){
Write-host "getting MWP device assets"
$ErrorActionPreference = 'Stop'
$o365results=(Get-MsolUser | Select-Object * )
elseif ($objclass -like '*licensetype'){
$o365results=(Get-MsolUser -All | Select DisplayName,userPrincipalname,isLicensed,BlockCredential,ValidationStatus,@{n="Licenses Type";e={$_.Licenses.AccountSKUid}})
}
elseif ($objclass -like '*mailbox'){
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $o365cred -Authentication Basic -AllowRedirection
Import-PSSession $Session -DisableNameChecking
#$o365results=(Get-MsolUser -All | Where-Object {$_.IsLicensed -eq $true -and $_.BlockCredential -eq $false} | Select-Object UserPrincipalName | ForEach-Object {Get-Mailbox -Identity $_.UserPrincipalName | Where-Object {$_.WhenChangedUTC -ge $tenantlastupdate} | Select-Object *})
$o365results=(Get-Mailbox | Where-Object {$_.WhenChangedUTC -ge $tenantlastupdate} | Select-Object *)
Remove-PSSession $Session
}
else {
write-host "We got something we didn't quite expect..."
write-host "request for $objclass"
return
}
$ic = [int]($o365results | measure).count
write-host "We got $ic results for $objclass"
Write-host "Assuming all went well, Now do some processing and uploading..."
return $($o365results)
}
$Path = "HKCU:\Software\BNCacheAgent\$subtenant\"
$Path=$Path.replace('\\','\')
Try{
$tenantguid = GetKey $($Path+$tenantdomain) $("TenantGUID") $("Enter Unique GUID for $tenantdomain in the password field:")
}
Catch
{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
Write-Host "Failed to retrieve tenant GUID from registry The error message was '$ErrorMessage' It is likely that you are not running this script as the original user who saved the secure tenant GUID."
Break
exit
}
Try
{
$Path = "HKCU:\Software\BNCacheAgent\"
$APIKey = GetKey $($Path) $("APIKey") $("Enter global APIKey in the password field:")
}
Catch
{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
Write-Host "Failed to retrieve APIKey from registry The error message was '$ErrorMessage' It is likely that you are not running this script as the original user who saved the APIKey value."
Break
exit
}
Try{
# Attempt to query the API to find out what data they would like us to retrieve
$Howsoonisnow=[DateTime]::UtcNow | get-date -Format "yyyy-MM-ddTHH:mm:ss"
$apiurl="https://api-cache.bluenetcloud.com/api/v1/get-data-requests"
$ServicePoint = [System.Net.ServicePointManager]::FindServicePoint($apiurl)
$params = @{"TenantGUID"=$tenantguid; "ClientAgentUTCDateTime" = $Howsoonisnow}
$Response = Invoke-RestMethod -uri $apiurl -Body $params -Method GET -Headers @{"x-api-key"=$APIKey;Accept="application/json"} -ErrorVariable RestError -ErrorAction SilentlyContinue
$Response | fl
}
Catch{
$ErrorMessage = $_.Exception.Message
$FailedItem = $_.Exception.ItemName
$httpresponse = $_.Exception.Response
$HttpStatusCode = $RestError.ErrorRecord.Exception.Response.StatusCode.value__
$HttpStatusDescription = $RestError.ErrorRecord.Exception.Response.StatusDescription
if ($ErrorMessage -eq 'Unable to connect to the remote server'){
Write-Host "`n"
Write-Warning "Unable to connect to the remote server $baseapiurl"
Write-Host "Please check DNS, firewall, and Internet connectivity to verify."
exit
}
write-host "Error Message $ErrorMessage `nFailed Item:$FailedItem `nhttp Response:$httpresponse`n"
$result = $_.Exception.Response.GetResponseStream()
$reader = New-Object System.IO.StreamReader($result)
$reader.BaseStream.Position = 0
$reader.DiscardBufferedData()
$responseBody = $reader.ReadToEnd();
Write-Host "`nFailed to submit $m to $apiURL $ErrorMessage $FailedItem" -ForegroundColor Yellow
Write-Host "HTTP Response Status Code: "$HttpStatusCode
Write-Host "HTTP Response Status Description: "$HttpStatusDescription
Write-Host "TenantName: "$TenantName
Write-Host "Result: "$responseBody
EXIT
}
$R2 = $Response | Convertfrom-Json
($R2.DataRequests | measure).count
if (($R2.DataRequests | measure).count -eq 0){
Write-Host "Client identified successfully - no data requests at this time. If this is your first run, please be sure the client's reporting setup has been completed."
Write-Host "Req: "($R2.DataRequests | measure).count
exit
}
$o365req=($r2.DataRequests | where-object -Property SourceName -like 'O365*')
if (![string]::IsNullOrEmpty($o365req) ){
Try{
write-host "Let's init o365"
$o365initialized=(get-o365admin $false)
write-host "got o365admin and the results are $o365initialized"
}
Catch{
write-host "Sorry - O365 init epic failure!"
}
}# end initializing O365
$dr = 0
Write-Host "Processing "$(($R2.DataRequests | measure).count) "data object requests."
$R2.DataRequests | ForEach-Object{
$dr++
Write-Host "Processing $dr of"$(($R2.DataRequests | measure).count) "data object requests."
$global:querytimestamp=[DateTime]::UtcNow | get-date -Format "yyyy-MM-ddTHH:mm:ss"
$ModDate=$_.NextUpdate
$MaxAge=$_.MaxAgeMinutes
$HasModified=$_.HasModifiedDate
$Delegated=$_.O365DelegatedAdmin
$SourceReqUpdate = $false
if ($querytimestamp -ge $ModDate) {
$SourceReqUpdate=$true
Write-Host $_.SourceName "Next Update requested at/after [$ModDate] with a MaxAge of $MaxAge and will be updated."
}
if (!$SourceReqUpdate){
Write-Host $_.SourceName "Next Update requested at/after [$ModDate] with a MaxAge of $MaxAge and is not in need of a query"
return
}
if ($_.SourceName -like "*ADSI*"){
#Ignoring ADSI requests - this script is O365 only)
}# end if (ADSI source request)
elseif ($_.SourceName -like "*vmware*"){
#Ignoring vmware requests - this script is O365 only)
} # End elseif $_.SourceName -like "*vmware*"
elseif ($_.SourceName -like "o365*"){
if ($o365initialized -eq $false){
Write-Warning "API has requested O365 data, but I could not initialize the MsolService."
exit
}
$global:querytimestamp=[DateTime]::UtcNow | get-date -Format "yyyy-MM-ddTHH:mm:ss"
$o365result=get-o365-assets $_.Sourcename
submit-cachedata $o365result $_.SourceName
}# $_.SourceName -like "o365*"
else {
write-host "Some other data request... "$_.SourceName" ... and I have no idea what to do with it!"
return
}
}# Next $R2.DataRequests object
Write-Host "All Done processing "$(($R2.DataRequests | measure).count) " requests."
Get-PSSession | Remove-PSSession
if ($noui -ne $true){
# Check to see if the job is scheduled
Set-CacheSyncJob
}
Remove-Variable -name apikey | Out-Null
Remove-Variable -name tenantguid | Out-Null
Remove-Variable -name params | Out-Null