当员工因任何原因离开我们的组织时,目前我们会禁用他们的 AD 帐户,但不会立即将其删除。但是,问题在于这些用户仍然显示在全局地址列表中。
我确定有一个 PowerShell 脚本可以删除它们,但我想让事情变得更加简化。
我希望这里的某个人可能能够提供一种更好的方法来禁用用户,这将在此过程中自动将他们从 GAL 中删除。
到目前为止,我可以想到两种可能的解决方案。
创建一个每小时运行一个 PS 脚本的脚本,该脚本将从 GAL 中删除禁用的用户。
使用将同时禁用用户并将其从 GAL 中删除的 PS 命令。
选项 2 可能是更好的选择,所以如果有人可以提供帮助,我将不胜感激。
提前致谢。
无需重新发明轮子,在petri.co.il 上找到了这个优雅的解决方案:
# http://www.petri.co.il/forums/showthread.php?p=109975
# usage: Disable-User [accountname] [enable/disable]
function get-dn ($SAMName) {
$root = [ADSI]''
$searcher = new-object System.DirectoryServices.DirectorySearcher($root)
$searcher.filter = "(&(objectClass=user)(sAMAccountName= $SAMName))"
$user = $searcher.findall()
if ($user.count -gt 1) {
$count = 0
foreach($i in $user) {
write-host $count ": " $i.path
$count = $count + 1
}
$selection = Read-Host "Please select item: "
return $user[$selection].path
} else {
return $user[0].path
}
}
$Name = $args[0]
$status = $args[1]
$path = get-dn $Name
if ($path -ne $null) {
"'" + $path + "'"
if ($status -match "enable") {
# Enable the account
$account=[ADSI]$path
$account.psbase.invokeset("AccountDisabled", "False")
$account.setinfo()
Set-Mailbox "$Name" -HiddenFromAddressListsEnabled $False
} else {
# Disable the account
$account=[ADSI]$path
$account.psbase.invokeset("AccountDisabled", "True")
$account.setinfo()
Set-Mailbox "$Name" -HiddenFromAddressListsEnabled $True
}
} else {
write-host "No user account found!" -foregroundcolor white -backgroundcolor red
}
Run Code Online (Sandbox Code Playgroud)
另存为Disable-User.ps1
并运行.\Disable-User.ps1 SAMaccountname disable
小智 6
在 Exchange 管理控制台中使用 Quest AD PowerShell Commandlet 时,有一种比上述解决方案更简单的解决方案,可以在两行代码中完成。
param(
[string]$username = $(throw "A user ID is required.") #throw exception if no value provided
)
Disable-QADUser -Identity $username -service "dc.domain.local:389"
Set-Mailbox -identity "domain\$username" -HiddenFromAddressListsEnabled $true -DomainController "dc.domain.local"
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
20267 次 |
最近记录: |