我使用UserPrincipal来收集非域成员服务器上本地用户的详细信息。当用户的密码过期或过期时,我想给他们发电子邮件。
除了最大密码时代,我什么都有。我无法得到这个值和我发现的关于Active Directory连接的所有内容,而我并不需要这些连接。当我尝试调整它以适应本地服务器时,它不起作用。
以下是我所拥有的:
$date = Get-Date
$contextType = [System.DirectoryServices.AccountManagement.ContextType]::Machine
$principalContext = New-Object System.DirectoryServices.AccountManagement.PrincipalContext($contextType)
$principalSearcher = New-Object System.DirectoryServices.AccountManagement.PrincipalSearcher(New-Object System.DirectoryServices.AccountManagement.UserPrincipal($principalContext))
ForEach($userPrincipal in $principalSearcher.FindAll())
{
$maxPasswordAge = 90
$name = $userPrincipal.Name
$samAccountName = $userPrincipal.SamAccountName
$description = $userPrincipal.Description
$fullname = $userPrincipal.DisplayName
$lastPasswordSet = $userPrincipal.LastPasswordSet
$passwordExpires = $lastPasswordSet.AddDays($maxPasswordAge)
$passwordExpiresDays = New-TimeSpan -Start $date -End $passwordExpires
$passwordExpiresDays = [Math]::Floor($passwordExpiresDays.TotalDays)
Write-Host "Name: "$name
Write-Host "SAM Account Name: "$samAccountName
Write-Host "Full Name: "$fullName
Write-Host "Password last set: "$lastPasswordSet
Write-Host "Password expires: "$passwordExpiresDays
Write-Host "Max Password Age: "$maxPasswordAge
}在上面的代码中,我手动将值设置为90,以使代码正常工作。我需要用代码替换它来检索正确的值。
我尝试了以下方法从我的DirectoryEntry对象中提取UserPrincipal对象,然后从本机对象中提取该对象,但是我无法让它工作:
$directoryEntry = $userPrincipal.GetUnderlyingObject()
$native = [ADSI]$directoryEntry.NativeObject我肯定这很容易,我忽略了一些显而易见的事情.
发布于 2016-11-16 19:14:12
由于默认情况下未启用本地用户帐户的密码过期,您可能无法获得值。
您需要在“计算机本地安全策略”中手动启用最大密码年龄。
编辑:您可以从注册表读取本地安全策略设置的值:
$maxPasswordAge = (Get-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\Netlogon\Parameters -Name MaximumPasswordAge).MaximumPasswordAgehttps://stackoverflow.com/questions/40638448
复制相似问题