2016-04-18 16 views
3

通常、ポータルにログインするアカウントを作成してMSOLにログインする(Azure ADの場合はLive IDを使用できないため)そのアカウントを共同管理者にしてから、MSOLにログインします。アカウントを作成し、PowershellからMSOLにサインインする

これらの手順は完全にPowershellで実行できますか?

ライブIDでログインしてから、アカウントを作成することができます。純粋にPowershellからAADにログインできます。つまり、新しいAzure契約から、ポータルの近くに行くことなくAADにログインすることができます。

これまでのところ、サービスプリンシパルを作成することしか考えていませんでしたが、MSOLのポータルまたは管理者アカウントなしでそのディレクトリのアクセス許可を与える方法を理解していません。

これができない場合、これがなぜ不可能であるかに関する標準的な答えで十分です。

答えて

2

グラフAPIを使用して、サブスクリプションのデフォルトADにユーザーを追加してから、REST APIを使用してそのユーザーを従来の管理者に割り当てることができます。ここに私が書いたPowerShellスクリプトがあります。

$subscriptionID = "<the Subscription ID>" 

# This is the tenant id of you subscription 
$tenantID = "<the tenant id of your subscription>" 

# The login endpoint. It can be https://login.microsoftonline.com/, too. $loginEndpoint = "https://login.windows.net/" 

# This is the resource URI for Graph API. 
$graphResourceURI = "https://graph.windows.net/" 

# This is the resource URI for Azure Management REST API. It can be https://management.azure.com/ for ARM 
$managementResourceURI = "https://management.core.windows.net/" 

# The redirect URI for PowerShell 
$redirectURI = "urn:ietf:wg:oauth:2.0:oob" 

# The common client id. 
$clientID = "1950a258-227b-4e31-a9cf-717495945fc2" 

# the URL for requesting the Authorization code. 
$authorizeURLGraph = $loginEndpoint+$tenantID+"/oauth2/authorize?response_type=code&client_id="+$clientID+"&resource="+[system.uri]::EscapeDataString($graphResourceURI)+"&redirect_uri="+[system.uri]::EscapeDataString($redirectURI) 

# Create an IE session in PowerShell 
$ie = new-object -ComObject "InternetExplorer.Application" 

# Set the IE session to be silent, so that it won't prompt for confirmation. 
$ie.silent = $true 

# Browsing the URL for requesting the Authorization code. 
$ie.navigate($authorizeURLGraph) 
while($ie.Busy) { Start-Sleep -Milliseconds 100 } 

# Getting the Parameters from the redirect URL. 
$parameters = $ie.LocationURL.Substring($redirectURI.length + 1).split("{&}") 

# Identify Authorization code. 
foreach ($parameter in $parameters){ 
    if ($parameter.substring(0,5) -eq "code="){ 
     $code = $parameter.substring(5) 
     break 
    } 
} 

# the URL for requesting access token. 
$tokenURL = $loginEndpoint+$tenantID+"/oauth2/token" 

# the token request body. 
$body = "grant_type=authorization_code&client_id="+$clientID+"&code="+$code+"&redirect_uri="+[system.uri]::EscapeDataString($redirectURI)+"&resource="+[system.uri]::EscapeDataString($graphResourceURI) 

# the token request headers. 
$headers = @{"Content-Type"="application/x-www-form-urlencoded"} 

# Acquiring an access token. 
$authenticationResult = Invoke-RestMethod -Method POST -Uri $tokenURL -Headers $headers -Body $body 

# Use the access token to setup headers for your http request. 
$authHeader = $authenticationResult.token_type + " " + $authenticationResult.access_token 
$headers = @{"Authorization"=$authHeader; "Content-Type"="application/json"} 

# Create a user. 
Invoke-RestMethod -Method POST -Uri "https://graph.windows.net/$tenantID/users?api-version=1.6-internal" ` 
         -Headers $headers -InFile ./user.json 

# The same as above, except the resource URI. 
$authorizeURLGraph = $loginEndpoint+$tenantID+"/oauth2/authorize?response_type=code&client_id="+$clientID+"&resource="+[system.uri]::EscapeDataString($managementResourceURI)+"&redirect_uri="+[system.uri]::EscapeDataString($redirectURI) 

$ie = new-object -ComObject "InternetExplorer.Application" 

$ie.silent = $true 

$ie.navigate($authorizeURLGraph) 
while($ie.Busy) { Start-Sleep -Milliseconds 100 } 

$parameters = $ie.LocationURL.Substring($redirectURI.length + 1).split("{&}") 

foreach ($parameter in $parameters){ 
    if ($parameter.substring(0,5) -eq "code="){ 
     $code = $parameter.substring(5) 
     break 
    } 
} 

$tokenURL = $loginEndpoint+$tenantID+"/oauth2/token" 

$body = "grant_type=authorization_code&client_id="+$clientID+"&code="+$code+"&redirect_uri="+[system.uri]::EscapeDataString($redirectURI)+"&resource="+[system.uri]::EscapeDataString($managementResourceURI) 

$headers = @{"Content-Type"="application/x-www-form-urlencoded"} 

$authenticationResult = Invoke-RestMethod -Method POST -Uri $tokenURL -Headers $headers -Body $body 

$authHeader = $authenticationResult.token_type + " " + $authenticationResult.access_token 
$headers = @{"Authorization"=$authHeader; "Content-Type"="application/json"} 

# Assign the new user to be co-admin. 
Invoke-RestMethod -Method PUT -Uri "https://management.azure.com/subscriptions/$subscriptionID/providers/Microsoft.Authorization/classicAdministrators/newAdmin?api-version=2015-06-01" ` 
         -Headers $headers -InFile ./admin.json 

ここには、user.jsonとadmin.jsonのサンプルがあります。

user.json:

{ 
    "accountEnabled": true, 
    "displayName": "graphtest", 
    "mailNickname": "graphtest", 
    "passwordProfile": { 
    "password": "Test1234", 
    "forceChangePasswordNextLogin": false 
    }, 
    "userPrincipalName": "[email protected]<subdomain>.onmicrosoft.com" 
} 

admin.json

このスクリプトを使用する前に、あなたがIEでライブIDにログインする必要がありますので、このPowerShellスクリプトは、あなたのIEのセッションに依存
{ 
    "properties": { 
    "emailAddress": "[email protected]<subdomain>.onmicrosoft.com", 
    "role": "CoAdministrator" 
    }, 
    "type": "Microsoft.Authorization/classicAdministrators", 
    "name": "newAdmin" 
} 

。私はまだプライベートブラウジングを見ています。うまくいけば、私はIEセッションではなく、PowerShellでログインすることができます。

+0

Live IDを使用していないため、これは本当に問題には答えませんが、興味深いコードです。 –

+0

ライブIDを使用する場合は、ユーザーの作成をスキップして、 'admin.json'のemailAddressを自分のライブIDに変更することができます。しかし、私のPowerShellスクリプトは使いやすいです。 –

+0

PowerShellでライブIDを作成しますか? –

関連する問題