0

I have a small Powershell script that uploads a file to blob storage. The idea is to run this as an inline script inside Devops once a build has been completed instead of having the path/file hardcoded like now.

If I run this in a Powershell command prompt on my computer, it works fine.

az storage blob upload --% --container-name mycontainer --account-name myaccount --name "File.zip" --file "c:\Projects\File.zip" --sas-token "{my token}"

However, I want to exchange the hardcoded path+file with a variable that my build pipleline can set. But here is where I haven't figured out how to actually use the variable.

The following does not work when I try to run it locally. To test I created a variable and then made a call.

// Create variable
New-Variable -Name "TestFile" -Value "c:\projects\File.zip"
(enter)
// Try to upload it
az storage blob upload --% --container-name mycontainer --account-name myaccount --name "File.zip" --file $TestFile --sas-token "{my token}"

Results in:

FileOperationError: [WinError 2] The system cannot find the file specified: '$TestFile'

I am assuming that I need to declare it in a different way or pipe it to make it work, but how?

Patrick
  • 5,442
  • 9
  • 53
  • 104
  • You are right @zett42. Moving --% to just before the token (which is why I have it there) solved the issue. If you write it as a reply I can mark it as resolved. – Patrick Feb 04 '21 at 16:10
  • I've voted to close this question as duplicate. – zett42 Feb 04 '21 at 16:11

1 Answers1

0

Use a speech mark at the beginning, and at the end of your variable, for example,"$TestFile"

Shaqil Ismail
  • 1,794
  • 1
  • 4
  • 5
  • Same result unfortunately. I have tried --file $TestFile, --file "$TestFile" and --file '$TestFile' but they all yield the same result. My guess is that the value is not resolved so it actually think $TestFile is the value itself. – Patrick Feb 04 '21 at 16:02
  • Using an apostrophe, should be make this literal, so what you enter in between the two apostrophes is exactly what will appear only. – Shaqil Ismail Feb 04 '21 at 16:49
  • When using the `New-Variable` command, I know using the `Get-Variable` would work to use the variable, which was created with the `New-Variable` command. – Shaqil Ismail Feb 05 '21 at 11:27