2017-04-05 405 views
0

因此,我有一個腳本,用於在兩種情況下查詢LastLogonTimeStamp屬性和刪除帳戶,其中一個是屬於兩個安全組,另一個是他們在75天內沒有登錄到計算機。下面是我的腳本:什麼是dsCorePropagation屬性,爲什麼我的腳本使用它?

$75DayExemptGroup = Get-ADGroup -Identity "AccountComplianceExemption_75DayLimit" | Select -exp DistinguishedName 
$AccountInactvityGroupMembers = Get-ADGroup -Identity "AccountComplianceExemption_AccountInactivity" -Properties Members | Select -ExpandProperty Members 

foreach ($UserDN in $AccountInactvityGroupMembers) { 
$fullADUser = Get-ADUser $UserDN -properties LastLogonTimeStamp,MemberOf,Description,CanonicalName -ErrorAction SilentlyContinue | Where {$_.DistinguishedName -notlike "*MAB_Devices*" -and $_.DistinguishedName -notlike "*Mailboxes*" -and $_.DistinguishedName -notlike "*AFG TNOSC*" -and $_.Name -notlike "svc.*" -and $_.Name -notlike "grp.*"} 
if ($fullADUser.MemberOf -like "*$75DayExemptGroup*") { 
    $LastLogonDate = [DateTime]::FromFileTime($fullADUser.LastLogonTimeStamp) 
    $75Days = [datetime]::Today.AddDays(-75) 
    if ($LastLogonDate -lt $75Days -and $fullADUser.LastLogonTimeStamp -ne $null) 
    { 
     $OrigDesc = $fullADuser.Description 
     $OUPath = $fullADuser.CanonicalName 
     $NewDesc = "Deleted by JNCC-A for 75 days of inactivity - Original Desc [$OrigDesc] - OU: $OUPath" 
     Set-ADUser $fullADuser -Description $NewDesc 
     if ($?) {Remove-ADUser $fullADUser -Confirm $false} 
    } 
    $LastLogonDate = "" 
    } 
} 

所以我運行腳本,對吧?有兩個用戶在兩個組中,對嗎?然後有一個用戶低於75天標記,一個高於75天標記,然後是一個從未登錄過的用戶,因此在AD對象中顯示「<not set>」。現在,當我的腳本比較用戶的$ LastLogonDate變量從未登錄時,它顯示日期「1601 1月1日4:30:00 AM」。現在,我認爲這是某種默認情況下,當值爲null或0我猜,但我仔細觀察了ADUC對象,並在「dsCorePropagation」屬性中顯示該日期,當我在Powershell中查詢它時,它顯示日期,但是當我查看AD對象上的該屬性時,它包含「0」的十六進制代碼。

我的腳本按照原樣運行,因爲我將$ fullADUser.LastLogonTimeStamp與null比較,我只是認爲獲取更多視角會更有趣,因爲$ LastLogonDate變量不爲null,因爲$ fullADUser.LastLogonTimeStamp爲null。

回答

0

由於System.DateTime的是一個非空類型,因此,如果您通過FromFileTime空或零值,它只會返回最小FILETIME值,這是1601年1月1日

C:\WINDOWS\system32> [datetime]::FromFileTime($null) 
01 January 1601 00:00:00 

如果您想要一個DateTime爲空,你必須使用Nullable DateTime

+0

是的,我做了一些更多的研究,並最終弄清楚了它。謝謝! – Joseph

相關問題