使用 Azure Powershell 函数列出我所有的 Azure 网站

List all my Azure websites using an Azure Powershell Function

我正在测试新的 Azure Functions,并想编写一个 return 我所有 Azure 网站的函数。但不用说我 运行 遇到了一些问题,文档仍然很少。

运行.ps1

# Get the input request
$in = Get-Content $req -Raw | ConvertFrom-Json

Write-Output "Loading..."

Get-AzureRmSubscription -SubscriptionId $in.SubscriptionId | Select-AzureRmSubscription
$Result = Get-AzureWebsite
Write $Result

这个函数以订阅id为参数,用来列出可用的网站。但是我得到了这个例外。

2017-06-13T12:43:57.763 Get-AzureRmSubscription : Run Login-AzureRmAccount to login.

所以我尝试添加 Login-AzureRmAccount 但后来我得到了。

2017-06-13T12:45:04.959 Login-AzureRmAccount : Error HRESULT E_FAIL has been returned from a call to a COM component.

这就是我现在的立场。

更新

在@4c74356b41 的帮助下,我现在可以登录了。我的登录代码如下所示。

$subscriptionId = "<SubscriptionId>"
$tenantid = "<TenantId>"
$clientid = "<ApplicationId>"
$password = "<Password>"
$userPassword = ConvertTo-SecureString -String $password -AsPlainText -Force
$userCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $clientid, $userPassword
Add-AzureRmAccount -TenantId $tenantid -ServicePrincipal -SubscriptionId $subscriptionId -Credential $userCredential

当我测试代码时,我可以看到这项工作。但是只要我添加这一行。

Select-AzureSubscription -Current -SubscriptionId $subscriptionId

我得到这个异常。

Select-AzureSubscription : The subscription id <SubscriptionId> doesn't exist.
Parameternavn: id
At line:11 char:1
+ Select-AzureSubscription -Current -SubscriptionId $subscriptionId
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Select-AzureSubscription], ArgumentException
    + FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.Profile.SelectAzureSubscriptionCommand

我也试过添加这一行。

Get-AzureRmSubscription –SubscriptionId $subscriptionId | Select-AzureRmSubscription

看起来好像在工作,它只发出警告 WARNING: Unable to acquire token for tenant 'Common' 但仍然列出正确的订阅详细信息,没有任何例外。

然后当我尝试

Get-AzureWebsite

我得到这个异常。

Get-AzureWebsite : No default subscription has been designated. Use Select-AzureSubscription -Default <subscriptionName> to set the default subscription.
At line:15 char:1
+ Get-AzureWebsite
+ ~~~~~~~~~~~~~~~~
    + CategoryInfo          : CloseError: (:) [Get-AzureWebsite], ApplicationException
    + FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.Websites.GetAzureWebsiteCommand

那么,您希望如何在不进行身份验证的情况下使用您的订阅?您希望任何人无需任何验证就可以修改您的资源吗?所以你需要在做任何事情之前进行身份验证。

在 Azure Function 中使用 powershell 与在您的计算机上使用 powershell 没有什么不同(模块管理除外)。

要登录,您可以使用 service principal auth ang login,方法如下:

Add-AzureRmAccount -TenantId $tenantid -ServicePrincipal -SubscriptionName $name `
   -Credential ([pscredential]::new($clientid,(ConvertTo-SecureString -String $password -AsPlainText -Force)))

您可以用环境变量替换变量(硬编码在代码中)。

添加 4c74356b41 的答案,Get-AzureWebsite 是一个 Azure 经典模式 cmdlet。现在,您登录您的 ARM 订阅,因此,它需要您登录经典订阅。 Select-AzureSubscription 是用于 select 经典订阅的经典 cmdlet。

在Azure ARM模式下,网站更名为Webapp,您可以查看Azure App Service announcement

所以,如果你想列出你所有的 webapp,你应该使用 cmdlet Get-AzureRmWebApp

更多信息请参考此link:Using Azure Resource Manager-Based PowerShell to Manage Azure Web Apps