我正在研究AD清理脚本。我想要发生的是脚本每个月运行一次,并将该月的所有禁用帐户移动到禁用OU下的OU中,并用日期标记它。在大多数情况下它是有效的。我想帮助添加逻辑,不要尝试创建具有相同日期的新OU。有没有人有任何想法?
$date=get-date -Format o
$date=$date.Split("T")
$date=$date[0]
New-ADOrganizationalUnit -Name $date -Path "OU=Disabled, DC=mydomain, DC=net" -ErrorAction SilentlyContinue
$disabled = (get-aduser -filter * -properties lastlogondate | Where-Object
{$_.enabled -eq $false -and $_.lastlogondate -lt (get-date).adddays(-90)}
|select Name).name
foreach($user in $disabled)
{
Get-ADUser $user | Move-ADObject -TargetPath "OU=$date, OU=Disabled,
DC=mydomain, DC=net" -ErrorAction SilentlyContinue
}
答案 0 :(得分:1)
由于@lit已经开始,我会这样做:
$existingOU = Get-AdOrganizationalUnit -filter { name -eq "$date" };
if($null -eq $existingOU) {
"....do your New-AdOrgani.... cmdlet execution here...." }
我希望这会有所帮助并且有一个很棒的1: - 。
答案 1 :(得分:0)
我提出的这项工作确实有效,但我也会尝试一下。谢谢你的帖子。
$date=get-date -Format o
$date=$date.Split("T")
$date=$date[0]
$disabled = (get-aduser -filter * -properties lastlogondate | Where-Object {$_.enabled -eq $false -and $_.lastlogondate -lt (get-date).adddays(-90)} |select Name).name
try {
New-ADOrganizationalUnit -Name $date -Path "OU=Disabled, DC=mydomain, DC=net"
} catch {
#If we want another action
}
foreach($user in $disabled)
{
Get-ADUser $user | Move-ADObject -TargetPath "OU=$date, OU=Disabled, DC=mydomain, DC=net" -ErrorAction SilentlyContinue
}