2

I am having two strings. If the strings are equal then nothing to do , the else statement is fired. Just it works for in single-line format. but if the strings multiple lines like below then it does not work. Where am I do wrong ?

For example $myvar:

log.txt:

15732: 19/Jan/2019 15:40:06.969 ERROR message -outofmemory

log_2.txt:

15732: 19/Jan/2019 15:40:06.969 ERROR message -fatal error 01 (122)

Here is my script:

$myvar

if (!($myvar)) {
    Write-Host "No exception info to report" -BackgroundColor Green
} else {
    $InputString = $myvar
    $StringToMatch = Get-Content C:\ftp_file\report\dump.txt
    if ($InputString -eq $StringToMatch) {
        Write-Host "it will not be sent mail because same error message $($InputString)" -BackgroundColor Green
    } else {
        Write-Host "it will be sent mail" -BackgroundColor Cyan
        Send-Mail blah blah..
        #Export myvar variable again
        "$myvar" | Out-File -FilePath C:\ftp_file\report\dump.txt
    }
}
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
Arbelac
  • 1,698
  • 6
  • 37
  • 90
  • 3
    Use `Get-Content` with the `-Raw` switch to get the file content as one single string. Without it, it returns an string array (the content split on newlines). Besides that, what is in your `$StringToMatch` string. If that is something that should be found somewhere in the content of the file, you will be better of using a regular expression like `if ($StringToMatch -match [Regex]::Escape($InputString))`. – Theo Mar 15 '19 at 11:28
  • `$StringToMatch` string same as `$myvar` – Arbelac Mar 15 '19 at 12:01
  • I don't folow that comment I'm afraid.. Does that mean `$myvar` should be completely equal to `$StringToMatch` ? In that case, you can use the `-eq` comparison operator, but you will still need to use `Get-Content -Raw` – Theo Mar 15 '19 at 12:05
  • Like you said I have tested it. But no luck. – Arbelac Mar 15 '19 at 12:09
  • The example strings for `$myvar` are all just one line. Where is the multiple lines in there? – Theo Mar 15 '19 at 12:10
  • Ok man I have used `if ($StringToMatch -match [Regex]::Escape($InputString))` . it works any conditions. – Arbelac Mar 15 '19 at 12:36

0 Answers0