Powershell 获取ADuser和嵌套广告组

Powershell 获取ADuser和嵌套广告组,powershell,active-directory,Powershell,Active Directory,我正在使用一个脚本来通知用户密码即将过期,但在获取嵌套组的结果时遇到了一些问题。我的脚本将筛选属于父测试成员的用户。我不会让任何用户直接成为此测试的成员,他们将是Child01 Test和Child02 Test的成员Child01和Child02是父测试的成员 有没有办法使用Get ADUser或者我应该使用类似于Get ADGroupMember的方法来实现这一点 $smtpServer="mail.company.com" $expireindays = 10 $ADGroup ="CN=

我正在使用一个脚本来通知用户密码即将过期,但在获取嵌套组的结果时遇到了一些问题。我的脚本将筛选属于
父测试
成员的用户。我不会让任何用户直接成为此测试的成员,他们将是
Child01 Test
Child02 Test
的成员
Child01
Child02
是父测试的成员

有没有办法使用
Get ADUser
或者我应该使用类似于
Get ADGroupMember
的方法来实现这一点

$smtpServer="mail.company.com"
$expireindays = 10
$ADGroup ="CN=Parent-test,OU=Groups,OU=Test,DC=Test1,DC=Test2,DC=Test3,DC=com"
$OfficeOU ="OU=Test,DC=Test1,DC=Test2,DC=Test3,DC=com"

#Get Users From AD who are enabled
Import-Module ActiveDirectory
$users = get-aduser -filter {memberof -eq $ADGroup} -properties * -searchbase $OfficeOU |where {$_.Enabled -eq "True"} | where { $_.PasswordNeverExpires -eq $false } | where { $_.passwordexpired -eq $false }

foreach ($user in $users)
{
$Name = (Get-ADUser $user | foreach { $_.GivenName})
$emailaddress = $user.emailaddress
$passwordSetDate = (get-aduser $user -properties * | foreach { $_.PasswordLastSet })
$PasswordPol = (Get-AduserResultantPasswordPolicy $user)
# Check for Fine Grained Password
if (($PasswordPol) -ne $null)
{
$maxPasswordAge = ($PasswordPol).MaxPasswordAge
}

else
{
$maxPasswordAge = (Get-ADDefaultDomainPasswordPolicy).MaxPasswordAge
}


$expireson = $passwordsetdate + $maxPasswordAge
$today = (get-date)
$daystoexpire = (New-TimeSpan -Start $today -End $Expireson).Days
$subject="Your Network/Outlook password will expire in $daystoExpire days"
$attachment="C:\Util\Outlook Web App Password Change Procedure.pdf"
$body ="
Dear $name,
<p> Your Network/Outlook password will expire in $daystoexpire days.<br>
Please follow the instructions in the attached guide to change your password. For     assistance, please contact me or send an email to admin@support.com<br>

if ($daystoexpire -lt $expireindays)
{
Send-Mailmessage -smtpServer $smtpServer -from $from -to $emailaddress -subject    $subject -body $body -attachments $attachment -bodyasHTML -priority High

}  

}
$smtpServer=“mail.company.com”
$expireindays=10
$ADGroup=“CN=Parent test,OU=Groups,OU=test,DC=Test1,DC=Test2,DC=Test3,DC=com”
$OfficeOU=“OU=Test,DC=Test1,DC=Test2,DC=Test3,DC=com”
#从已启用的AD获取用户
导入模块ActiveDirectory
$users=get aduser-filter{memberof-eq$ADGroup}-properties*-searchbase$OfficeOU |其中{$\.Enabled-eq“True”}其中{$\.PasswordNeverExpires-eq$false}其中{$\.passwordexpired-eq$false}
foreach($users中的用户)
{
$Name=(Get ADUser$user | foreach{$\.GivenName})
$emailaddress=$user.emailaddress
$passwordSetDate=(get aduser$user-properties*| foreach{$\密码集})
$PasswordPol=(获取AduserResultantPasswordPolicy$user)
#检查细粒度密码
if(($PasswordPol)-ne$null)
{
$maxPasswordAge=($PasswordPol).maxPasswordAge
}
其他的
{
$maxPasswordAge=(获取AddFaultDomainPasswordPolicy)。maxPasswordAge
}
$expireson=$passwordsetdate+$maxPasswordAge
$today=(获取日期)
$daystoexpire=(新时间跨度-开始$today-结束$Expireson)。天
$subject=“您的网络/Outlook密码将在$daystoexpired天后过期”
$attachment=“C:\Util\Outlook Web App密码更改过程.pdf”
$body=”
亲爱的$name,
您的网络/Outlook密码将在$daystoexpire天内过期。
请按照随附指南中的说明更改密码。如需帮助,请与我联系或发送电子邮件至admin@support.com
如果($daystoexpire-lt$expire-indays) { 发送邮件-smtpServer$smtpServer-从$from-到$emailaddress-主题$subject-正文$body-附件$attachment-正文HTML-高优先级 } }
我会先使用
Get-ADGroupMember$ADGroup-Recursive
收集组成员列表,然后将用户与之匹配,例如:

$pattern = [regex]::Escape($OfficeOU)

Get-ADGroupMember $ADGroup -Recursive |
Get-ADUser -Properties * |
? { $_.Enabled -and $_.PasswordNeverExpires -eq $false -and $_.passwordexpired -eq $false -and $_.DistinguishedName -match $pattern }

感谢您的输入,我将尝试合并它,看看我得到了什么。我刚刚更新了我的帖子,以显示整个脚本,以便提供更好的图片。我需要在最初的get ADUser命令中获得所有广告属性,因为我引用了一些其他属性来向用户发送通知。然后只需将其更改为
-Properties*
就像您在脚本中所做的那样。我之所以将这两个密码属性放在我的示例有效所需的位置,唯一的原因是它们。:)如果这太难解释,请随时告诉我!我只是被PS弄湿了脚。我通过使用“Get-ADGroupMember$ADGroup-Recursive | Get-aduser-properties*| where”来实现这一点{$\.Enabled-eq“True”}其中{$\.PasswordNeverExpires-eq$false}其中{$\.passwordexpired-eq$false}“然后是我的代码的其余部分。与我的示例相比,您的示例所显示的方式有什么优点/缺点?我没有使用match和pattern变量。不同之处在于,cmdlet越少通常意味着执行速度越快,因此将所有测试分组到一个
,其中
-语句更快、更干净。”(请记住,
只是
的别名,其中
).至于模式,那只是为了匹配OU。我不确定这是否是一个标准,所以我将其包括在内以确保安全。酷。.我不知道
where
的别名。这个小项目是我第一次使用PowerShell,但我发现当我有最终目标并且可以努力实现时,学习起来要容易得多谢谢,谢谢你抽出时间。