Powershell Issue Comparing Array Variables
-
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!