2017-06-05 40 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} 
     } 
    } 
} 
相關問題