既存のサービス プリンシパルの取得

サービス プリンシパルを一覧表示する

使いたい既存のサービス プリンシパルが既にある場合は、この手順で既存のサービス プリンシパルを取得する方法について詳しく説明します。

az ad sp list を使用して、テナント内のサービス プリンシパルの一覧を取得できます。 既定では、このコマンドによって、テナントの最初の 100 個のサービス プリンシパルが返されます。 テナントのサービス プリンシパルをすべて取得するには、--all パラメーターを使用します。 この一覧の取得には時間がかかる場合があるため、次のパラメーターのいずれかを使用して一覧をフィルター処理することをお勧めします。

  • --display-name は、指定した名前に一致する "プレフィックス" を持つサービス プリンシパルを要求します。 サービス プリンシパルの表示名は、作成時に --name パラメーターで設定した値です。 サービス プリンシパルの作成時に --name を設定していない場合、名前のプレフィックスは azure-cli- になります。
  • --spn は、正確に一致するサービス プリンシパル名でフィルター処理します。 サービス プリンシパル名は常に https:// で始まります。 --name に使用した値が URI ではない場合、この値は https:// とこれに続く表示名となります。
  • --show-mine は、サインインしているユーザーによって作成されたサービス プリンシパルのみを要求します。
  • --filter は OData フィルターを使用して、"サーバー側" のフィルター処理を実行します。 この方法は、CLI の --query パラメーターを使用したクライアント側のフィルター処理よりも推奨されます。 OData フィルターの詳細については、フィルターのための OData 式の構文に関するページをご覧ください。

サービス プリンシパル オブジェクトについて返される情報は、詳細情報です。 サインインに必要な情報のみを取得するには、クエリ文字列 [].{id:appId, tenant:appOwnerOrganizationId} を使用します。 以下の例は、現在ログインしているユーザーによって作成されたすべてのサービス プリンシパルのサインイン情報を取得するものです。

az ad sp list --show-mine --query "[].{SPname:displayName, SPid:appId, tenant:appOwnerOrganizationId}" --output table

多数のサービス プリンシパルを持つ大規模な組織で作業している場合は、次のコマンドの例を試してください:

# get service principals containing a keyword
az ad sp list --display-name mySearchWord --output table

# get service principals using an OData filter
az ad sp list --filter "displayname eq 'myExactServicePrincipalName'" --output json

# get a service principal having a certain servicePrincipalNames property value
az ad sp list --spn https://spURL.com

重要

ユーザーとテナントの両方を az ad sp list az ad sp show で取得できますが、認証シークレット または 認証方法は使用できません。 Azure Key Vault 内の証明書のシークレットは az keyvault secret show を使用して取得できますが、既定では、それ以外のシークレットは保存されません。 認証方法やシークレットを忘れた場合は、サービス プリンシパルの資格情報をリセットします。

サービス プリンシパルのプロパティ

az ad sp list を使用してサービス プリンシパルの一覧を取得すると、スクリプトで参照できる出力プロパティが多数あります。

[
  {
    "accountEnabled": true,
    "addIns": [],
    "alternativeNames": [],
    "appDescription": null,
    "appDisplayName": "myServicePrincipalName",
    "appId": "00000000-0000-0000-0000-000000000000",
    "appOwnerOrganizationId": "00000000-0000-0000-0000-000000000000",
    "appRoleAssignmentRequired": false,
    "appRoles": [],
    "applicationTemplateId": null,
    "createdDateTime": null,
    "deletedDateTime": null,
    "description": null,
    "disabledByMicrosoftStatus": null,
    "displayName": "myServicePrincipalName",
    "homepage": "https://myURL.com",
    "id": "00000000-0000-0000-0000-000000000000",
    "info": {
      "logoUrl": null,
      "marketingUrl": null,
      "privacyStatementUrl": null,
      "supportUrl": null,
      "termsOfServiceUrl": null
    },
    "keyCredentials": [],
    "loginUrl": null,
    "logoutUrl": null,
    "notes": null,
    "notificationEmailAddresses": [],
    "oauth2PermissionScopes": [
      {
        "adminConsentDescription": "my admin description",
        "adminConsentDisplayName": "my admin display name",
        "id": "00000000-0000-0000-0000-000000000000",
        "isEnabled": true,
        "type": "User",
        "userConsentDescription": "my user description",
        "userConsentDisplayName": "my user display name",
        "value": "user_impersonation"
      }
    ],
    "passwordCredentials": [],
    "preferredSingleSignOnMode": null,
    "preferredTokenSigningKeyThumbprint": null,
    "replyUrls": [],
    "resourceSpecificApplicationPermissions": [],
    "samlSingleSignOnSettings": null,
    "servicePrincipalNames": [
      "00000000-0000-0000-0000-000000000000",
      "https://myURL.com"
    ],
    "servicePrincipalType": "Application",
    "signInAudience": null,
    "tags": [
      "WindowsAzureActiveDirectoryIntegratedApp"
    ],
    "tokenEncryptionKeyId": null,
    "verifiedPublisher": {
      "addedDateTime": null,
      "displayName": null,
      "verifiedPublisherId": null
    }
  }
]

--query パラメーターを使用して、サービス プリンシパルのプロパティを変数で取得し、格納します。

# Bash script
spID=$(az ad sp list --display-name myServicePrincipalName --query "[].{spID:appId}" --output tsv)
tenantID=$(az ad sp list --display-name myServicePrincipalName --query "[].{tenant:appOwnerOrganizationId}" --output tsv)
userConsentDescr=$(az ad sp list --display-name myServicePrincipalName --query "[].{ucs:oauth2PermissionScopes.userConsentDescription[0]}" --output tsv)
echo "Using appId $spID in tenant $tenantID for $userConsentDescr"

次のステップ

既存のサービス プリンシパルを取得する方法がわかったので、次のステップに進み、サービス プリンシパルのロールを管理する方法を理解してください。