如何在PowerShell中向Azure CLI提供SAS令牌

5
我正在使用PowerShell中的Azure CLI 2.0来管理存储账户。我拥有一个SAS令牌(我将其存储在变量中),并且希望在命令中使用它。以下是我正在运行的脚本:
$sasToken = 'st=2017-11-13T10%3A55%3A06Z&se=2017-11-13T11%3A27%3A06Z&sp=w&spr=https&sv=2017-04-17&sr=c&sig=%2BA6LDTwHes6JdxEAHXSvbYc70y30OcznjMVSyFbCXog%3D'

az storage blob upload `
    --account-name mystorageaccount `
    --container-name mycontainer `
    --file c:\temp\file.txt `
    --name file.txt `
    --sas-token $sasToken

当我运行这个程序时,出现了以下错误:
The specified resource does not exist.
'se' is not recognized as an internal or external command,
operable program or batch file.
'sp' is not recognized as an internal or external command,
operable program or batch file.
'spr' is not recognized as an internal or external command,
operable program or batch file.
'sv' is not recognized as an internal or external command,
operable program or batch file.
'sr' is not recognized as an internal or external command,
operable program or batch file.
'sig' is not recognized as an internal or external command,
operable program or batch file.

我看到 PowerShell 在每次遇到 & 符号时都会截断 SAS token,而 Azure CLI 没有将其视为同一字符串的一部分。

有没有办法强制 PowerShell 将 SAS token 作为完整字符串调用 Azure CLI?

4个回答

8
尝试在SAS令牌外加一层引号: $sasToken = '"st=2017-11-13T10%3A55%3A06Z&se=2017-11-13T11%3A27%3A06Z&sp=w&spr=https&sv=2017-04-17&sr=c&sig=%2BA6LDTwHes6JdxEAHXSvbYc70y30OcznjMVSyFbCXog%3D"'
这样CLI命令看起来就像这样:
--sas-token "st=2017-11-13T10%3A55%3A06Z&se=2017-11-13T11%3A27%3A06Z&sp=w&spr=https&sv=2017-04-17&sr=c&sig=%2BA6LDTwHes6JdxEAHXSvbYc70y30OcznjMVSyFbCXog%3D"

但是如果 SAS 令牌存储在变量中呢? - veuncent

3
我今天早上遇到了这个问题,并且解决了它,当SAS Uri存储在一个变量中时,就像这样:
$sasUri = "`"$pkgSasUri`"" 
....
az deployment group create `
  --resource-group $rg `
  --name create_deployment_cses `
  --template-file .\template.json `
  --parameters .\parameters.json configurationSasUri=$confSasUri packageSasUri=$pkgSasUri

基于Microsoft开发博客

1
如果您从Azure CLI中获取SAS密钥,请注意,如果您使用“-o json”运行它,输出值将在引号中。 您可以直接将其分配给变量,并且可以被Azure CLI使用而没有问题(至少,我可以将其放在AKV秘密中)
这是我编写的函数,在我的ps1代码中获取该变量:
function Get-SASToken {
    param (
        # Storage Account 
        [Parameter(Mandatory=$true)][alias("a")][string]$StorageAccount,
        # Storage Container name
        [Parameter(Mandatory=$true)][alias("c")][string]$StorageContainer,
        # Parameter help description
        [Parameter(Mandatory=$true)][alias("p")][string]$SAPolicy
    )
    echo (az storage container generate-sas --account-name $StorageAccount -n $StorageContainer --policy-name $SAPolicy -o json)

}

0
对于那些遇到相同问题的人,我能够通过以下方法解决它:
$sasTokenEscaped = $IsWindows ? "`"$sasToken`"" : $sasToken

适用于Windows和Linux上的PSCore。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接