'Escape double quotes in a command within multiline string concatenation

I am trying to run psql \copy command from within an Azure DevOps pipeline which is working fine for all my tables but the table where our table name contains dashes: my-table-name.

Task script:

- task: PowerShell@2
    displayName: "Populate data"
    inputs:
      targetType: 'inline'
      script: |
        #Setup PostgreSQL
        Set-Service postgresql-x64-14 -StartupType manual
        Start-Service postgresql-x64-14
        Get-CimInstance win32_service | Where-Object Name -eq "postgresql-x64-14"
        #Create tables in PostgreSql database
        $PG_SQL = "C:\Program Files\PostgreSQL\14\bin\psql.exe";
        & $PG_SQL -d abcd -U abcd -c "\copy \"my-table-name\" from '$(build.artifactstagingdirectory)\dump.csv' with (format csv,header true,delimiter ';')";

With a hope to achieve (this works when ran within the server):

psql -d abcd -U abcd -c "\copy \"my-table-name\" from 'directory\dump.csv' with (format csv,header true,delimiter ';')"

Error:

ERROR:  syntax error at or near "-"
LINE 1: COPY  my-table-name FROM STDIN with (format csv,header true...
                ^

What I tried:

  1. Backtick: & $PG_SQL -d abcd -U abcd -c "\copy "my-table-name" from '$(build.artifactstagingdirectory)\dump.csv' with (format csv,header true,delimiter ';')"; -> gives above mentioned error
  2. Forward slash: & $PG_SQL -d abcd -U abcd -c "\copy \"my-table-name\" from '$(build.artifactstagingdirectory)\dump.csv' with (format csv,header true,delimiter ';')"; -> gives above mentioned error
  3. Double double quotes: & $PG_SQL -d abcd -U abcd -c "\copy ""my-table-name"" from '$(build.artifactstagingdirectory)\dump.csv' with (format csv,header true,delimiter ';')"; -> gives above mentioned error
  4. Pre-storing in variable and then surrounding variable with double quotes: $cpy_cmd = '\copy \"my-table-name\" from D:\a\1\a\my-table-name.csv with (format csv,header true,delimiter \";\")'; & $PG_SQL -d abcd -U abcd -c "$cpy_cmd"; -> works, however here I have to use the static path to the file instead of the cmdlet(is this how I should call it?)


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source