-
Notifications
You must be signed in to change notification settings - Fork 0
/
action.ps1
77 lines (74 loc) · 4.75 KB
/
action.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
# HelloID-Task-SA-Target-ExchangeOnPremises-DistributionGroupGrantMembership
############################################################################
# Form mapping
$formObject = @{
GroupIdentity = $form.GroupIdentity
UsersToAdd = [array]$form.Users
}
[bool]$IsConnected = $false
try {
$adminSecurePassword = ConvertTo-SecureString -String $ExchangeAdminPassword -AsPlainText -Force
$adminCredential = [System.Management.Automation.PSCredential]::new($ExchangeAdminUsername, $adminSecurePassword)
$sessionOption = New-PSSessionOption -SkipCACheck -SkipCNCheck
$exchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $ExchangeConnectionUri -Credential $adminCredential -SessionOption $sessionOption -Authentication Kerberos -ErrorAction Stop
$null = Import-PSSession $exchangeSession -DisableNameChecking -AllowClobber -CommandName 'Add-DistributionGroupMember'
$IsConnected = $true
foreach ($user in $formObject.UsersToAdd) {
try {
Write-Information "Executing ExchangeOnPremises action: [DistributionGroupGrantMembership] for: [$($formObject.GroupIdentity)]"
$null = Add-DistributionGroupMember -Identity $formObject.GroupIdentity -Member $user.UserPrincipalName -Confirm:$false -ErrorAction Stop
$auditLog = @{
Action = 'GrantMembership'
System = 'ExchangeOnPremises'
TargetIdentifier = $formObject.GroupIdentity
TargetDisplayName = $formObject.GroupIdentity
Message = "ExchangeOnPremises action: [DistributionGroupGrantMembership] user [$($user.UserPrincipalName)] to: [$($formObject.GroupIdentity)] executed successfully"
IsError = $false
}
Write-Information -Tags 'Audit' -MessageData $auditLog
Write-Information "ExchangeOnPremises action: [DistributionGroupGrantMembership] user [$($user.UserPrincipalName)] to: [$($formObject.GroupIdentity)] executed successfully"
} catch {
$ex = $_
if ($ex.CategoryInfo.Reason -eq 'MemberAlreadyExistsException') {
$auditLog = @{
Action = 'GrantMembership'
System = 'ExchangeOnPremises'
TargetIdentifier = $formObject.GroupIdentity
TargetDisplayName = $formObject.GroupIdentity
Message = "ExchangeOnPremises action: [DistributionGroupGrantMembership] user [$($user.UserPrincipalName)] to: [$($formObject.GroupIdentity)] executed successfully"
IsError = $false
}
Write-Information -Tags 'Audit' -MessageData $auditLog
Write-Information "ExchangeOnPremises action: [DistributionGroupGrantMembership] user [$($user.UserPrincipalName)] to: [$($formObject.GroupIdentity)] executed successfully"
} else {
$auditLog = @{
Action = 'GrantMembership'
System = 'ExchangeOnPremises'
TargetIdentifier = $formObject.GroupIdentity
TargetDisplayName = $formObject.GroupIdentity
Message = "Could not execute ExchangeOnPremises action: [DistributionGroupGrantMembership] user [$($user.UserPrincipalName)] to: [$($formObject.GroupIdentity)], error: $($ex.Exception.Message)"
IsError = $true
}
Write-Information -Tags 'Audit' -MessageData $auditLog
Write-Error "Could not execute ExchangeOnPremises action: [DistributionGroupGrantMembership] user [$($user.UserPrincipalName)] to: [$($formObject.GroupIdentity)], error: $($ex.Exception.Message)"
}
}
}
} catch {
$ex = $_
$auditLog = @{
Action = 'GrantMembership'
System = 'ExchangeOnPremises'
TargetIdentifier = $formObject.GroupIdentity
TargetDisplayName = $formObject.GroupIdentity
Message = "Could not execute ExchangeOnPremises action: [DistributionGroupGrantMembership] to: [$($formObject.GroupIdentity)], error: $($ex.Exception.Message)"
IsError = $true
}
Write-Information -Tags 'Audit' -MessageData $auditLog
Write-Error "Could not execute ExchangeOnPremises action: [DistributionGroupGrantMembership] to: [$($formObject.GroupIdentity)], error: $($ex.Exception.Message)"
} finally {
if ($IsConnected) {
Remove-PSSession -Session $exchangeSession -Confirm:$false -ErrorAction Stop
}
}
############################################################################