【问题标题】:What is the dsCorePropagation attribute and why does my script use it?什么是 dsCorePropagation 属性,为什么我的脚本使用它?
【发布时间】:2017-04-06 21:49:13
【问题描述】:

所以我有一个脚本,我在其中查询 LastLogonTimeStamp 属性并在两个条件下删除帐户,一个是它们属于两个安全组,另一个是它们在 75 天内没有登录到计算机。以下是我的脚本:

$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 = ""
    }
}

所以我运行脚本,对吗?两个组中都有 3 个用户,对吗?然后有一个用户低于 75 天标记,一个用户高于 75 天标记,还有一个用户从未登录,因此在 AD 对象中显示“<not set>”。现在,当我的脚本比较用户从未登录的 $LastLogonDate 变量时,它显示日期“1601 年 1 月 1 日上午 4:30:00”。现在,我认为这是某种默认值,当我猜该值为 null 或 0 时,但我仔细查看了 ADUC 对象,当我在 Powershell 中查询它时,它在“dsCorePropagation”属性中显示了该日期,它显示了日期,但是当我查看 AD 对象上的该属性时,它包含“0”的十六进制代码。

我的脚本按原样工作是因为我将 $fullADUser.LastLogonTimeStamp 与 null 进行比较,我只是想更深入地了解为什么 $LastLogonDate 变量不为 null 因为 $fullADUser.LastLogonTimeStamp 为 null 会很有趣。

【问题讨论】:

    标签: powershell active-directory


    【解决方案1】:

    因为 System.DateTime 是不可为空的类型,所以如果您向 FromFileTime 传递一个 null 或零值,它只会返回最小的 FileTime 值,即 01/01/1601

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

    如果您希望 DateTime 可以为空,则必须使用 Nullable DateTime

    【讨论】:

    • 是的,我做了更多的研究,终于弄明白了。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2017-01-10
    • 2011-02-21
    • 2022-12-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多