import-module ActiveDirectory
(get-aduser "djohns02" -Properties AccountExpirationDate).AccountExpirationDate
如果用户在其计算机上设置并配置了ADUC工具,则可以使用此功能。问题是用户不会进行此设置并安装。
我的下一步想法是尝试设置一个powershell脚本,以便在具有此设置的计算机上运行远程脚本。由于他们不是管理员,他们无法得到这个设置并且据我所知工作。作为管理员,我可以强制信任并启用远程,但不能作为标准用户。
我有没有想到的可能性?目标是让承包商能够检查他们的帐户何时到期。
答案 0 :(得分:1)
net user %USERNAME% /domain
我的程序可以过滤结果,只给出过期
答案 1 :(得分:0)
以下是您可以使用的不需要AD cmdlet的PowerShell脚本:
# Get-AccountExpirationDate.ps1
# Written by Bill Stewart
#requires -version 2
[CmdletBinding()]
param(
[parameter(Mandatory=$true,ValueFromPipeline=$true)]
[String[]] $Name
)
begin {
$ACCOUNT_NEVER_EXPIRES = 0x7FFFFFFFFFFFFFFF
$Searcher = [ADSISearcher] [ADSI] ""
$Searcher.PageSize = 1000
$Searcher.PropertiesToLoad.AddRange(@("accountexpires","distinguishedname","samaccountname"))
function Get-SearchResultProperty {
param(
[System.DirectoryServices.ResultPropertyCollection] $properties,
[String] $propertyName
)
if ( $properties[$propertyName] ) {
$properties[$propertyName][0]
}
}
function Get-AccountExpirationDate {
param(
$name
)
$Searcher.Filter = "(&(objectCategory=person)(objectClass=user)(anr=$name))"
$searchResults = $searcher.FindAll()
$resultCount = ($searchResults | measure-object).Count
if ( $resultcount -gt 0 ) {
foreach ( $searchResult in $searchResults ) {
$properties = $searchResult.Properties
$accountExpires = Get-SearchResultProperty $properties "accountexpires"
if ( $accountExpires -ne $null ) {
if ( ($accountExpires -eq 0) -or ($accountExpires -eq $ACCOUNT_NEVER_EXPIRES) ) {
$accountExpires = $null
}
else {
$accountExpires = [DateTime]::FromFileTime($accountExpires)
}
}
"" | select-object `
@{Name = "distinguishedName"; Expression = {Get-SearchResultProperty $properties "distinguishedname"}},
@{Name = "sAMAccountName"; Expression = {Get-SearchResultProperty $properties "samaccountname"}},
@{Name = "accountExpires"; Expression = {$accountExpires}}
}
}
$searchResults.Dispose()
}
}
process {
foreach ( $nameItem in $Name ) {
Get-AccountExpirationDate $nameItem
}
}
你可以像这样运行它(假设脚本在C:\ Scripts中):
PS C:\Scripts> .\Get-AccountExpirationDate.ps1 kendyer