I need to check if 2 variable values ​​are the same.

Welcome Forums General PowerShell Q&A I need to check if 2 variable values ​​are the same.

Viewing 3 reply threads
  • Author
    Posts
    • #185087
      Participant
      Topics: 3
      Replies: 2
      Points: 41
      Rank: Member

      I have a script that asks for login ($ user) and password ($ password). Based on this, it executes a sql query with the result being a password ($ result)

      $query = "select haslo from uzytkownicy where login='$user'"
      
      $result = Get-ODBC-Data -query $query
      
      if ($password -eq $result){
      
      Write-Output "ok" }
      
      else {
      
      Write-Output "nok"
      
      }
      

      My problem is that even if $ password is the same as $ result, I have a nok. I suppose the problem is that the result of the query is 

      (write-output $ result)

      haslo

      ———

      testowehaslo

      ( write-output $password )

      testowehaslo

      so there are 2 additional lines above the password. Can anyone tell me what I'm doing wrong?

    • #185111
      Participant
      Topics: 4
      Replies: 99
      Points: 210
      Helping Hand
      Rank: Participant

      Please use the pre tags to format your code and use $result.haslo instead of $result

    • #185120
      Participant
      Topics: 3
      Replies: 2
      Points: 41
      Rank: Member

      It works. Thx.

    • #185222
      Senior Moderator
      Topics: 8
      Replies: 1141
      Points: 3,928
      Helping Hand
      Rank: Community Hero

      It works. Thx.

      sharing the problem and solution for others.

      $password is a string and $result is an object, hence the comparison fails. when doing it like this below is what actually happens.

      since $Password is a string, then the $result will be converted to string(internally) to do a comparison and will result in to @{haslo
      ='testowehaslo'}.

      So, solution is to cherrypick the value like $result.haslo,

      if($password -ew $result.haslo)
      {
      # whatever
      }
      
Viewing 3 reply threads
  • You must be logged in to reply to this topic.