-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPSAzureFunctions.psm1
623 lines (520 loc) · 23.2 KB
/
PSAzureFunctions.psm1
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
Function Connect-GraphAPI {
<#
.SYNOPSIS
Connect to Azure Graph API
.DESCRIPTION
Connect to Azure Graph API with either a Secret value from your Azure Application
OR
Connect with a uploaded certificate, you need to install the certificate in your LocalMachine\My store for this to work.
When connected you recieve a header variable $azureGraphAuthenticationHeader that you can use with your own Invoke-RestMethod commands if needed.
All the Modules within this Module already uses this value so you never need to add it anywhere if you are not doing your own things.
.PARAMETER AzureTenantID
Your Azure Tenant ID
.PARAMETER ApplicationID
The Azure Application ID for your Application.
.PARAMETER APISecret
If CertThumbprint is not used then this should be supplied - the Secret value created from your Azure Application
.PARAMETER CertThumbprint
Your Certificate Thumbprint that is installed in your 'LocalMachine\My' store and Uploaded to the Azure Application
.PARAMETER LogToFile
This parameter is connected to the Module PSLoggingFunctions mot information can be found on the GitHub.
https://github.com/rakelord/PSLoggingFunctions
.EXAMPLE
Connect-GraphAPI -AzureTenantID "1533bb8f-4d6a-54b3-a5d3-48463a22ca25" -ApplicationID "2cf231fa-236d-4ew1-ja72-a14a45web451" -APISecret "ZTasdergearbgaerbnreanrae=" -LogToFile $True
Connect-GraphAPI -AzureTenantID "1533bb8f-4d6a-54b3-a5d3-48463a22ca25" -ApplicationID "2cf231fa-236d-4ew1-ja72-a14a45web451" -CertThumbprint "40F51F9DCA3245FSDG7654GTSDSGDF1D78031C9" -LogToFile $False
#>
param(
[parameter(mandatory)]
$AzureTenantID,
[parameter(mandatory)]
$ApplicationID,
$APISecret,
$CertThumbprint,
[parameter(mandatory)]
[ValidateSet("True","False")]
$LogToFile
)
$Body = @{
Grant_Type = "client_credentials"
Scope = "https://graph.microsoft.com/.default"
client_Id = "$ApplicationID"
Client_Secret = "$APISecret"
}
$OAUTH2Url = "https://login.microsoftonline.com/$AzureTenantID/oauth2/v2.0/token"
Write-Log -Message "Connecting to Azure Graph API" -Active $LogToFile
if ($CertThumbprint){ # Certificate
$Cert = Invoke-TryCatchLog -InfoLog "Searching for the Certificate: $CertThumbprint" -LogToFile $LogToFile -ScriptBlock {
Get-X509Certificate -thumbPrint $CertThumbprint -storeName "My"
}
$binaryCertificateFingerprint = Convert-HexStringToByteArray($Cert.Thumbprint)
$base64EncodedFingerprint = [System.Convert]::ToBase64String($binaryCertificateFingerprint)
$JWT_Header = @{
alg = "RS256"
x5t = $base64EncodedFingerprint
typ = "JWT"
} | ConvertTo-Json
$now = (Get-Date).ToUniversalTime()
$createDate = (New-TimeSpan -Start 1970-01-01 -End ($now).DateTime).TotalSeconds
$expiryDate = (New-TimeSpan -Start 1970-01-01 -End ($now).AddMinutes(60).DateTime).TotalSeconds
$JWT_Payload = @{
iss = $ApplicationID
sub = $ApplicationID
aud = $OAUTH2Url
iat = $createDate
nbf = $createDate
exp = $expiryDate
jti = (New-Guid).Guid
} | ConvertTo-Json
$JWT_Token = Invoke-TryCatchLog -InfoLog "Generating the JWT Token" -LogToFile $LogToFile -ScriptBlock {
New-Jwt -Cert $Cert -PayloadJson $JWT_Payload -Header $JWT_Header
}
$Form = @{
grant_type = "client_credentials"
client_assertion_type = "urn:ietf:params:oauth:client-assertion-type:jwt-bearer"
client_id = $ApplicationID
scope = "https://graph.microsoft.com/.default"
client_assertion = $JWT_Token
}
$Headers = @{
"Content-Type" = "application/x-www-form-urlencoded;charset=UTF-8"
}
$GraphAPIToken = Invoke-TryCatchLog -InfoLog "Retrieving the Azure Graph API Token" -LogToFile $LogToFile -ScriptBlock {
Invoke-RestMethod -Method POST -Uri $OAUTH2Url -Body $Form -Headers $Headers
}
$azureGraphAuthenticationHeader = @{ Authorization = "$($GraphAPIToken.token_type) $($GraphAPIToken.access_token)" }
}
else { # Secret
$GraphAPIToken = Invoke-TryCatchLog -InfoLog "Retrieving the Azure Graph API Token" -LogToFile $LogToFile -ScriptBlock {
Invoke-RestMethod -Uri "$OAUTH2Url" -Method POST -Body $Body
}
$azureGraphAuthenticationHeader = @{ Authorization = "$($GraphAPIToken.token_type) $($GraphAPIToken.access_token)" }
}
# Verify connection
$global:AzureGraphAPIAuthenticated = $false
if ($GraphAPIToken.access_token){
$global:AzureGraphAPIAuthenticated = $true
Write-Log -Message "Azure Graph API Authenticated: $AzureGraphAPIAuthenticated" -Active $LogToFile
Write-Host "Azure Graph API Authenticated: $AzureGraphAPIAuthenticated"
Write-Host "Use Header Connection Variable ="'$azureGraphAuthenticationHeader'
$global:azureGraphAuthenticationHeader = $azureGraphAuthenticationHeader
return ""
}
Write-Log -Message "Azure Graph API Authenticated: $AzureGraphAPIAuthenticated" -Active $LogToFile
Write-Host "Azure Graph API Authenticated: $AzureGraphAPIAuthenticated"
return $false
}
function Find-AzureGraphAPIConnection {
if (!$AzureGraphAPIAuthenticated){
Write-Warning "Azure Graph API is not authenticated, you need to run Connect-GraphAPI and make sure you put in the correct credentials!"
return $false
}
return $true
}
function Convert-HexStringToByteArray {
param (
[string]$hex
)
$byteArray = [byte[]]::new($hex.length / 2)
for ($i = 0; $i -lt $hex.length; $i += 2) {
$byteArray[$i / 2] = [byte]::Parse($hex.Substring($i, 2), [System.Globalization.NumberStyles]::HexNumber)
}
return $byteArray
}
Function Get-EndpointManagerDevices {
<#
.SYNOPSIS
Retrieve all Endpoint Manager Devices from Azure
.DESCRIPTION
Retrieve all Endpoint Manager Devices from Azure and the ability to get the data as a HashTable or a normal Powershell object.
Also gives all objects a separation if they are mobile phones or computers.
.PARAMETER LogToFile
This parameter is connected to the Module PSLoggingFunctions mot information can be found on the GitHub.
https://github.com/rakelord/PSLoggingFunctions
.PARAMETER AsHashTable
Great a HashTable with the serialNumber property as the Key value $Object[$serialNumber]
.EXAMPLE
Get-EndpointManagerDevices -AsHashTable -LogToFile $True
Get-EndpointManagerDevices -LogToFile $False
.NOTES
You need these Azure Application permissions for this to work.
Application - DeviceManagementManagedDevices.Read.All
#>
param(
[parameter(mandatory)]
[ValidateSet("True","False")]
$LogToFile,
[switch]
$AsHashTable
)
if (Find-AzureGraphAPIConnection){
$Uri = "https://graph.microsoft.com/beta/deviceManagement/managedDevices"
$EndpointDevices = @()
do {
$Results = Invoke-TryCatchLog -InfoLog "Retrieving 1000 Endpoint devices" -LogToFile $LogToFile -ScriptBlock {
Invoke-RestMethod -Headers $azureGraphAuthenticationHeader -Uri $Uri -UseBasicParsing -Method "GET" -ContentType "application/json"
}
if ($Results.value) {
$EndpointDevices += $Results.value
}
else {
$EndpointDevices += $Results
}
$uri = $Results.'@odata.nextlink'
} until (!($uri))
# sort devices by lastSyncDateTime to fix duplicate issues from Endpoint AND give all devices a device_type with either 'Mobile phone' or 'Computer'
$EndpointDevices = $EndpointDevices | Sort-Object -Property lastSyncDateTime | Select-Object *,@{
l='device_type';e={
if ($_.imei -AND $_.operatingSystem -ne 'Windows'){
return "Mobile phone"
}
return "Computer"
}
}
# Faster filtering if HashTable is used, if you can reference the serialnumber when looking for device in Object list.
$HashTable = @{}
foreach ($Device in $EndpointDevices){
$HashTable[$Device.serialNumber] = $Device
}
if ($AsHashTable) { return $HashTable }
$deviceList = @()
foreach ($Device in $HashTable.Keys) {
$deviceList += $HashTable[$Device]
}
return $deviceList
}
}
function Get-X509Certificate {
Param(
[parameter(mandatory)]
$thumbPrint,
[parameter(mandatory)]
$storeName
)
$store = New-Object System.Security.Cryptography.X509Certificates.X509Store($storeName, "LocalMachine")
$store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadOnly)
$certificates = $store.Certificates.Find([System.Security.Cryptography.X509Certificates.X509FindType]::FindByThumbprint, $thumbPrint, $false)
$store.Close()
return $certificates[0]
}
function ConvertTo-Base64UrlString {
<#
.SYNOPSIS
Base64url encoder.
.DESCRIPTION
Encodes a string or byte array to base64url-encoded string.
.PARAMETER in
Specifies the input. Must be string, or byte array.
.INPUTS
You can pipe the string input to ConvertTo-Base64UrlString.
.OUTPUTS
ConvertTo-Base64UrlString returns the encoded string by default.
.EXAMPLE
PS Variable:> '{"alg":"RS256","typ":"JWT"}' | ConvertTo-Base64UrlString
eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9
.LINK
https://github.com/SP3269/posh-jwt
.LINK
https://jwt.io/
#>
[CmdletBinding()]
param (
[Parameter(Mandatory=$true,ValueFromPipeline=$true)]$in
)
if ($in -is [string]) {
return [Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($in)) -replace '\+','-' -replace '/','_' -replace '='
}
elseif ($in -is [byte[]]) {
return [Convert]::ToBase64String($in) -replace '\+','-' -replace '/','_' -replace '='
}
else {
throw "ConvertTo-Base64UrlString requires string or byte array input, received $($in.GetType())"
}
}
function New-Jwt {
<#
.SYNOPSIS
Creates a JWT (JSON Web Token).
.DESCRIPTION
Creates signed JWT given a signing certificate and claims in JSON.
.PARAMETER Payload
Specifies the claim to sign in JSON. Mandatory string.
.PARAMETER Header
Specifies a JWT header. Optional. Defaults to '{"alg":"RS256","typ":"JWT"}'.
.PARAMETER Cert
Specifies the signing certificate of type System.Security.Cryptography.X509Certificates.X509Certificate2. Must be specified and contain the private key if the algorithm in the header is RS256.
.PARAMETER Secret
Specifies the HMAC secret. Can be byte array, or a string, which will be converted to bytes. Must be specified if the algorithm in the header is HS256.
.INPUTS
You can pipe a string object (the JSON payload) to New-Jwt.
.OUTPUTS
System.String. New-Jwt returns a string with the signed JWT.
.EXAMPLE
PS Variable:\> $cert = (Get-ChildItem Cert:\CurrentUser\My)[1]
PS Variable:\> New-Jwt -Cert $cert -PayloadJson '{"token1":"value1","token2":"value2"}'
eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJ0b2tlbjEiOiJ2YWx1ZTEiLCJ0b2tlbjIiOiJ2YWx1ZTIifQ.Kd12ryF7Uuk9Y1UWsqdSk6cXNoYZBf9GBoqcEz7R5e4ve1Kyo0WmSr-q4XEjabcbaG0hHJyNGhLDMq6BaIm-hu8ehKgDkvLXPCh15j9AzabQB4vuvSXSWV3MQO7v4Ysm7_sGJQjrmpiwRoufFePcurc94anLNk0GNkTWwG59wY4rHaaHnMXx192KnJojwMR8mK-0_Q6TJ3bK8lTrQqqavnCW9vrKoWoXkqZD_4Qhv2T6vZF7sPkUrgsytgY21xABQuyFrrNLOI1g-EdBa7n1vIyeopM4n6_Uk-ttZp-U9wpi1cgg2pRIWYV5ZT0AwZwy0QyPPx8zjh7EVRpgAKXDAg
.EXAMPLE
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2("/mnt/c/PS/JWT/jwt.pfx","jwt")
$now = (Get-Date).ToUniversalTime()
$createDate = [Math]::Floor([decimal](Get-Date($now) -UFormat "%s"))
$expiryDate = [Math]::Floor([decimal](Get-Date($now.AddHours(1)) -UFormat "%s"))
$rawclaims = [Ordered]@{
iss = "examplecom:apikey:uaqCinPt2Enb"
iat = $createDate
exp = $expiryDate
} | ConvertTo-Json
$jwt = New-Jwt -PayloadJson $rawclaims -Cert $cert
$apiendpoint = "https://api.example.com/api/1.0/systems"
$splat = @{
Method="GET"
Uri=$apiendpoint
ContentType="application/json"
Headers = @{authorization="bearer $jwt"}
}
Invoke-WebRequest @splat
.LINK
https://github.com/SP3269/posh-jwt
.LINK
https://jwt.io/
#>
[CmdletBinding()]
param (
[Parameter(Mandatory=$false)][string]$Header = '{"alg":"RS256","typ":"JWT"}',
[Parameter(Mandatory=$true, ValueFromPipeline=$true)][string]$PayloadJson,
[Parameter(Mandatory=$false)][System.Security.Cryptography.X509Certificates.X509Certificate2]$Cert,
[Parameter(Mandatory=$false)]$Secret # Can be string or byte[] - checks in the code
)
Write-Verbose "Payload to sign: $PayloadJson"
try { $Alg = (ConvertFrom-Json -InputObject $Header -ErrorAction Stop).alg } # Validating that the parameter is actually JSON - if not, generate breaking error
catch { throw "The supplied JWT header is not JSON: $Header" }
Write-Verbose "Algorithm: $Alg"
try { ConvertFrom-Json -InputObject $PayloadJson -ErrorAction Stop | Out-Null } # Validating that the parameter is actually JSON - if not, generate breaking error
catch { throw "The supplied JWT payload is not JSON: $PayloadJson" }
$encodedHeader = ConvertTo-Base64UrlString $Header
$encodedPayload = ConvertTo-Base64UrlString $PayloadJson
$jwt = $encodedHeader + '.' + $encodedPayload # The first part of the JWT
$toSign = [System.Text.Encoding]::UTF8.GetBytes($jwt)
if (-not $PSBoundParameters.ContainsKey("Cert")) {
throw "RS256 requires -Cert parameter of type System.Security.Cryptography.X509Certificates.X509Certificate2"
}
Write-Verbose "Signing certificate: $($Cert.Subject)"
$rsa = $Cert.PrivateKey
if ($null -eq $rsa) { # Requiring the private key to be present; else cannot sign!
throw "There's no private key in the supplied certificate - cannot sign"
}
else {
# Overloads tested with RSACryptoServiceProvider, RSACng, RSAOpenSsl
try { $sig = ConvertTo-Base64UrlString $rsa.SignData($toSign,[Security.Cryptography.HashAlgorithmName]::SHA256,[Security.Cryptography.RSASignaturePadding]::Pkcs1) }
catch { throw New-Object System.Exception -ArgumentList ("Signing with SHA256 and Pkcs1 padding failed using private key $($rsa): $_", $_.Exception) }
}
$jwt = $jwt + '.' + $sig
return $jwt
}
function Get-EntraIDUsers {
<#
.SYNOPSIS
Retrieve all users from Azure Entra
.DESCRIPTION
Retrieve all users from Azure Entra and let the user choose if they want a HashTable object or just normal Powershell Object
Also the ability to set which property to be the key in the hashtable.
.PARAMETER HashTableKey
The $variable[keyvalue] - The key value that will be the filter
.PARAMETER AsHashTable
If the function should return a HashTable otherwise it will be normal powershell object.
.PARAMETER LogToFile
This parameter is connected to the Module PSLoggingFunctions mot information can be found on the GitHub.
https://github.com/rakelord/PSLoggingFunctions
.EXAMPLE
Return a HashTable with the userprincipalName as Hash Key and create a log
Get-EntraUsers -AsHashTable -HashTableKey "userprincipalName" -LogToFile $True
Return a Normal Powershell object and do not Log
Get-EntraUsers -LogToFile $False
.NOTES
You need these Azure Application permissions for this to work.
Application - User.Read.All
#>
Param(
[switch]
$AsHashTable,
$HashTableKey,
$Filter,
[parameter(mandatory)]
[ValidateSet("True","False")]
$LogToFile
)
if (Find-AzureGraphAPIConnection){
$Uri = "https://graph.microsoft.com/beta/users?" + '$top=999'
if ($Filter){
$Uri = $Uri + '&$filter=' + $Filter
}
$UserObjects = @()
do {
$Results = Invoke-TryCatchLog -InfoLog "Retrieving 1000 Entra User objects" -LogToFile $LogToFile -ScriptBlock {
Invoke-RestMethod -Headers $azureGraphAuthenticationHeader -Uri $Uri -UseBasicParsing -Method "GET" -ContentType "application/json"
}
if ($Results.value) {
$UserObjects += $Results.value
}
else {
$UserObjects += $Results
}
$uri = $Results.'@odata.nextlink'
if ($Filter -AND $uri){
$uri = $uri + '&$filter=' + $Filter
}
} until (!($uri))
if ($AsHashTable){
$HashTable = @{}
foreach ($user in $UserObjects) {
$HashTable[$user."$HashTableKey"] = $user
}
return $HashTable
}
return $UserObjects
}
}
Function Get-AzureTenantSecurityScore {
<#
.SYNOPSIS
Retrieve the Azure Tenants Security Score
.DESCRIPTION
Retrieve the Azure Tenants Security Score, only get the latest score instead of a long list.
And only retrieve the Secore + Comparable Tenants Score
.PARAMETER LogToFile
This parameter is for logging, check GitHub for PSLoggingFunctions if you want to learn more, just set $True if you want logging or $False if you don't want any logging.
.EXAMPLE
Get-AzureTenantSecurityScore -LogToFile $False
*OUTPUT*
ComparableTenantsSecurityScore TenantSecurityScore
------------------------------ -------------------
40,43 60,64
.NOTES
You need these Azure Application permissions for this to work.
Application - SecurityEvents.Read.All
#>
Param(
[parameter(mandatory)]
[ValidateSet("True","False")]
$LogToFile
)
if (Find-AzureGraphAPIConnection){
$securityScore = Invoke-TryCatchLog -InfoLog "Retrieving Security Score for Tenant" -LogToFile $LogToFile -ScriptBlock {
Invoke-RestMethod -Method GET -Uri 'https://graph.microsoft.com/beta/security/secureScores?$top=1' -Headers $azureGraphAuthenticationHeader
}
return [pscustomobject]@{
TenantSecurityScore = [math]::Round(($securityScore.value.currentScore / $securityScore.value.maxScore) * 100,2)
ComparableTenantsSecurityScore = ($securityScore.value.averageComparativeScores | Where-Object {$_.basis -eq 'TotalSeats'}).averageScore
}
}
}
Function Get-AzureServicePrincipals {
# Permission: Application.Read.All
param(
[parameter(mandatory)]
[ValidateSet("True","False")]
$LogToFile
)
if (Find-AzureGraphAPIConnection) {
$Url = "https://graph.microsoft.com/beta/servicePrincipals?" + '$top=999'
$Output = Invoke-TryCatchLog -InfoLog "Retrieve App servicePrincipals" -LogToFile $LogToFile -ScriptBlock {
Invoke-RestMethod -Method GET -Uri $Url -Headers $azureGraphAuthenticationHeader
}
return $Output
}
}
Function Get-AzureServicePrincipalOwner {
# Permission: Application.Read.All
param(
[parameter(mandatory)]
$ServicePrincipalID,
[parameter(mandatory)]
[ValidateSet("True","False")]
$LogToFile
)
if (Find-AzureGraphAPIConnection) {
$Url = "https://graph.microsoft.com/beta/servicePrincipals/$ServicePrincipalID/owners"
$Output = Invoke-TryCatchLog -InfoLog "Retrieve App servicePrincipal: $ServicePrincipalID Owner" -LogToFile $LogToFile -ScriptBlock {
Invoke-RestMethod -Method GET -Uri $Url -Headers $azureGraphAuthenticationHeader
}
return $Output
}
}
Function Get-AzureServicePrincipalSignInActivities {
# Permission: AuditLog.Read.All
param(
[parameter(mandatory)]
[ValidateSet("True","False")]
$LogToFile
)
if (Find-AzureGraphAPIConnection) {
$Url = "https://graph.microsoft.com/beta/reports/servicePrincipalSignInActivities?" + '$top=999'
$Output = Invoke-TryCatchLog -InfoLog "Retrieve Sign in Activity for App servicePrincipals" -LogToFile $LogToFile -ScriptBlock {
Invoke-RestMethod -Method GET -Uri $Url -Headers $azureGraphAuthenticationHeader
}
return $Output
}
}
Function Get-AzureApplications {
# Permission: Application.Read.All, Directory.Read.All
param(
[parameter(mandatory)]
[ValidateSet("True","False")]
$LogToFile
)
if (Find-AzureGraphAPIConnection) {
$Url = "https://graph.microsoft.com/beta/applications"
$Output = Invoke-TryCatchLog -InfoLog "Retrieve Azure Applications" -LogToFile $LogToFile -ScriptBlock {
Invoke-RestMethod -Method GET -Uri $Url -Headers $azureGraphAuthenticationHeader
}
return $Output
}
}
Function Get-AzureApplicationOwner {
# Permission: Application.ReadWrite.OwnedBy
param(
[parameter(mandatory)]
$AppID,
[parameter(mandatory)]
[ValidateSet("True","False")]
$LogToFile
)
if (Find-AzureGraphAPIConnection) {
$Url = "https://graph.microsoft.com/beta/applications(appId='$AppID')/owners"
$Output = Invoke-TryCatchLog -InfoLog "Retrieve Azure Application $AppID Owner" -LogToFile $LogToFile -ScriptBlock {
Invoke-RestMethod -Method GET -Uri $Url -Headers $azureGraphAuthenticationHeader
}
return $Output
}
}
function Get-EndpointManagerDevice {
param(
[parameter(mandatory)]
$serialNumber,
[parameter(mandatory)]
[ValidateSet("True","False")]
$LogToFile
)
if (Find-AzureGraphAPIConnection) {
if (IsNotNULL($serialNumber)) {
$EndpointDevice = Invoke-TryCatchLog -InfoLog "Retrieving device $DeviceName in EndpointManager" -LogToFile $LogToFile -ScriptBlock {
$Uri = "https://graph.microsoft.com/beta/deviceManagement/managedDevices" + '?$filter='+ "serialNumber eq '$DeviceName'"
Invoke-RestMethod -Method GET -Uri $Uri -Headers $azureGraphAuthenticationHeader -ContentType "application/json"
}
return $EndpointDevice.value
}
}
}
Function Remove-EndpointManagerDevice {
param(
[parameter(mandatory)]
$serialNumber,
[parameter(mandatory)]
[ValidateSet("True","False")]
$LogToFile
)
if (Find-AzureGraphAPIConnection) {
$EndpointDevice = Get-EndpointManagerDevice -serialNumber $serialNumber -LogToFile $LogToFile
Invoke-TryCatchLog -InfoLog "Deleting device $DeviceName - $($EndpointDevice.id) in EndpointManager" -LogToFile $LogToFile -LogType DELETE -ScriptBlock {
Invoke-RestMethod -Method DELETE -Uri "https://graph.microsoft.com/beta/deviceManagement/managedDevices/$($EndpointDevice.id)" -Headers $azureGraphAuthenticationHeader -ContentType "application/json"
}
}
}