Az Powershell 模块 - 说"Cannot find storage account with name xxx"但它存在



我的powershell脚本构建了一个azure函数&所有的依赖关系。

这就是正在发生的事情:(我在这里手动演示…(

我请求存储帐户信息如下:

PS C:Usersme>  Get-AzStorageAccount -ResourceGroupName widget-resource-group                

StorageAccountName     ResourceGroupName PrimaryLocation SkuName      Kind      AccessTier CreationTime          ProvisioningState EnableHttpsTrafficOnly LargeFileShares
------------------     ----------------- --------------- -------      ----      ---------- ------------          ----------------- ---------------------- ---------------
widgetx4ge6v27rlgdk widget-resource-group eastus          Standard_LRS StorageV2 Hot        2022-03-10 2:00:26 PM Succeeded         True

它会返回正确的信息。然后我试着得到这样的连接字符串:

PS C:Usersme> func azure storage fetch-connection-string widgetx4ge6v27rlgdk                 
Cannot find storage account with name widgetx4ge6v27rlgdk 

但它表示找不到存储帐户。实际代码如下:

# Look up function app name that was dynamically created by ARM template:
$AZ_FUNCTION_APP = Get-AzFunctionApp -ResourceGroupName $currentEnv.AZ_RESOURCE_GROUP_NAME
#look up the storage account name for this resource group. 
$AZ_STORAGE_ACCOUNT = Get-AzStorageAccount -ResourceGroupName $currentEnv.AZ_RESOURCE_GROUP_NAME
Write-Output $AZ_STORAGE_ACCOUNT.StorageAccountName
# Get new connection string for the storage account.
func azure storage fetch-connection-string $AZ_STORAGE_ACCOUNT.StorageAccountName

当代码运行时,一切正常,直到调用";func azure存储获取连接字符串";。

关于我遗漏了什么,有什么建议吗?

编辑1

如果它有帮助的话,当我对租户1,订阅A运行它时,这个逻辑运行得很好。但对于租户1,预订B,它会爆炸。我已经确保它运行的服务帐户原则是两个订阅的贡献者。就其价值而言,脚本能够创建资源组和其中的许多资源。这只是帽子,当我试图得到连接字符串,它炸弹。当它试图在我的函数应用程序中部署函数时,它还会在脚本中进一步轰炸。不过,错误消息是类似的——它抱怨找不到我刚刚创建的功能应用程序。

编辑2

所以我发现了这个问题,但不知道如何用一种好的/简单的方式解决它。对于90%的脚本,包括登录,我使用新的Az Powershell模块。然而;func azure";该工具依赖于az-cli提供的登录信息。(似乎已缓存??(

为了让您进入同一页面,以下是脚本中代码的相关部分:

$Credential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $AZ_DEPLOYMENT_CLIENT_ID, $Secure2
#Connect
Connect-AzAccount -ServicePrincipal -TenantId $AZ_TENANT_ID -Credential $Credential
#OPTIONAL - List the subscriptions available to the current User
Get-Azcontext -ListAvailable
#Set the subscription context to subscription 2
Set-Azcontext -Subscription $AZ_SUBSCRIPTION_ID -Tenant $AZ_TENANT_ID
#Create a new resource group
New-AzResourceGroup -Name $AZ_RESOURCE_GROUP_NAME -Location $AZ_RESOURCE_LOCATION -Force
New-AzResourceGroupDeployment -ResourceGroupName $AZ_RESOURCE_GROUP_NAME -TemplateFile (Join-Path $PSScriptRoot "./artifacts/widget-resources.json")
# Look up function app name that was dynamically created by ARM template:
$AZ_FUNCTION_APP = Get-AzFunctionApp -ResourceGroupName $AZ_RESOURCE_GROUP_NAME
#look up the storage account name for this resource group. 
$AZ_STORAGE_ACCOUNT = Get-AzStorageAccount -ResourceGroupName $AZ_RESOURCE_GROUP_NAME
Write-Output $AZ_STORAGE_ACCOUNT.StorageAccountName
# this is where it is failing because it is using a subscription that is visible to az cli.
func azure storage fetch-connection-string $AZ_STORAGE_ACCOUNT.StorageAccountName

以下是我从powershell cli:中进行的故障排除

az account list

返回的是:

{
"cloudName": "AzureCloud",
"homeTenantId": "asdf-asdf-asdf-asdf-12312312313",
"id": "[guid]",
"isDefault": false,
"managedByTenants": [],
"name": "subscription-1",
"state": "Enabled",
"tenantId": "[our-tenant-id]",
"user": {
"name": "[another-guid]",
"type": "servicePrincipal"
}
}

当我运行上面的命令时,它只返回一个名为"的订阅;订阅-1";以供讨论。这不是/不是剧本其余部分正在处理的那个。脚本的其余部分处理订阅2在测试过程中,我在调用func azure storage之前添加了以下几行代码:

az login --service-principal --username $AZ_APPLICATION_CLIENT_ID --password $AZ_SECRET --tenant $AZ_TENANT --allow-no-subscriptions
#set the subscription we want to use
az account set --subscription $subscription2
func azure storage fetch-connection-string $AZ_STORAGE_ACCOUNT.StorageAccountName

现在它找到了正确的订阅和资源组/存储帐户。现在,当我再次运行az帐户列表时,它会显示两个订阅。

添加一条评论/意见。使用所需的订阅id运行az登录/az帐户集后,我注意到我可以从脚本中删除az登录和帐户集逻辑,它只使用缓存的值。我并不是说这就是我想做的。。。因为我认为最好直截了当。但这只是一个观察结果,它解释了最初是什么咬了我。

所以我的问题是……有没有办法避免两次登录——一次使用az-cli,另一次使用az Powerhsell模块?我正在考虑放弃Az Powershell模块,只在Az-cli中重写所有内容。

但要求社区看看是否有更好的方法来做到这一点。

编辑3

根据azure核心功能工具的文档,从技术上讲,我应该能够使用powershell模块或cli:

https://learn.microsoft.com/en-us/azure/azure-functions/functions-run-local?tabs=v4%2Cwindows%2Ccsharp%2Cportal%2Cbash#publish

"您必须在本地安装Azure CLI或Azure PowerShell,才能从Core Tools发布到Azure">

是的,混合使用azcli和azure powershell,因为它们本身就是独立的实体,所以您需要分别登录它们。是的,你是对的,最好抛弃它们,选择其中一个!这样就干净多了

问题是azure核心函数工具正在使用缓存的az帐户列表来查找我的资源。换句话说,在我不知情的情况下,func方法使用的是az-cli,而脚本的其余部分使用的是新的az Powershell模块。

现在,我刚刚用az-cli语法重写了所有内容,对此我很满意。但根据文档,azure核心功能工具似乎应该能够与az-cli或az-powershell一起使用。将提出一个单独的问题来解决这一点。现在,我的脚本又开始工作了。

相关内容

最新更新