-
Notifications
You must be signed in to change notification settings - Fork 2
/
DeleteGroup-PowerShell-Azure-Function.ps1
93 lines (75 loc) · 2.66 KB
/
DeleteGroup-PowerShell-Azure-Function.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
# DeleteGroup.ps1
# deletes an existing Office 365 group, by atwork.at, Toni Pohl, Martina Grom
# POST with body:
# {
# "GroupName" : "My Test 20"
# }
$requestBody = Get-Content $req -Raw | ConvertFrom-Json
$GroupName = $requestBody.GroupName
Write-Output "TenantID: $env:TenantID AppID: $env:AppID AppSecret: $env:AppSecret"
Write-Output "GroupName to delete: $GroupName"
function Initialize-Authorization {
param
(
[string]
$ResourceURL = 'https://graph.microsoft.com',
[string]
[parameter(Mandatory)]
$TenantID,
[string]
[Parameter(Mandatory)]
$ClientKey,
[string]
[Parameter(Mandatory)]
$AppID
)
$Authority = "https://login.windows.net/$TenantID/oauth2/token"
[Reflection.Assembly]::LoadWithPartialName("System.Web") | Out-Null
$EncodedKey = [System.Web.HttpUtility]::UrlEncode($ClientKey)
$body = "grant_type=client_credentials&client_id=$AppID&client_secret=$EncodedKey&resource=$ResourceUrl"
# Request a Token from the graph api
$result = Invoke-RestMethod -Method Post `
-Uri $Authority `
-ContentType 'application/x-www-form-urlencoded' `
-Body $body
$script:APIHeader = @{'Authorization' = "Bearer $($result.access_token)" }
}
# Initialize Authorization
Initialize-Authorization -TenantID $env:TenantID -ClientKey $env:AppSecret -AppID $env:AppID
Write-Output "Initialize-Authorization..."
# Delete the Office 365 group
$uri = @"
https://graph.microsoft.com/v1.0/groups?`$select=id&`$filter=startswith(displayname,'$($GroupName)')
"@
try {
$result = Invoke-RestMethod -Method GET `
-Uri $uri `
-ContentType 'application/json' `
-Headers $script:APIHeader `
-ErrorAction Stop
# and get the Group ID
$GroupID = $result.value.id
Write-Output "GroupID: $GroupID"
} catch {
Write-Output "ERROR! $_"
}
if ($GroupID) {
$theresult = "DELETED: $GroupName, $GroupID found and deleted"
try {
$result = Invoke-RestMethod -Method DELETE `
-Uri "https://graph.microsoft.com/v1.0/groups/$GroupID" `
-ContentType 'application/json' `
-Headers $script:APIHeader `
-ErrorAction Stop
# last operation
Write-Output "GroupID: $result"
} catch {
Write-Output "ERROR! $_"
}
}
else
{
$theresult = "NOTFOUND: $GroupName not found"
}
Out-File -Encoding Ascii -FilePath $res -inputObject $theresult
Write-Output $theresult