首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >POWERSHELL:使用UserPrincipal获取密码过期日期

POWERSHELL:使用UserPrincipal获取密码过期日期
EN

Stack Overflow用户
提问于 2016-11-16 17:24:55
回答 1查看 1.3K关注 0票数 0

我使用UserPrincipal来收集非域成员服务器上本地用户的详细信息。当用户的密码过期或过期时,我想给他们发电子邮件。

除了最大密码时代,我什么都有。我无法得到这个值和我发现的关于Active Directory连接的所有内容,而我并不需要这些连接。当我尝试调整它以适应本地服务器时,它不起作用。

以下是我所拥有的:

代码语言:javascript
复制
$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对象,然后从本机对象中提取该对象,但是我无法让它工作:

代码语言:javascript
复制
$directoryEntry = $userPrincipal.GetUnderlyingObject()
$native = [ADSI]$directoryEntry.NativeObject

我肯定这很容易,我忽略了一些显而易见的事情.

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-11-16 19:14:12

由于默认情况下未启用本地用户帐户的密码过期,您可能无法获得值。

您需要在“计算机本地安全策略”中手动启用最大密码年龄

编辑:您可以从注册表读取本地安全策略设置的值:

代码语言:javascript
复制
$maxPasswordAge = (Get-ItemProperty -Path HKLM:\SYSTEM\CurrentControlSet\Services\Netlogon\Parameters -Name MaximumPasswordAge).MaximumPasswordAge
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40638448

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档