简体   繁体   中英

Properties not showing correctly for ADUsers in Powershell

I found something that was sort of odd while looking through ADUsers in Powershell.

If I typed the following:

PS> Get-ADUser Xuser -Properties PasswordLastSet, LastLogonDate

I would get an output like this:

DistinguishedName : CN=Xuser,OU=Domain Users,DC=LocalDC,DC=local
Enabled           : True
GivenName         : 
LastLogonDate     : 7/14/2016 8:24:03 AM
Name              : Xuser
ObjectClass       : user
PasswordLastSet   : 7/1/2016 11:48:37 AM
SamAccountName    : Xuser
Surname           :
UserPrincipalName :

As you can see, I have timestamps on PasswordLastSet and LastLogonDate.

However, if I type this following:

PS> $user = Get-ADUser Xuser
PS> $user.LastLogonDate -eq $null
PS> True
PS> $user.PasswordLastSet -eq $null
PS> True

To make sure nothing was messed up, I typed:

PS> $user

DistinguishedName : CN=Xuser,OU=Domain Users,DC=LocalDC,DC=local
Enabled           : True
GivenName         : 
LastLogonDate     : 
Name              : Xuser
ObjectClass       : user
PasswordLastSet   : 
SamAccountName    : Xuser
Surname           :
UserPrincipalName :

For some reason, that information shows $null.

One thing I noticed is that when I typed:

PS> $user = Get-ADUser Xuser

It doesn't gather PasswordLastSet and LastLogonDate by default--you have to request that specific information as parameters with the -Properties switch.

If I type:

PS> $user.LastLogonDate
PS> $user

It now has the LastLogonDate field but it is $null. Same goes if I type:

PS> $user.PasswordLastSet
PS> $user

Both PasswordLastSet and LastLogonDate show in the output for $user but if I run a condition to see if the value is $null, it returns True .

Kind of squirrely behavior and I was curious as to why it does that. My best educated guess would be that $user is only populated with the default information from the Get-ADUser cmdlet and it is very simply WYSIWYG. To obtain those particular fields, I have to implicitly request them.

I wanted to see if certain fields were hidden by default (unless requested in the initial Get-ADUser cmdlet), so, I typed:

PS> $user.random
PS> $user

And sure enough, random : was a field in there.

I could even go as far as to do this:

PS> $user.random = "Property"

It shows random : {Property} when I type $user

Question: Can anyone explain briefly why it behaves this way?

如果您要测试变量是否具有内容而不是仅仅存在,请尝试:

$user.LastLogonDate.Length -gt 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM