1

Trying to pass GitHub secrets to powershell script as parameters but getting *** while executing workflow and it failed:

- name: Connecting to shared drive
  env:
    username: ${{secrets.TEST_USERNAME }}
    password: ${{secrets.TEST_PASSWORD }}
  run: ${{ github.workspace }}\Connect-Drive.ps1 ${{ env.username }} ${{ env.password }}

Getting this error while executing:

The term '***' is not recognized as a name of a cmdlet, function, script file, or executable
Azeem
  • 11,148
  • 4
  • 27
  • 40
Sagar
  • 51
  • 1
  • 8
  • 2
    That is a message from powershell letting you know that parameters were not properly quoted ("escaped"). It writes `***` because secrets in the output are hidden (by GH). Not a powershell expert, but [from what I could gather here](https://stackoverflow.com/a/59681993/367456), wrapping the expressions in double or single quotes may work already (is technically not 100% correct for all contents of the expressions, but I have no idea where this is properly documented in some microsoft user-manual, that you can also link online too). `Connect-Drive.ps1 '${{ env.username }}' '${{ env.password }}'` – hakre Apr 13 '23 at 18:25
  • 2
    Sagar, also check the [new answer from Azeem below](https://stackoverflow.com/a/76012144/367456), it looks like a good suggestion to me and also some additional discussion on the quoting /cc @Azeem – hakre Apr 14 '23 at 07:39

2 Answers2

2

Regarding those asterisks ***, according to Accessing your secrets:

Warning: GitHub automatically redacts secrets printed to the log ...

So, those secrets are automatically being redacted in logs.

After setting the secrets as environment variables using env, you can use those directly with $Env:<variable> syntax instead of using ${{ env.variable }}:

- name: Connecting to shared drive
  env:
    username: ${{ secrets.TEST_USERNAME }}
    password: ${{ secrets.TEST_PASSWORD }}
  run: |
    "$Env:GITHUB_WORKSPACE\Connect-Drive.ps1" "$Env:username" "$Env:password"

Need double quotes around $Env:<variable> to be expandable.

See the Default environment variables for GITHUB_WORKSPACE.

If you do use the env context, make sure to use quotes. In this case, prefer single quotes as the values may contain literals expandable by the shell. See the Quoting Rules for Powershell 7.2 for more details as GHA-hosted Windows runners have Powershell 7.2.10 preinstalled.

Azeem
  • 11,148
  • 4
  • 27
  • 40
0

I think powershell command needs to be surrounded by $(...)

Try this:

run: $(${{github.workspace}}\Connect-Drive.ps1 ${{env.username}} ${{env.password}})
Justin
  • 927
  • 11
  • 24
  • 1
    Why try this? How does this answer the question? Please add an explanation. – hakre Apr 14 '23 at 07:41
  • as powershell script needs to be surrounded by $( ) – Justin Apr 15 '23 at 08:03
  • Please edit in all the important details to your answer (and - but that's only me - if you could drop a link to a reference of some kind for the rationale, e.g. a link to some docs, would be fine). – hakre Apr 15 '23 at 08:23