在这里输入图像描述,我需要删除那些进入一个特殊OU的用户,检查是在这个OU中已经超过6个月了,删除他们。我不知道我是否正确,但我使用Whenchanged属性,也使用Get-ADgroupmemberDate,但是我的脚本运行良好,直到在OU或组中有一个用户,一旦我有两个以上的用户,就会出现这个错误。
无法将“System.String[]”转换为“System.DateTime”类型
Get-ADUser -Filter * -SearchBase 'OU=testOU,DC=fire,DC=cloud' -Properties whenChanged | ForEach {
$userinfo = Get-ADUser -filter {Enabled -eq $True} -SearchBase 'OU=testOU,DC=fire,DC=cloud' -properties whenChanged | Sort-Object DisplayName | Select-Object DisplayName, whenChanged
$today = Get-Date -Format "yyyy-MM-dd"
$userData = $userinfo.Whenchanged
$convert = ([string]$userData.ToshortDatestring()).Split()
$result = (New-TimeSpan -Start $convert -End $today).Days
If ($result -ge 10) {
#Get-ADUser -Filter * -SearchBase "OU=testOU,DC=fire,DC=cloud" | Disable-ADAccount
#Get-ADUser -filter * -searchbase "OU=testOU,DC=fire,DC=cloud" | where-object { $_.Enabled -eq $false } | remove-ADobject -Confirm:$false
"Hello"
}
}发布于 2019-09-20 13:24:21
您已经结束了使用Get-ADUser cmdlet的操作。
如果我正确地理解了这个问题,您会想得到一个列表,列出OU中已经6个月没有修改过的所有用户对象(尽管您的代码建议为10天)。
然后,如果启用了该用户对象,则需要禁用它,如果用户对象已被禁用,则需要删除它。
在这种情况下,这应该可以做到:
$refDate = (Get-Date).AddMonths(-6) # test for users that have been in this OU for more than 6 months
Get-ADUser -Filter * -SearchBase 'OU=testOU,DC=fire,DC=cloud' -Properties DisplayName, Modified | ForEach-Object {
# iterating over this user collection means that in each run, the $_ automatic variable contains a single user
if ($_.Modified -lt $refDate) {
if ($_.Enabled) {
Write-Host "Disabling user $($_.DisplayName)" -ForegroundColor Yellow
$_ | Disable-ADAccount
}
else {
Write-Host "Deleting user $($_.DisplayName)" -ForegroundColor Magenta
$_ | Remove-ADUser
}
}
}
多亏了每日。如果需要将日期与X个月前的午夜进行比较(基本上省略日期的时间部分),请将$refDate的声明更改为
$refDate = (Get-Date).Date.AddMonths(-6)并将测试更改为
if ($_.Modified.Date -lt $refDate) {根据您为被上述函数取消/删除的所有用户编写CSV的注释,您可以使用以下方法实现该功能:
$today = Get-Date
$refDate = $today.Date.AddMonths(-6) # test for users that have been in this OU for more than 6 months
$result = Get-ADUser -Filter * -SearchBase 'OU=testOU,DC=fire,DC=cloud' -Properties DisplayName, Modified | ForEach-Object {
# iterating over this user collection means that in each run, the $_ automatic variable contains a single user
if ($_.Modified.Date -lt $refDate) {
# create an object with some details about the user for export to CSV
$out = [PsCustomObject]@{
'UserName' = $_.DisplayName
'AccountName' = $_.SamAccountName
'Date' = $today
'Action' = ''
}
if ($_.Enabled) {
Write-Host "Disabling user $($_.DisplayName)" -ForegroundColor Yellow
$out.Action = 'Disabled'
$_ | Disable-ADAccount
}
else {
Write-Host "Deleting user $($_.DisplayName)" -ForegroundColor Magenta
$out.Action = 'Deleted'
$_ | Remove-ADUser
}
# emit the PsCustomObject so it gets collected in variable $result
$out
}
}
if ($result) {
# output the $result collection to screen
$result
# output to a CSV file
$result | Export-Csv -Path 'Ex-users.csv' -NoTypeInformation
}
else { "No users were deleted or disabled" }https://stackoverflow.com/questions/58028969
复制相似问题