Powershell if statement evaluating a NULL value as not NULL

Status
Not open for further replies.

chhall_ato

Seniorius Lurkius
30
So I'm working with Powershell trying to query TPM data from computer objects. I'm having an issue where, when there is no TPM data for a given object, which should be NULL, it evaluates to not NULL. The script below is how I've been experimenting, trying to get my syntax correct. You'll see that I store the value of the msTPM-OwnerInformation in $strTPMString. If it's blank, this should be NULL. However, the if statement always returns Yes, whether or not the var is blank. I've echoed the var and it has no value, I've tried evaluating it to "" and $NULL, I've tried -eq and -ne. Nothing seems to evaluate an empty value to false. Anyone with experience in Powershell and querying AD in this manner that could help me out there?

$strQuery = "Select msTPM-OwnerInformation from 'LDAP://dc=domain,dc=com' where name='123456G'"

$objConnection = New-Object -ComObject "ADODB.Connection"
$objCommand = New-Object -ComObject "ADODB.Command"
$objConnection.Open("Provider=ADsDSOObject;")
$objCommand.ActiveConnection = $objConnection
$objCommand.CommandText = $strQuery
$objRecordSet = $objCommand.Execute()

$strTPMString = $objRecordSet.Fields.item("msTPM-OwnerInformation").Value
echo $strTPMString
if ($strTPMString -eq $NULL)
{
echo "No"
}
else
{
echo "Yes"
}

$objConnection.Close()
 

daishi

Ars Legatus Legionis
10,387
I see your question has been answered. I think I may have encountered issues before when trying to check for null so I will add that link to my list of powershell links.

Have you thought about using the activedirectory powershell module made by microsoft? It would make your code a lot cleaner.

get-adcomputer 123456G -properties msTPM-OwnerInformation
 
Status
Not open for further replies.