I created a foreach
loop with a if..else
statement which should check whether a variable is null/empty or not. Everything seems to work fine. However, the results returned all have one outcome despite some variables being empty and some other not.
I tried using different bits within the if..else
statement such as
if ([string]::IsNullOrWhiteSpace($password))
or
if ($password -eq $null)
or
if ($password.length -gt 2)
but I had no luck whatsoever.
I started working on this to find quickly which computers didn't have LAPS working to investigate why. The issue is fixed but this script is still bugging me as I can't understand what I am doing wrong.
This is my code, tried to use both the ms-mcs-admpwd
attribute and the LAPS built-in get-admpwd
, no difference.
$computers = Get-ADComputer -Filter * -SearchBase $OU -Properties * |
Select-Object Name, ms-mcs-admpwd
foreach ($computer in $computers) {
$password = Get-ADComputer $computer.Name -Properties * |
Select ms-mcs-admpwd
if ($password) {
Write-Host "LAPS password on $computer present"
} else {
Write-Host "LAPS password on $computer not present"
}
Write-Host $password
Write-Host " "
}
This is the outcome:
LAPS password on @{Name=Computer1; ms-mcs-admpwd=} present @{ms-mcs-admpwd=} LAPS password on @{Name=Computer2; ms-mcs-admpwd=8CG1]8,q.j} present @{ms-mcs-admpwd=8CG1]8,q.j} LAPS password on @{Name=Computer3; ms-mcs-admpwd=P2v94d+05q} present @{ms-mcs-admpwd=P2v94d+05q} LAPS password on @{Name=Computer4; ms-mcs-admpwd=} present @{ms-mcs-admpwd=}
As you can see Computer1 and Computer4 have no ms-mcs-admpwd
attribute, yet the outcome is the same as Computer2 and Computer3.