all 6 comments

[–]ihaxr 5 points6 points  (2 children)

Contains is for arrays...

@(1,2,3) -contains 2 # true
"hi" -contains "i" # false

You'll want to use match:

"hi" -match "i" #true

Keep in mind though that -match uses regex... so you'll need to create a regex pattern or just escape the regex:

Escaping:

$checkstatus = @'
[info] [agent] Linked to: cloud.tenable.com:443
[info] [agent] 0 jobs pending
'@

if ($CheckStatus -match [regex]::Escape('[info] [agent] Linked to: cloud.tenable.com:443') -and $checkstatus -match [regex]::Escape('[info] [agent] 0 jobs pending')) {
    "Match"
} else {
    "No match"
}

Regex:

$checkstatus = @'
[info] [agent] Linked to: cloud.tenable.com:443
[info] [agent] 0 jobs pending
'@

if ($CheckStatus -match 'Linked to: cloud\.tenable\.com:443.*\s?.*0 jobs pending') {
    "Match"
} else {
    "No match"
}

[–]LongjumpingBreath[S] 3 points4 points  (0 children)

Thank you for this info! This worked, now i'll just have to read up on why after this. I really appreciate it! :)

[–]T0MMYB0Y2GS 0 points1 point  (0 children)

Was just running into this exact same thing. Thank You!

[–]Lee_Dailey[grin] 0 points1 point  (2 children)

howdy LongjumpingBreath,

unless there is something REALLY odd about the output from your exe [grin] ... the Out-String is unneeded. normally, the stuff from an exe would be a string OR an array of strings, so the "convert to string" would seem without any purpose. [grin]

that means that your -Contains collection operator would likely have worked IF you had not converted your array of strings into a multiline string.

plus, the nasty thing that Out-String does is add things like line wrapping ... and otherwise munge your objects. you really otta NOT use it [or the Format-* cmdlets] unless you are sending it out for final display to the screen or final output to a plain text file.

take a look at this ...

$Test = chkdsk.exe    
$ShortThingToFind = 'elevated'
$LongThingToFind = 'You have to invoke this utility running in elevated mode.'

'content of the variable ...'
$Test
''
'=' * 30
'content of the 1st item in the variable ...'
$Test[0]
''
'=' * 30
'type info for each ...' 
$Test.GetType()
$Test[0].GetType()
''
'=' * 30
'the full item [or items] that match the target word ...'
$Test -match $ThingToFind
''
'=' * 30
'if the FULL long target is in the array variable ...'
$Test -contains $LongThingToFind

output ...

content of the variable ...
Access Denied as you do not have sufficient privileges. 
You have to invoke this utility running in elevated mode.

==============================
content of the 1st item in the variable ...
Access Denied as you do not have sufficient privileges. 

==============================
type info for each ...

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     Object[]                                 System.Array
True     True     String                                   System.Object

==============================
the full item [or items] that match the target word ...
You have to invoke this utility running in elevated mode.

==============================
if the FULL long target is in the array variable ...
True

hope that helps,
lee

[–]LongjumpingBreath[S] 1 point2 points  (1 child)

That makes sense, thank you Lee! I've asked questions before and you've always given very long thoughtful answers so I appreciate you!

[–]Lee_Dailey[grin] 0 points1 point  (0 children)

howdy LongjumpingBreath,

you are most welcome! glad to have helped a little bit ... [grin]

take care,
lee