ML
    • Recent
    • Categories
    • Tags
    • Popular
    • Users
    • Groups
    • Register
    • Login

    Powershell Issue Comparing Array Variables

    IT Discussion
    powershell
    3
    11
    1.8k
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • FiyaFlyF
      FiyaFly
      last edited by FiyaFly

      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 variables

      PS 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?

      1 Reply Last reply Reply Quote 0
      • Mike DavisM
        Mike Davis
        last edited by

        What happens if you do:
        $IsThereAnError = $logError.Contains($element)
        $IsThereAnError

        $logError might have to be converted to a string

        1 Reply Last reply Reply Quote 0
        • Mike DavisM
          Mike Davis
          last edited by

          If that doesn't work, try:

          $IsThereAnError = ($logError -as [string]).Contains($element)
          $IsThereAnError
          
          1 Reply Last reply Reply Quote 0
          • FiyaFlyF
            FiyaFly
            last edited by

            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?

            1 Reply Last reply Reply Quote 0
            • FiyaFlyF
              FiyaFly
              last edited by

              PS Admin>$IsThereAnError = ($logError -as [string]).Contains($element)
              PS Admin>$IsThereAnError
              False
              
              1 Reply Last reply Reply Quote 0
              • Mike DavisM
                Mike Davis
                last edited by

                scrolled back and took a better look. Try:

                $IsThereAnError = ($logError -as [string]).Contains($element.UNIQUEID)
                $IsThereAnError
                
                1 Reply Last reply Reply Quote 0
                • FiyaFlyF
                  FiyaFly
                  last edited by

                  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

                  1 Reply Last reply Reply Quote 0
                  • FiyaFlyF
                    FiyaFly
                    last edited by

                    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
                    
                    1 Reply Last reply Reply Quote 0
                    • FiyaFlyF
                      FiyaFly
                      last edited by

                      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!

                      1 Reply Last reply Reply Quote 0
                      • ObsolesceO
                        Obsolesce
                        last edited by

                        PowerShell tag?

                        FiyaFlyF 1 Reply Last reply Reply Quote 0
                        • FiyaFlyF
                          FiyaFly @Obsolesce
                          last edited by

                          @Tim_G Nice catch. Tagged. Thanks!

                          1 Reply Last reply Reply Quote 0
                          • 1 / 1
                          • First post
                            Last post