Skip to content

Instantly share code, notes, and snippets.

@junichia
Last active May 19, 2018 07:52
Show Gist options
  • Save junichia/08c11ede1329ba08b39e9d7436434f92 to your computer and use it in GitHub Desktop.
Save junichia/08c11ede1329ba08b39e9d7436434f92 to your computer and use it in GitHub Desktop.
【20180519 改】AccessToken を使用して Login-AzureRMAccount / Add-AzureRMAccount を実行する ref: https://qiita.com/junichia/items/969a10b2d7beea923b50
Login-AzureRMAccount
$UserID = "userid@company.com"
$UserPass = ConvertTo-SecureString "password" -AsPlainText -Force
$cred = New-Object System.Management.Automation.PsCredential $UserID,$UserPass
Login-AzureRmAccount -Credential $cred
Login-AzureRmAccount : AADSTS50079: The user is required to use multi-factor authentication.
Trace ID: 7c047e1d-4da8-4c7a-8e0e-b7bdcf1a2200
Correlation ID: 8732b1bc-4247-4e7f-876d-9a1ee75e18d5
Timestamp: 2017-08-31 15:55:25Z: リモート サーバーがエラーを返しました: (400) 要求が不適切です
発生場所 行:4 文字:1
+ Login-AzureRmAccount -Credential $cred
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [Add-AzureRmAccount]、AadAuthenticationFailedException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.Profile.AddAzureRMAccountCommand
Get-AzureRmVM : Run Login-AzureRmAccount to login.
発生場所 行:5 文字:1
+ Get-AzureRmVM
+ ~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [Get-AzureRmVM]、PSInvalidOperationException
+ FullyQualifiedErrorId : InvalidOperation,Microsoft.Azure.Commands.Compute.GetAzureVMC
$ClientId = "09f50028-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
$ClientSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx="
$SubscriptionId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
$TenantId = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
$ClientCredential = new-object "Microsoft.IdentityModel.Clients.ActiveDirectory.ClientCredential" $ClientId,$ClientSecret
$authority = "https://login.windows.net/" + $TenantId
$Resource = "https://management.azure.com/"
$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" $authority,$false
$authResult = $authContext.AcquireToken($Resource, $ClientCredential)
Login-AzureRmAccount -AccessToken $authResult.AccessToken -AccountId $AuthClientId -SubscriptionId $SubscriptionId
$ClientId = "xxxxxxxx-xxxx-xxxx-xxxx-fe7037d73d68"
$redirectUri = "https://Login-AzureRMAccount"
$SubscriptionId = "xxxxxxxx-xxxx-xxxx-xxxx-bd98aef0acbe"
$TenantId = "xxxxxxxx-xxxx-xxxx-xxxx-c2df404a71ee"
$authority = "https://login.windows.net/" + $TenantId
$resourceId = "https://management.azure.com/"
$authContext = New-Object "Microsoft.IdentityModel.Clients.ActiveDirectory.AuthenticationContext" $authority,$false
$authResult = $authContext.AcquireToken($resourceId, $ClientId, $redirectUri)
Login-AzureRmAccount -AccessToken $authResult.AccessToken -AccountId $ClientId -SubscriptionId $SubscriptionId
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment