Azure Powershell程序化登录

时间:2016-08-03 20:34:22

标签: powershell azure

我正在使用Azure(特别是HDInsight)使用个人帐户(没有工作/学校代办)。

我会创建一个自动登录azure并执行某些操作的脚本。

我在使用我们的凭据记录后找到了保存azure publishsetting json文件的解决方案,但此设置文件包含到期的令牌。

我该如何处理这个问题?完成此自动登录的最佳方法是什么?

由于 罗伯特

4 个答案:

答案 0 :(得分:1)

您需要创建服务主体。创建服务主体后,您可以使用Role-Based Access Control为特定资源分配权限。从那里,您的脚本可以作为服务主体登录,而无需您以交互方式登录。

此方法的主要问题是保护对脚本的访问,因为它包含允许访问Azure资源的凭据。

这个article有一个很好的演练:

#First, login as yourself so you can setup the service principal
Login-AzureRmAccount

#Password doesn't have to be *your* password, but the password the script will use
$app = New-AzureRmADApplication –DisplayName "<Your script name>" –HomePage "http://localhost" –IdentifierUris "http://localhost/YourAppName" –Password "<Password>"

#Create the service principal
New-AzureRmADServicePrincipal –ApplicationId $app.ApplicationId

#Assign the Reader role to your new service principal.  Other roles listed at
#https://azure.microsoft.com/en-us/documentation/articles/role-based-access-built-in-roles/
New-AzureRmRoleAssignment –RoleDefinitionName Reader –ServicePrincipalName $app.ApplicationId

$pass = ConvertTo-SecureString "<Password>" -AsPlainText –Force

#Servce principal username looks like 92c22f1f-d1d4-46a1-b025-edb47fc03809@something.onmicrosoft.com
#the GUID part is $app.ApplicationId and the domain part is found in the Azure portal
$cred = New-Object -TypeName pscredential –ArgumentList "<Service Principal UserName>", $pass

Login-AzureRmAccount -Credential $cred -ServicePrincipal –TenantId <TenantId>

答案 1 :(得分:0)

如果它不是生产/共享设置,而且还有更多开发人员设置,您也可以这样做,小心,密码是纯文本:

$SubscriptionName = 'MySubscription'
$pswd  = 'MyPassword' | ConvertTo-SecureString -AsPlainText -Force
$creds = New-Credential -UserName 'MyEmail@something.com' -Password $pswd
Add-AzureRmAccount -Credential $creds
Set-AzureRmContext -SubscriptionName $SubscriptionName
Login-AzureRmAccount -Credential $creds -SubscriptionName $SubscriptionName

答案 2 :(得分:0)

以下是一些可以启动的命令。

<web:FromCurrency>

答案 3 :(得分:0)

以下信息可能会对您有所帮助

  • 在Azure中创建自动化帐户
  • 将自动帐户中的凭据添加为变量(例如 variablename = loginazure)脚本下方将自动登录azure(使用Powershell工作流程Runbook)。

    $AzureLogin = Get-AutomationPSCredential -Name 'loginazure'  
    $AzurePortalLogin = New-Object -TypeName   System.Management.Automation.PSCredential$AzureLogin  
    Add-AzureRmAccount -Credential $AzurePortalLogin   
    Get-AzureRmSubscription -SubscriptionName "your subscription name" | Set-AzureRmContext
    

Inline Script {}

中使用上述脚本

问候

Thamarai Selvan S

相关问题