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.
    • 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