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?

2
  • 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. Commented Feb 4, 2021 at 16:10
  • I've voted to close this question as duplicate. Commented Feb 4, 2021 at 16:11

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

3 Comments

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.
Using an apostrophe, should be make this literal, so what you enter in between the two apostrophes is exactly what will appear only.
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.