如何使用函数应用程序在Azure中使用PowerShell Login-AzureRmAccount

问题描述:

与本教程相关(Using PowerShell Modules in Azure Functions)我可以在我的功能应用程序中运行PowerShell。现在我想从Microsoft运行这个PowerShell教程(Build your first Azure data factory using Azure PowerShell)。在第一步,我要运行这个PowerShell的代码:如何使用函数应用程序在Azure中使用PowerShell Login-AzureRmAccount

PowerShell Login-AzureRmAccount 
PowerShell Get-AzureRmSubscription 
#Run the following command to select the subscription that you want to work with. This subscription should be the same as the one you used in the Azure portal. 
PowerShell Get-AzureRmSubscription -SubscriptionName <SUBSCRIPTION NAME> | Set-AzureRmContext 

问题是,这个教程是在(使用PoserShell从Windows)本地机器使用PowerShell的,但我需要使用功能的Apps运行本教程,然后我需要改变场景...
你能指导我如何改变或使用Login-AzureRmAccountGet-AzureRmSubscription

默认情况下,Login-AzureRmAccount会进行交互式登录,这在Azure函数中不起作用。相反,您需要使用服务主体进行登录,例如

Login-AzureRmAccount -ServicePrincipal -ApplicationId "http://my-app" -Credential $pscredential -TenantId $tenantid 

Azure中的PowerShell docs对此有更多的信息。

至于选择订阅,交互式或功能之间没有区别。

请记住,PowerShell是Azure函数中的一种实验性语言,并未得到完全支持。

Azure功能是一种应用服务,只拥有有限的权限。如果您想要使用它登录Azure,则可能需要使用Azure Keyvault服务原则以帮助您自动登录Azure。 有主要的步骤,你需要做的:

  1. 使用PowerShell创建自签名证书并将其导出到本地

  2. 使用powerhshell创建一个服务主体为自动登录

  3. 上传自签证书指纹并为天蓝色功能配置SSL(上传PFX文件)

  4. 创建keyvault并为服务主体

  5. 创建蔚蓝的功能应用的访问策略,然后使用自签名的证书指纹,应用标识等信息在蔚蓝的功能自动登录 。最后,您可以使用cmdlet Get-AzureKeyVaultSecret至 检索证书的内容并重建证书。

你可以参考this article创建Azure的Keyvault和服务理念。然后,您可以在功能应用程序中使用PowerShell自动登录Azure。

添加PowerShell将您的功能应用和配置所有设置后,就可以运行这些脚本:Login-AzureRmAccount -ServicePrincipal -CertificateThumbprint $certThumbprint -ApplicationId $appId -TenantId $tenantId

enter image description here

查看关于Azure的服务理念更多细节this document

This blog也可以对你有所帮助。

注:自签名证书需要被导入到通过门户网站蔚蓝的功能,并与WEBSITE_LOAD_CERTIFICATES在设置应用

+0

嗨,@Reza阿米亚,没尝试这种解决方案配置?我可否知道你的问题是否已解决? –