forked from joerod/powershell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GAL_cleanup.ps1
54 lines (36 loc) · 1.3 KB
/
GAL_cleanup.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
<#
.SYNOPSIS
This script looks for accounts in AD that are disabled, then checkes Exchange to see if the account is still visable
in the GAL.
.SYNTAX
Gal_cleanup.ps1 [-Remove]
.EXAMPLE
-------------------------- EXAMPLE 1 --------------------------
PS C:\>create_admin_account.ps1
This will show all accounts that are disabled in AD but still visable in the GAL.
-------------------------- EXAMPLE 2 --------------------------
PS C:\>create_admin_account.ps1 -remove
This will remove all accounts that are disabled in AD but still visable in the GAL.
#>
[CmdletBinding()]
param([switch] $Remove)
function GAL_Cleanup{
foreach($user in (Get-ADUser -Properties * -Filter {Enabled -eq $false})){
try{
if((get-Mailbox $($user.SamAccountName) -ErrorAction Stop | select -expand HiddenFromAddressListsEnabled) -eq $false){
New-Object -TypeName PSCustomObject -Property @{
SAMAccoutName = $user.samaccountname
Name = $user.name
}
if($remove){
Set-Mailbox $user.SamAccountName -HiddenFromAddressListsEnabled $true
Write-Output "$($user.SamAccountName) has been removed from the GAL"
}
}
}
catch {
Write-Verbose "There is no mailbox for $($user.DisplayName)"
}
}
}
GAL_Cleanup