Powershell Issue Comparing Array Variables
-
I'm finding this kind of hard to explain, so I will try to make it simple using direct outputs from powershell:
The definitions of my variablesPS Admin>$logError 2/14/2017 3:21:55 1211577 Response Timeout UniqueID 37 10 02 01 D9 00 EC Socket 0 SockId 0 FailureCount 2 of 3 PS Admin>$element UNIQUEID -------- 37 PS Admin>$element.UNIQUEID 37
So, I try to do this:
PS Admin>$logError -contains $element False
Which really confuses me, but I think the issue lies in the way I'm pulling data into these and attempting to compare an array to a string.:
PS Admin>write-host($logError) 2/14/2017 3:21:55 1211577 Response Timeout UniqueID 37 10 02 01 D9 00 EC Socket 0 SockId 0 FailureCount 2 of 3 PS Admin>write-output($logError) 2/14/2017 3:21:55 1211577 Response Timeout UniqueID 37 10 02 01 D9 00 EC Socket 0 SockId 0 FailureCount 2 of 3 PS Admin>write-host($element) System.Data.DataRow PS Admin>write-output($element) UNIQUEID -------- 37
I can give more information if needed, but my main focus is this- how do I compare the 37 to the $logError instead of it comparing the System.Data.Datarow to $logError?
-
What happens if you do:
$IsThereAnError = $logError.Contains($element)
$IsThereAnError$logError might have to be converted to a string
-
If that doesn't work, try:
$IsThereAnError = ($logError -as [string]).Contains($element) $IsThereAnError
-
PS Admin>$IsThereAnError = $logError.Contains($element)
Method invocation failed because [Microsoft.PowerShell.Commands.MatchInfo] doesn't contain a method named 'Contains'.
At line:1 char:37- $IsThereAnError = $logError.Contains <<<< ($element)
- CategoryInfo : InvalidOperation: (Contains:String) [], RuntimeException
- FullyQualifiedErrorId : MethodNotFound
PS Admin>$IsThereAnError = $logError -Contains($element) PS Admin>$IsThereAnError False
had to switch the . to a -. Something also to note is this:
PS Admin>$PSVersionTable Name Value ---- ----- CLRVersion 2.0.50727.5485 BuildVersion 6.1.7601.17514 PSVersion 2.0 WSManStackVersion 2.0 PSCompatibleVersions {1.0, 2.0} SerializationVersion 1.1.0.1 PSRemotingProtocolVersion 2.1
Keep talking to management about getting some maintenance time to get this server upgraded.
I do believe $logError is a string, but I was thinking I should still be able to compare the variable in the element array to it?
- $IsThereAnError = $logError.Contains <<<< ($element)
-
PS Admin>$IsThereAnError = ($logError -as [string]).Contains($element) PS Admin>$IsThereAnError False
-
scrolled back and took a better look. Try:
$IsThereAnError = ($logError -as [string]).Contains($element.UNIQUEID) $IsThereAnError
-
AH. May be getting at least somewhere:
PS Admin>($logError -as [string]).contains($element.UNIQUEID -as [string]) True
I can at least get something to work with this, however is there a better way? I am pretty sure they are both arrays, and am looking for how to check that for sure right now
-
PS Admin>$element.gettype() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True False DataRow System.Object PS Admin>$logError.gettype() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True False MatchInfo System.Object
-
Yep. Core issue was the handling of those two data types. After setting the data types as strings on any compare or entry, works perfectly.
Thanks! -
PowerShell tag?
-
@Tim_G Nice catch. Tagged. Thanks!