Everything is equal to "\\Z"

OK, Losing my mind a bit here. I’m a fairly basic Powershell user, I don’t tend to write anything terribly complicated but can’t figure out why every string I test in this script is true. Here’s the script;

$PreviousLineIsComputer = $false
$PreviousLine = “”
$lines = get-content -path “C:\temp\Oratest.txt”
foreach ($line in $lines)
{
Write-Host “PreviousLineIsComputer is $PreviousLineIsComputer, Line is $line, Previous Line is $PreviousLineComputerName”
if ($line)
{
$TestString = $line
$FirstThree = $TestString.Substring(0,3)
Write-Host “First three chars are $FirstThree”
if ($FirstThree = “\Z”)
{
Write-Host “The First Three test is true”
if ($PreviousLineIsComputer)
{
Write-Host “Oracle is not installed on $PreviousLineComputerName”
}

  $PreviousLineIsComputer = $true
  $PreviousLineComputerName = $line
  }
  else
  {
  Write-Host "Test String first 8 is $TestString.Substring(0,8)"
        if ($TestString.Substring(0,8) = "TNS Ping")
        {
           Write-Host "Oracle $TestString.SubString(21,35) is installed on $PreviousLineComputerName"
        }
        $PreviousLineIsComputer = $false
        $PreviousLineComputerName = $line
        Write-Host "This line is not a computer - $TestString"
        Write-Host "IsComputer is $IsComputer, Line is $line, Previous Line is $PreviousLineComputerName"
  }

}
}

The data in oratest.txt is;

\ZXXXXXXXXXX

TNS Ping Utility for 64-bit Windows: Version 19.0.0.0.0 - Production on 09-APR-2021 09:41:15

Copyright (c) 1997, 2019, Oracle. All rights reserved.

TNS-03502: Insufficient arguments. Usage: tnsping []
\ZXXXXXXXXX
\ZXXXXXXXXX

TNS Ping Utility for 64-bit Windows: Version 19.0.0.0.0 - Production on 09-APR-2021 09:41:38

Copyright (c) 1997, 2019, Oracle. All rights reserved.

TNS-03502: Insufficient arguments. Usage: tnsping []

and the output is;

PS H:> C:\temp\FindOracleVer.ps1
PreviousLineIsComputer is False, Line is \Z7LPC0C1SZC , Previous Line is TNS-03502: Insufficient arguments. Usage: tnsping <address

[]
First three chars are \Z
The First Three test is true
PreviousLineIsComputer is True, Line is , Previous Line is \Z7LPC0C1SZC
PreviousLineIsComputer is True, Line is TNS Ping Utility for 64-bit Windows: Version 19.0.0.0.0 - Production on 09-APR-2021 09:41:15,
Previous Line is \Z7LPC0C1SZC
First three chars are TNS
The First Three test is true
Oracle is not installed on \Z7LPC0C1SZC

I can understand how “\Z” is equal to “\Z” but how is “TNS” equal to “\Z”

1 Like

So all I needed to do was post this topic for me to realize my rookie misake in using = instead of -eq

3 Likes

You are not alone on that one. I spent a good deal of time troubleshooting a large script only to find the solution was the same as yours :slight_smile: