0

I have small script. Which i want test it with "If, else statement". I have the same value variables but when try to compare it, it gives else condition, but it should give true condition. Below are my vars with results.

$ASE1=Get-Content -path "C:\Program Files\Kratos\Tools\ASE\cnf\_ASE.cnf" | out-string -Stream | Select-String "VERSION"

Version 85

$ASE2=Get-Content -path "C:\ProgramData\Kratos\Tools\NaCl\Files\xASE.cnf"| out-string -Stream | Select-String "VERSION"

Version 85

if($ASE1 -eq $ASE2) { echo "equal"} else { echo "not equal"}

This above small script return me "not equal" condition but in true this vars is equal to each other. Do you know what im doing wrong ?

  • I''ve fix it, by changing "-ne" to "-notmatch". – Amir Damirov Jun 26 '19 at 12:44
  • Then post an actual answer – Sven Jun 26 '19 at 13:52
0

Select-String doesn't return a string but a Microsoft.PowerShell.Commands.MatchInfo Object

  • As these Objects differ your result is expected. (view output of $ASE1,$ASE2)
  • Both Get-Content and Out-String from your example are superfluous.
  • As the pattern is RegEx based, you might use a (capture group) to just get the digits of your version.
$Pattern = 'Version (\d+)'
$ASE1 = Select-String -Pattern $Pattern -Path "C:\Program Files\Kratos\Tools\ASE\cnf\_ASE.cnf"
$ASE2 = Selcet-String -Pattern $Pattern -Path "C:\ProgramData\Kratos\Tools\NaCl\Files\xASE.cnf"
if($ASE1.Matches.Groups[1].Value -eq $ASE2.Matches.Groups[1].Value) { 
    "equal"
} else {
    "not equal"
}
0

-readcount 0 works the same as piping to out-string.
Piping out-string or get-content to select-string puts a bottleneck on your script.

$ASE1 = Get-Content "C:\Program Files\Kratos\Tools\ASE\cnf\_ASE.cnf" -ReadCount 0
$ASE2 = Get-Content "C:\ProgramData\Kratos\Tools\NaCl\Files\xASE.cnf" -ReadCount 0
[regex]$Pattern = 'Version\s\d+'
if ($Pattern.Matches($ASE1).value -eq $Pattern.Matches($ASE2).value) { 
    "equal"
}
else {
    "not equal"
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.