删除禁用的帐户,因为基于定制90天属性值

问题描述:

我自动移动在OU中所有的广告被停用的帐户将停用的日期extensionattribute4这个脚本:删除禁用的帐户,因为基于定制90天属性值

import-module activedirectory 
$timer = (Get-Date) 
$TargetOU = "OU=Disabled Accounts,DC=domain,DC=lan" 
$DisabledAccounts = get-aduser -filter { enabled -eq $false } -SearchBase "OU=Test,OU=EMEA,DC=domain,DC=lan" 

ForEach ($account in $DisabledAccounts) { 
set-aduser -Identity $account.distinguishedName -add @{extensionAttribute4="$timer"} 
} 

ForEach ($account in $DisabledAccounts) { 
Move-ADObject -Identity $account.distinguishedName -TargetPath $TargetOU 

但是,当我想删除广告被停用的帐户与参考的extensionattribute4更少日起90日内与该脚本:

import-module activedirectory 
$DaysInactive = 90 
$time = (Get-Date).Adddays(-($DaysInactive)) 
$DisabledAccounts = get-aduser -filter { extensionattribute4 -lt $time -and enabled -eq $false } -SearchBase "OU=Disabled Accounts,DC=domain,DC=lan" 

ForEach ($account in $DisabledAccounts) { 
Remove-ADObject -Identity $account.distinguishedName 
} 

我已经得到了一个错误:

get-aduser : Invalid type 'System.DateTime'. 
Parameter name: extensionattribute4 
At C:\removedisabledadaccounts.ps1:4 char:21 
+ $DisabledAccounts = get-aduser -filter { extensionattribute4 -lt $time -and enab ... 
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
    + CategoryInfo   : InvalidArgument: (:) [Get-ADUser], ArgumentException 
    + FullyQualifiedErrorId : Invalid type 'System.DateTime'. 
Parameter name: extensionattribute4,Microsoft.ActiveDirectory.Management.Commands.GetADUser 

该错误表示您正在尝试执行该属性不接受的操作。当您在早期操作中填充字段时,将日期转换为一个字符串,其中包含@{extensionAttribute4="$timer"}。无论如何,我无法想象这些属性存储为除字符串以外的任何其他属性。实际上,试图存储日期对象以类似的失败告终。

荣誉使用-Filter,但我相信这是超出-Filter/-LDAPFilter的东西,所以你应该只需要做一些后处理。

Get-ADUser -Filter {enabled -eq $false} -SearchBase "OU=Disabled Accounts,DC=domain,DC=lan" -Properties extensionattribute4 | 
    Where-Object{$time -ge $_.extensionattribute4} 

因为我们需要,我们需要确保它在-Properties列表中返回该属性的工作。

+0

非常感谢你的工作! – puffydee