0

I'm trying to use comparison of variables in workflow rule section. My job:

variables:

  isDBExists: 0
  compareValue: 0

job1:
 stage: createVar  
 script:
    - Import-Module C:\SqlServer.21.1.18221\sqlserver 
    - $isDBExists = Invoke-sqlcmd -Query "SELECT 1" -ConnectionString $CONNSTR | format-table -property column1 -HideTableHeaders
    - $compareValue = Invoke-sqlcmd -Query "SELECT 1" -ConnectionString $CONNSTR | format-table -property column1 -HideTableHeaders
    - $isDBExists = [int]($isDBExists | Out-String).Trim()
    - $compareValue = [int]($compareValue | Out-String).Trim()


job2:
  stage: compare
  tags:
    - ps
  script:
    - Import-Module C:\SqlServer.21.1.18221\sqlserver 
    - Invoke-Sqlcmd -ConnectionString $CONN -InputFile $script
  rules:
    - if: $isDBExists == $compareValue
      when: on_success



Why $isDBExists and $compareValue are not equial? I tried to compare by strings, checked the data types with the gettype() function, checked the values, they are equal in powershell. Thanks

andrey
  • 23
  • 4
  • [`Format-Table`](https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/format-table) should only be used for *displaying* information and not be assigned to a variable. I guess you want to do something like: `Invoke-sqlcmd -Query "SELECT 1" -ConnectionString $CONNSTR | Select-Object -Expand Column1`. See [Select the values of one property on all objects of an array in PowerShell](https://stackoverflow.com/a/5176941/1701026) – iRon Dec 20 '22 at 10:43
  • thank you for your answer, I tried to do this method, it turned out more succinctly, but I noticed strange behavior. If the compared variables are initialized with the same value, then everything works correctly, if the values are different (I tried strings and integers), then the values of the variables are not equal (after re-assigning values to variables, I checked their values before comparison - they are equal) – andrey Dec 20 '22 at 18:06

0 Answers0