我正在尝试为我们的支持团队创建一个模块,其中将包含一些我们日常使用的工具,但到目前为止我们一直使用 CMD。
我们使用的命令之一是 net user $username/domain
以检查用户密码是否已过期以及命令输出的所有其他有用详细信息。
我试图将该命令放入这样的函数中:
function Get-UserDetails {
[CmdletBinding()]
Param(
[Parameter(Mandatory=$true)]
[string]$UserName
)
net user $UserName /domain
}
该函数工作正常,但我只想过滤一些细节的输出。
问题是 net user
不是 PowerShell cmdlet,它没有属性,所以我无法选择任何属性。
所以我的问题是:
您知道在一个命令中获取所有数据的更好方法吗?因为 Get-ADUser
输出的数据比 net user
少。
最佳答案
您可以使用 Get-ADUser
并从中选择 msDS-UserPasswordExpiryTimeComputed
属性。问题是 - 即使使用 -Properties *
,此属性也可能不会枚举,因此在尝试检查返回的对象时它可能不明显。为了使事情更好,时间戳不是人类可读的格式。
尽管如此,您可以从 AD cmdlet 中获取密码到期日期,并使其易于阅读,如下所示:
# Get ADUser
$user = Get-ADUser username -Properties msDS-UserPasswordExpiryTimeComputed
# Get expiry timestamp and convert it from file time format
$userCredExpiryDate = [DateTime]::FromFileTime( $user.'msDS-UserPasswordExpiryTimeComputed' )
Here is the MSDN documentation for that AD DS attribute .
对于显示在 net user/domain
但不在 Get-ADUser
中的其他字段值 - 应该有其他 AD DS 属性,如果它们不显示,您可以搜索'显示 -Properties *
。对于这些,您需要在 AD DS 文档中查找适当的属性。
更新:Someone linked me to this page在 another question (与此行为相关)并且这似乎列出了可用于处理的其他属性,但在尝试查看对象的“所有”AD DS 属性时不会返回。我不知道这个列表有多完整,但它是了解您必须使用的其他 AD 属性的一个很好的起点。
关于windows - 净用户 $userName/domain,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51332146/