Connect-AzureAD

使用已驗證的帳戶連線,以使用 Active Directory Cmdlet 要求。

Syntax

Connect-AzureAD
       [-AzureEnvironmentName <EnvironmentName>]
       [-TenantId <String>]
       [-Credential <PSCredential>]
       [-AccountId <String>]
       [-LogLevel <LogLevel>]
       [-LogFilePath <String>]
       [-InformationAction <ActionPreference>]
       [-InformationVariable <String>]
       [-WhatIf]
       [-Confirm]
       [<CommonParameters>]
Connect-AzureAD
       [-AzureEnvironmentName <EnvironmentName>]
       -TenantId <String>
       -CertificateThumbprint <String>
       -ApplicationId <String>
       [-LogLevel <LogLevel>]
       [-LogFilePath <String>]
       [-InformationAction <ActionPreference>]
       [-InformationVariable <String>]
       [-WhatIf]
       [-Confirm]
       [<CommonParameters>]
Connect-AzureAD
       [-AzureEnvironmentName <EnvironmentName>]
       [-TenantId <String>]
       -AadAccessToken <String>
       [-MsAccessToken <String>]
       -AccountId <String>
       [-LogLevel <LogLevel>]
       [-LogFilePath <String>]
       [-InformationAction <ActionPreference>]
       [-InformationVariable <String>]
       [-WhatIf]
       [-Confirm]
       [<CommonParameters>]

Description

Connect-AzureAD Cmdlet 會連線已驗證的帳戶,以用於 Azure Active Directory Cmdlet 要求。

您只能搭配 Azure Active Directory Cmdlet 使用此已驗證帳戶。

範例

範例 1:將 PowerShell 會話連線至租使用者

PS C:\> Connect-AzureAD -Confirm

此命令會將目前的 PowerShell 會話連線至 Azure Active Directory 租使用者。 命令會提示您輸入您要連線之租使用者的使用者名稱和密碼。 Confirm參數會提示您確認。

如果您的認證已啟用多重要素驗證,您必須使用互動式選項登入,或使用服務主體驗證。

範例 2:使用變數連接會話

PS C:\> $Credential = Get-Credential
PS C:\> Connect-AzureAD -Credential $Credential

第一個命令會取得使用者認證,然後將它們儲存在$Credential變數中。

第二個命令會使用 $Credential 中的認證來連接目前的 PowerShell 會話。

此帳戶會使用組織識別碼認證向 Azure Active Directory 進行驗證。 您無法使用此帳戶使用多重要素驗證或 Microsoft 帳號憑證來執行 Azure Active Directory Cmdlet。

範例 3:將會話連線為服務主體

# Login to Azure AD PowerShell With Admin Account
Connect-AzureAD 

# Create the self signed cert
$currentDate = Get-Date
$endDate = $currentDate.AddYears(1)
$notAfter = $endDate.AddYears(1)
$pwd = "<password>"
$thumb = (New-SelfSignedCertificate -CertStoreLocation cert:\localmachine\my -DnsName com.foo.bar -KeyExportPolicy Exportable -Provider "Microsoft Enhanced RSA and AES Cryptographic Provider" -NotAfter $notAfter).Thumbprint
$pwd = ConvertTo-SecureString -String $pwd -Force -AsPlainText
Export-PfxCertificate -cert "cert:\localmachine\my\$thumb" -FilePath c:\temp\examplecert.pfx -Password $pwd

# Load the certificate
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate("C:\temp\examplecert.pfx", $pwd)
$keyValue = [System.Convert]::ToBase64String($cert.GetRawCertData())


# Create the Azure Active Directory Application
$application = New-AzureADApplication -DisplayName "test123" -IdentifierUris "https://test123"
New-AzureADApplicationKeyCredential -ObjectId $application.ObjectId -CustomKeyIdentifier "Test123" -StartDate $currentDate -EndDate $endDate -Type AsymmetricX509Cert -Usage Verify -Value $keyValue

# Create the Service Principal and connect it to the Application
$sp=New-AzureADServicePrincipal -AppId $application.AppId

# Give the Service Principal Reader access to the current tenant (Get-AzureADDirectoryRole)
Add-AzureADDirectoryRoleMember -ObjectId 5997d714-c3b5-4d5b-9973-ec2f38fd49d5 -RefObjectId $sp.ObjectId

# Get Tenant Detail
$tenant=Get-AzureADTenantDetail
# Now you can login to Azure PowerShell with your Service Principal and Certificate
Connect-AzureAD -TenantId $tenant.ObjectId -ApplicationId  $sp.AppId -CertificateThumbprint $thumb

此命令會將使用者驗證為 Azure Active Directory 作為服務主體。

參數

-AadAccessToken

指定 Azure Active Directory Graph 存取權杖。

Type:String
Position:Named
Default value:None
Required:True
Accept pipeline input:False
Accept wildcard characters:False

-AccountId

指定帳戶的識別碼。 使用使用者存取權杖進行驗證時,您必須指定使用者的 UPN。

Type:String
Position:Named
Default value:None
Required:True
Accept pipeline input:False
Accept wildcard characters:False

-ApplicationId

指定服務主體的應用程式識別碼。

Type:String
Position:Named
Default value:None
Required:True
Accept pipeline input:False
Accept wildcard characters:False

-AzureEnvironmentName

指定 Azure 環境的名稱。 此參數可接受的值為:

  • AzureCloud
  • AzureChinaCloud
  • AzureUSGovernment
  • AzureGermanyCloud

預設值為 AzureCloud。

Type:EnvironmentName
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-CertificateThumbprint

指定具有執行此動作許可權之使用者帳戶之數位公開金鑰 X.509 憑證的憑證指紋。

Type:String
Position:Named
Default value:None
Required:True
Accept pipeline input:False
Accept wildcard characters:False

-Confirm

在執行 Cmdlet 前提示您確認。

Type:SwitchParameter
Aliases:cf
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-Credential

指定 PSCredential 物件。 如需 PSCredential 物件的詳細資訊,請輸入 Get-Help Get-Credential。

PSCredential物件會提供組織識別碼認證的使用者識別碼和密碼。

Type:PSCredential
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-InformationAction

指定此 Cmdlet 如何回應資訊事件。 此參數可接受的值為:

  • 繼續
  • 忽略
  • 詢問
  • SilentlyContinue
  • Stop
  • 暫止
Type:ActionPreference
Aliases:infa
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-InformationVariable

指定要在其中儲存資訊事件訊息的變數。

Type:String
Aliases:iv
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-LogFilePath

寫入此 PowerShell 會話之記錄檔的路徑。 如果您需要偏離預設 PowerShell 記錄檔位置,請在這裡提供值。

Type:String
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-LogLevel

指定記錄層級。 此參數可接受的值為:

  • Info
  • [錯誤]
  • 警告
  • None

預設值為資訊。

Type:LogLevel
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-MsAccessToken

指定 Microsoft Graph 存取權杖。

Type:String
Position:Named
Default value:None
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-TenantId

指定租使用者的識別碼。

如果您未指定此參數,帳戶會向主租使用者進行驗證。

您必須指定 TenantId 參數,以作為服務主體進行驗證,或使用 Microsoft 帳戶時進行驗證。

Type:String
Aliases:Domain, TenantDomain
Position:Named
Default value:None
Required:True
Accept pipeline input:False
Accept wildcard characters:False

-WhatIf

顯示執行 Cmdlet 後會發生的情況。 Cmdlet 並不會執行。

Type:SwitchParameter
Aliases:wi
Position:Named
Default value:False
Required:False
Accept pipeline input:False
Accept wildcard characters:False