-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpurge.ps1
37 lines (26 loc) · 948 Bytes
/
purge.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
$usernames = 'wcoblog','esreq'
$session = New-PSSession -ConfigurationName Microsoft.Exchange `
-ConnectionUri https://outlook.office365.com/powershell-liveid/ -AllowRedirection `
-Authentication Basic -Credential "${env:UserName}@uark.edu"
Import-PSSession $session -allowclobber
echo ""
foreach ($username in $usernames) {
$distname = $((Get-Recipient -Identity ${username}@uark.edu).DistinguishedName)
if (!($distname)) {continue}
$groups = Get-DistributionGroup -Filter "
Name -like 'WCOB:*'
-and Members -eq '$distname'
"
if (!($groups)) {
echo "No groups found for $username"
continue
}
foreach ($group in $groups) {
echo "Removing $username from $group"
Remove-DistributionGroupMember -Identity $group.Identity `
-Member "$distname" `
-Confirm:$false
}
}
Remove-PSSession $session
Read-Host "Press any key to exit..."