2017-06-05 23 views
0

@BenH和@TheMadTechnician非常有助于协助我使用脚本,以便从特定AD OU中的用户删除发布列表(仅限于)。我忘了添加所需的标准,因此决定张贴此作为一个单独的问题(原来的线程here针对两个OU中的所有用户并删除通讯组列表 - 添加日期条件

@ BenH的做法是这样的:

$OUs = 'OU=PendingDeletion,OU=Users,DC=Stuff,DC=Place,DC=net','OU=HoldForReview,OU=Users,DC=Stuff,DC=Place,DC=net' 
$Users = ForEach ($OU in $OUs) { 
    Get-ADUser -Filter * -SearchBase $OU 
} 

ForEach ($User in $Users) { 
    Get-ADPrincipalGroupMembership -Identity $user | 
    Where-Object {$_.GroupCategory -eq 0} | 
    ForEach-Object { 
     Remove-ADPrincipalGroupMembership -Identity $user -MemberOf $_ 
    } 
} 

我的问题 - 我可以迫使脚本只需要对已超过30天以前,通过增加一个变量,“位置对象”的逻辑来第一个循环是这样?:

$OUs = 'OU=PendingDeletion,OU=Users,DC=Stuff,DC=Place,DC=net','OU=HoldForReview,OU=Users,DC=Stuff,DC=Place,DC=net' 
    $30DaysOld = (Get-Date).AddDays(-30) 

    $Users = ForEach ($OU in $OUs) { 
     Get-ADUser -Filter * -SearchBase $OU | 
     Where-Object {$_.AccountExpirationDate -gt $30DaysOld}} 

    ForEach ($User in $Users) { 
    Get-ADPrincipalGroupMembership -Identity $user | 
    Where-Object {$_.GroupCategory -eq 0} | 
    ForEach-Object { 
     Remove-ADPrincipalGroupMembership -Identity $user -MemberOf $_ 
    } 
} 

可能过期的帐户行动?或者我需要将-gt更改为-lt才能获得正确的日期范围?

感谢您的期待!

+3

你的问题是“我写过这段代码,它工作吗?”?那么......它工作吗?它能让你获得你期望的账户吗? – TessellatingHeckler

+1

你会想要'-lt'。下面是一个应该说明的比较声明:'(Get-Date).AddDays(-30)-gt(Get-Date).AddDays(-40)'​​是'$ True'。您可以将'-WhatIf'添加到您的'Remove-ADPrincipalGroupMembership'中,以查看该命令将尝试执行的内容。 – BenH

+0

@TessellatingHeckler - 坚实的点,对不起,我不是更清楚。经过审查,我的问题不是很好。 第一个循环不会收集正确的用户;检查变量'$ Users'的内容,AccountExpirationDate显示空白。我将该行更改为: 'Get-ADUser -Filter * -Properties AccountExpirationDate -SearchBase $ OU' 现在我看到在AD帐户中列出的Exp Date,但使用'-lt'返回所有用户或AD中没有过期日期,并且'-gt'不返回任何用户。 – 3Jake

回答

1

根据要求做出答案。问题是与Where声明:

Where-Object {$_.AccountExpirationDate -gt $30DaysOld} 

虽然在逻辑上是听起来是正确的“其中帐户过期30多天前”它实际上就出来“,其中该帐户过期日期为大于30天前的日期'。如果您认为某些系统测量日期为自Unix纪元(1970年1月1日上午12:00:00 UTC)以来秒数已过,并且日期转换为整数,并且更有意义的是-gt运算符选择发生的日期后来按时间顺序排列,因为从纪元开始已经过去了更多的秒数,整数是一个更大的数字。

如果您将-gt更改为-lt它会完成您要查找的内容。另外,向其添加-and $_.AccountExpirationDate以确保AccountExpirationDate不为空。因此,我们最终用:

Where-Object {$_.AccountExpirationDate -lt $30DaysOld -and $_.AccountExpirationDate} 
0

@TheMadTechnician钉它 - 我需要改变嵌套“其中”声明:

Where-Object {$_.AccountExpirationDate -lt $30DaysOld -and $_.AccountExpirationDate} 

所以运行代码:

$30DaysOld = (Get-Date).AddDays(-30) 
$OUs = 'OU=PendingDeletion,OU=Users,DC=Stuff,DC=Place,DC=net','OU=HoldForReview,OU=Users,DC=Stuff,DC=Place,DC=net' 

$Users = ForEach ($OU in $OUs) { 
    Get-ADUser -Filter * -Properties AccountExpirationDate -SearchBase $OU | 
    Where-Object {$_.AccountExpirationDate -lt $30DaysOld -and $_.AccountExpirationDate} 
    } 

ForEach ($User in $Users) { 
    Get-ADPrincipalGroupMembership -Identity $user | 
    Where-Object {$_.GroupCategory -eq 0} | 
    ForEach-Object { 
     Remove-ADPrincipalGroupMembership -Identity $user -MemberOf $_ -Confirm:$false 
    } 
} 

希望TheMadTechnician将提交他们的意见作为一个答案所以我可以更新并给出因果报应的业力!如果这解决了您正在寻找的问题,请致电TheMadTechnician的评论!

0

OP这里 - 我一直在继续这方面的工作(与援助),并补充说,我想其他人可能觉得有用的一些额外的装饰,所以我想分享回来。

请不要高举这个问题,TheMadTechnician和BenH值得所有的信誉打破这个背面。如果你觉得这个很有用,那么请回答这个问题。

现在将写入已删除的发行版的名称到AD帐户,使用分号分隔符(如果您需要重新添加发行版,请剪切/粘贴名称),并且不会将混乱添加到AD帐户如果它不止一次针对帐户运行。

享受!

# Variables 

$30DaysOld = (Get-Date).AddDays(-30) 
$OUs = (
    'OU=PendingDeletion,OU=Users,DC=Stuff,DC=Place,DC=net', 
    'OU=HoldForReview,OU=Users,DC=Stuff,DC=Place,DC=net' 
) 

# Collect the needed users 

$Users = ForEach ($OU in $OUs) { 
    Get-ADUser -Filter * -Properties AccountExpirationDate,info -SearchBase $OU | 
    Where-Object {$_.AccountExpirationDate -lt $30DaysOld -and $_.AccountExpirationDate} 
} 

# Collect each user's Distro Lists & REMOVE 

ForEach ($User in $Users) { 
    [email protected]() 

    Get-ADPrincipalGroupMembership -Identity $user | 
    Where-Object {$_.GroupCategory -eq "distribution"} | 
    ForEach-Object { 
    $distrosremoved+=$_.name 
    Remove-ADPrincipalGroupMembership -Identity $user -MemberOf $_ -Confirm:$false 
    } 

    # Collect info from the Telephone > Notes field, and ADD the list of Distros into the existing info 

    if($distrosremoved){ 

     $distro_str="Removed Distro Lists: `r`n"+($distrosremoved -join "; ") 

     if ($user.info){ 
     $newinfo=$user.info+"`r`n"+$distro_str 
     Set-ADUser $user -Replace @{info=$newinfo} 
     }else{ 
     $newinfo=$distro_str 
     Set-ADUser $user -Add @{info=$distro_str} 
     } 
    } 
} 
相关问题