0

I need to write the full URI of an Azure storage blob - with a SAS token - from a variable into a file, and read it out on a subsequent run of a script (on the other side of a reboot). Because SAS token URIs contain at least a single ampersand, the variable output into the file is getting truncated at the first one;

$sasTokenURI | out-file "c:\sasTokenURI.txt"

Writes the URI up to and excluding the first ampersand.

How can I force Powershell to verbatim output the URI to a file and then read it verbatim in later?

0

I am not sure my understanding is right, if my answer has some wrong please point me. The following step works for me, maybe you could check.

$StorageAccountName = "shuilinuxdiag336"
$StorageAccountContext = (Get-AzureRmStorageAccount | Where-Object{$_.StorageAccountName -eq $StorageAccountName}).Context
$StorageContainerName = "customscriptfiles"

$StartTime = Get-Date
$EndTime = (Get-Date).AddHours(4)
$blobName = "install_nginx_ubuntu.sh"
$SASURI = New-AzureStorageBlobSASToken -Container $StorageContainerName `
    -Blob $blobName `
    -Context $StorageAccountContext `
    -Permission "rwd" `
    -StartTime $StartTime `
    -ExpiryTime $EndTime `
    -FullUri

$SASURI | out-file "C:\Users\v-shshui\test.txt"

$sasTokenURI = cat "C:\Users\v-shshui\test.txt"

Invoke-WebRequest -Uri $sasTokenURI -OutFile $blobName

I test in my lab, it works for me.

enter image description here

  • If you add -FullURI to $ArtifactsLocationSasToken so that it sets the entire URL to be blob, with the token, not just the token part, write that to the file, then read it out and use it with Get-WebContent do you get to download the blob? – Elomis Jan 29 '18 at 7:01
  • Yes, you could. I use Invoke-WebRequest to download the file, it works for me fine. I update my answer, you could check it. – Shui shengbao Jan 29 '18 at 7:28
0

Looks like the URI wasn't being chopped off by the setup script, it was getting chopped off when being passed to the setup script through a template. Ampersands in Powershell are reserved for some mysterious future use.

I stopped it doing it (for anyone with the same problem) by using .replace("&","@") throughout and only reversing the replace before I invoked a web request. It's working fine now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.