WMI是Windows管理规范的缩写,其中包含很多系统的软硬件信息。而Windows用户信息也可以通过WMI对象来获取。PowerShell提供了对WMI的访问功能,十分方便且强大——这就是Get-WmiObject这个cmdlet。
Get-WmiObject -Class Win32_UserAccount -Filter "Name='$env:username' and Domain='$env:userdomain'"
Get-WmiObject -Class Win32_UserAccount -Filter "Name='$env:username' and Domain='$env:userdomain'" | Select-Object *
Status : OK
Caption : hong-book/hong
Pass
wordExpires : False
__GENUS : 2
__CLASS : Win32_UserAccount
__SUPERCLASS : Win32_Account
__DYNASTY : CIM_ManagedSystemElement
__RELPATH : Win32_UserAccount.Domain="hong-book",Name="hong"
__PROPERTY_COUNT : 16
__DERIVATION : {Win32_Account, CIM_LogicalElement, CIM_ManagedSystemElement}
__SERVER : HONG-BOOK
__NAMESPACE : root/cimv2
__PATH : //HONG-BOOK/root/cimv2:Win32_UserAccount.Domain="hong-book",Name="hong"
AccountType : 512
Description :
Disabled : False
Domain : hong-book
FullName :
InstallDate :
LocalAccount : True
Lockout : False
Name : hong
PasswordChangeable : True
PasswordRequired : False
SID : S-1-5-21-181061805-855091228-1216038997-1000
SIDType : 1
Scope : System.Management.ManagementScope
Path : //HONG-BOOK/root/cimv2:Win32_UserAccount.Domain="hong-book",Name="hong"
Options : System.Management.ObjectGetOptions
ClassPath : //HONG-BOOK/root/cimv2:Win32_UserAccount
Properties : {AccountType, Caption, Description, Disabled...}
SystemProperties : {__GENUS, __CLASS, __SUPERCLASS, __DYNASTY...}
Qualifiers : {dynamic, Locale, provider, UUID}
Site :
Container :
function Test-UserPasswordExpires
{
param(
$UserName = $env:username,
$Domain = $env:userdomain
)
(Get-WmiObject -Class Win32_UserAccount -Filter "Name='$UserName' and Domain='$Domain'").PasswordExpires
}