0

@BenH 和 @TheMadTechnician 在帮助我编写脚本以从特定 AD 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 $_
    }
} 

我的问题 - 我可以通过向第一个循环添加变量和“Where-Object”逻辑来强制脚本仅对过期超过 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为 a-lt以获得正确的日期范围?

感谢您的关注!

4

3 回答 3

1

按要求作出答复。问题在于Where声明:

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

虽然从逻辑上讲,“帐户在 30 多天前过期的地方”听起来是正确的,但实际上是“帐户过期的日期大于 30 天前的日期”。当您认为某些系统将日期测量为自 Unix 纪元(UTC 时间 1970 年 1 月 1 日凌晨 12:00:00)以来经过的秒数时,日期会转换为整数,因此-gt操作员选择稍后发生的日期更有意义自纪元以来已经过去了更多的秒数,并且整数是一个更大的数字。

如果你改变它-gt-lt它会完成你正在寻找的东西。此外,添加-and $_.AccountExpirationDate它可确保AccountExpirationDate不为空。所以我们最终得到:

Where-Object {$_.AccountExpirationDate -lt $30DaysOld -and $_.AccountExpirationDate}
于 2017-06-13T17:44:02.880 回答
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) {
  $distrosremoved=@()

  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}
      }
  }
}  
于 2017-07-10T18:25:33.757 回答
0

@TheMadTechnician 搞定了——我需要将嵌套的“where”语句更改为:

 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 将提交他们的评论作为答案,这样我就可以更新并在 karma 到期的地方给出 karma!如果这解决了您正在寻找的问题,请支持 TheMadTechnician 的评论!

于 2017-06-12T21:01:26.597 回答