Reputation: 72
Sorry for this stupid question but I don't know how to simply test the result of my command (it returns an object)!
Here is my command:
$command = Get-ADUser -Server "MyServer" -Filter 'Name -eq "test"' -SearchBase "DC=MyDomain,DC=COM" -Properties badpwdcount,lockedout | Select-Object LockedOut
I just want to test the result like this :
if($command -eq $true){
write-host "OK"
}
When I try this command to see the result :
write-host $command
I get "@{LockedOut=True}". So, I have tried to change also my condition like :
if($command -eq "@{LockedOut=True}"){
write-host "OK"
}
But it's not working...
Upvotes: 0
Views: 9016
Reputation: 174485
When Write-Host $command
outputs @{LockedOut=True}
, it's because Select-Object LockedOut
produces a new object with a LockedOut
property.
Either change your Select-Object
statement to use the -ExpandProperty
parameter:
$command = Get-ADUser -Server "MyServer" -Filter 'Name -eq "test"' -SearchBase "DC=MyDomain,DC=COM" -Properties badpwdcount,lockedout | Select-Object -ExpandProperty LockedOut
if($command){
# "test" user is locked out
}
or inspect the LockedOut
property:
if($command.LockedOut){
# "test" user is locked out
}
As you can see, the -eq $true
comparison inside an if
statement is redundant
Be aware that you may get unexpected results if you have multiple users with the name test
in your directory. If you're locating users by username, you should filter on the SAMAccountName
property (which is guaranteed to be unique per-domain):
$command = Get-ADUser -Server "MyServer" -Filter 'SAMAccountName -eq "test"' -SearchBase "DC=MyDomain,DC=COM" -Properties badpwdcount,lockedout | Select-Object -ExpandProperty LockedOut
if($command){
# "test" user is locked out
}
Upvotes: 3