Bill Westrup
Bill Westrup

Reputation: 175

Powershell: Comparing a block of text to a file

It certainly seemed like a simple enough task but for whatever reason this doesn't work:

    #Verifies that the firefox proxy setting have been applied

    #locate Prefsjs file
    $PrefsFiles = Get-Item -Path ($env:SystemDrive+"\Users\*\AppData\Roaming\Mozilla\Firefox\Profiles\*\prefs.js")

    #read in Prefsjs
    $Prefsjs = (Get-Content $PrefsFiles)

    #Block to compare 
    $Update= @"
    user_pref("network.proxy.http", "0.0.0.0");
    user_pref("network.proxy.http_port", 80);
    "@

    ($Prefsjs -contains $Update)

The last line should return a true because the text actually does exist in $Prefsjs... Any ideas?

Upvotes: 3

Views: 198

Answers (1)

mjolinor
mjolinor

Reputation: 68273

It's not going to match because you're comparing a multi-line string to an array of single line strings.

You need to compare like objects, which means $Prefsjs also needs to be a single, multi-line string. The easiest way to do that is to add the -Raw switch to your Get-Content:

#read in Prefsjs
    $Prefsjs = (Get-Content $PrefsFiles -Raw)

But now $Prefsjs is not an array any more, so you can't use -Contains. It's now just a single string, so you can use the string contains() method to accomplish the same thing:

$Prefsjs.contains($Update)

Upvotes: 3

Related Questions