【问题标题】:List all my Azure websites using an Azure Powershell Function使用 Azure Powershell 函数列出我的所有 Azure 网站
【发布时间】:2017-06-13 12:48:33
【问题描述】:

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

run.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

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

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

【问题讨论】:

  • 另外,您是否提供了任何参数?即,如果您有多个与您的帐户关联的订阅,您可能需要运行 Login-AzureRmAccount -SubscriptionId $in.SubscriptionId 以确保您登录到正确的租户/订阅。
  • 不,我没有。我通过 Azure 门户运行它,只看到控制台输出。像这个例子raw.githubusercontent.com/dfinke/images/master/…
  • 啊;这可以解释 com 错误;即它试图打开一个窗口,同时仅限于控制台。尝试使用您的详细信息传递凭据参数。请参阅相关代码示例的答案。
  • @Martin Get-AzureWebsite 是经典的 cmdlet,您应该使用 Get-AzureRmWebApp 列出您的 webapp。你可以看看我的回答。
  • @Martin cmdlet Get-AzureRmWebApp 是否适合您?

标签: powershell azure azure-functions


【解决方案1】:

那么,您希望如何在不进行身份验证的情况下使用您的订阅?您是否希望任何人都能够在没有任何验证的情况下修改您的资源?所以你需要在做任何事情之前进行身份验证。

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

要登录,您可以使用 service principal auth ang 登录,例如:

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

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

【讨论】:

  • 谢谢,我会调查的。我想既然 Azure Function 已经需要一个密钥才能运行,我认为这是需要的身份验证。
  • 不,azure 函数不知道您的 azure 订阅;)
  • 因为我是新手。我可以为此创建服务用户吗?然后我是否必须创建一个 Azure Active Directory 才能使其正常工作?
  • 这是很多问题。我提供的链接涵盖了端到端的流程。如果没有 azure 广告,您就不能拥有 azure 订阅,它是在您创建订阅时创建的(所以它已经为您准备好了)
  • 谢谢,您的登录回答确实解决了问题,Walter 的Get-AzureRmWebApp 关闭了它。谢谢你们。
【解决方案2】:

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

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

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

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

【讨论】:

    猜你喜欢
    • 2022-01-20
    • 1970-01-01
    • 2021-10-10
    • 2016-12-06
    • 1970-01-01
    • 2014-12-30
    • 2015-04-13
    • 2021-04-16
    • 1970-01-01
    相关资源
    最近更新 更多