PowerShell variable comparison (like, eq) not working as expected

73 Views Asked by At

I'm trying to compare two variables, which store a SHA1 hash. One of the variables is from a WMIC command and the other is from a get-childitem command.

The WMIC command in question: (Get-WmiObject -Namespace "root\CIMV2\TerminalServices" -Class Win32_TSGeneralSetting | select SSLCertificateSHA1Hash | Select -first 1).SSLCertificateSHA1Hash

The get-childitem command:

(Get-ChildItem Cert:\LocalMachine\My -Recurse | `
Where-Object {$_.NotAfter -gt ([datetime]::now).AddDays(36)} | WHERE {$_.Subject -match $HOSTNAME } | Select -first 1).Thumbprint

For some reason the comparison fails, even though both variables contain the same SHA1 hash. I have no idea what I'm doing wrong.

PS C:\scripts> $THUMBPRINT
381E44961164E60E789FBA00A30A848A8E6DEE9A

PS C:\scripts> $CURRENT_RDP_THUMBPRINT
381E44961164E60E789FBA00A30A848A8E6DEE9A

PS C:\scripts> $CURRENT_RDP_THUMBPRINT -eq $THUMBPRINT
False

When using the -like option, I get the same false result. Thinking it might have been an issue with the variable types, they appear to both be of the same type:

PS C:\scripts> $CURRENT_RDP_THUMBPRINT.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object


PS C:\scripts> $THUMBPRINT.GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object
0

There are 0 best solutions below