[英]Get Azure Active Directory password expiry date in PowerShell
[英]POWERSHELL: Get password expiry date with UserPrincipal
我正在使用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,以使代碼正常工作。 我需要將其替換為代碼以獲取正確的值。
我嘗試了以下方法從UserPrincipal中提取DirectoryEntry對象,然后從本機對象中提取,但是我無法使其正常工作:
$directoryEntry = $userPrincipal.GetUnderlyingObject()
$native = [ADSI]$directoryEntry.NativeObject
我敢肯定它很簡單,並且我忽略了一些顯而易見的事情。
您可能無法獲得一個值,因為默認情況下未啟用本地用戶帳戶的密碼過期。
您需要在計算機的本地安全策略中手動啟用“ 最長密碼使用期限”。
編輯 :您可以從注冊表中讀取本地安全策略設置的值:
$maxPasswordAge = (Get-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\Netlogon\Parameters -Name MaximumPasswordAge).MaximumPasswordAge
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.