-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCIPP-API_Common Mailbox Forwarding and OOO.ps1
69 lines (52 loc) · 2.47 KB
/
CIPP-API_Common Mailbox Forwarding and OOO.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
#$debug = $true
### Authentication
$CIPPAPIUrl = Read-host -Prompt "Enter CIPP API URL"
$ApplicationId = Read-host -Prompt "Enter app ID"
$ApplicationSecret = Read-Host -Prompt "Enter app secret" -MaskInput
$TenantId = Read-Host -Prompt "Enter tenant ID"
$AuthBody = @{
client_id = $ApplicationId
client_secret = $ApplicationSecret
scope = "api://$($ApplicationId)/.default"
grant_type = 'client_credentials'
}
$token = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$TenantId/oauth2/v2.0/token" -Method POST -Body $AuthBody
$AuthHeader = @{ Authorization = "Bearer $($token.access_token)" }
### Begin Script
$commonName = Read-Host -Prompt "Enter mailbox common name. Example: {tenantuser}@contoso.org"
$forwardToMailbox = Read-Host "Enter forward to mailbox. Example: {mytenants}@example.com"
$forwardToDomain = Read-Host "Enter forward to domain. Example: mytenants@{example.com}"
$oooMessage = Read-Host "Enter OOO Message"
#Get all accounts with commonName
$accounts = Invoke-RestMethod -Uri "$CIPPAPIUrl/api/listusers?graphfilter=startswith(userPrincipalName,$commonName)&TenantFilter=AllTenants" -Method GET -Headers $AuthHeader -ContentType "application/json"
foreach ($account in $accounts){
### Mailbox Forwarding
#Get plus alias destination from the tenant name.
$tenantShortName = $account.Tenant.Substring(0,$account.Tenant.IndexOf("."))
#Create request body
$forwardBody = @{
tenantfilter = $account.Tenant
userid = $account.userPrincipalName
disableforwarding = 'false'
keepcopy = 'true'
forwardexternal = $forwardToMailbox+'+'+$tenantShortName+'@'+$forwardToDomain
}
#Convert to JSON
$forwardBody = (ConvertTo-Json -InputObject $forwardBody)
#Execute forwarding
Invoke-RestMethod -Uri "$CIPPAPIUrl/api/execemailforward" -Method POST -Body $forwardBody -Headers $AuthHeader -ContentType "application/json"
if($debug){pause}
### Mailbox OOO
#Create request body
$oooBody = @{
tenantfilter = $account.Tenant
user = $account.userPrincipalName
autoreplystate = 'enabled'
internalmessage = $oooMessage
}
#Convert to JSON
$oooBody = (ConvertTo-Json -InputObject $oooBody)
#Execute OOO
Invoke-RestMethod -Uri "$CIPPAPIUrl/api/execsetooo" -Method POST -Body $oooBody -Headers $AuthHeader -ContentType "application/json"
if($debug){pause}
}