【发布时间】: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