如何获取AD组的已启用和未过期帐户成员的数量

时间:2019-01-10 10:11:01

标签: powershell active-directory

我需要获取AD组已启用和未过期的帐户数。我可以启用已启用的帐户,但是那些过期的帐户有问题。

我一直在学习如何做,发现很多基本相同的解决方案:

Get-ADGroupMember -Identity $DisName |get-aduser|Where{$_.Enabled -eq $true -and $_.AccountExpires -ne 0}

那是行不通的。我已经检查了过期帐户的Property AccountExpires的值,并且不等于0。我还在不同的过期日期之间进行了比较,例如:过期,今天过期,明天过期和永不过期,以尝试在价值观中找到一些相关的东西,但我什么也没找到。

这是我的代码:

$DisName = 'CN=demo1group,OU=groups,OU=demo1,OU=res10000,OU=Customer,DC=cloud,DC=local'
$lic = (Get-ADGroupMember -Identity $DisName |get-aduser|Where{$_.Enabled -eq $true -and $_.AccountExpires -ne 0}).count 

我试图强制使帐户过期,但我总是获得与$ lic相同的值

2 个答案:

答案 0 :(得分:1)

AccountExpires值始终是FileTime值9223372036836854775807,除非您将用户修改为过期,否则AccountExpires值将更改为0(永不过期)(对于我的实验室中的所有AD用户该值均更改为0)

我在实验室手动设置了2019年1月9日的过期日期并获得:

AccountExpirationDate                : 10/01/2019 00:00:00
accountExpires                       : 131915520000000000

因此,最好设置一个变量$ currentdate = Get-Date,然后对AccountExpirationDate参数进行比较运算符?

以下是在我的实验室中对其进行验证的示例代码:

$currrentdate = (Get-Date).ToFileTime()
Get-ADGroupMember -Identity <groupname> | get-aduser -Properties * | where {$_.Enabled -eq $true -and $_.AccountExpirationDate -ne $null -and $_.AccountExpires -lt $currrentdate} | Select-Object -Property UserPrincipalName,AccountExpirationDate,AccountExpires

答案 1 :(得分:0)

您可能需要这样做:

var fs = require("fs");
fs.readFile("input.txt", function(err, data) {
  console.log("This will second, after print after readFile is complete);
  fs.writeFile("input.txt", "Replaced Text", function(err) {
    console.log("This will print last, after writeFile is complete");
  });
  console.log("This will print third, before writeFile is complete");
});
console.log("This will print first");

Get-ADGroupMember -Identity $DisName | Get-ADUser -Properties Enabled,AccountExpires | Where { $_.Enabled -eq $true -and $_.AccountExpires -ne 0 } 默认情况下仅返回一组有限的属性,但是您可以使用Get-ADUser返回附加属性的特定列表,或使用-Properties返回所有属性。