共用方式為


以互動方式登入 Azure PowerShell

對 Azure 的互動式登入提供更直覺且更有彈性的用戶體驗。 使用 Azure PowerShell 的互動式登入可讓使用者透過 PowerShell 介面直接向 Azure 進行驗證,這適用於臨機操作管理工作,以及需要手動登入的環境,例如具有多重要素驗證 (MFA) 的環境。 此方法可簡化腳本測試、學習和即時管理的存取,而不需要預先設定服務主體或其他非互動式驗證方法。

必要條件

互動式登入

若要以互動方式登入,請使用 Connect-AzAccount Cmdlet。 從 Az PowerShell 模組 12.0.0 版開始,Windows 系統預設會使用 Web 帳戶管理員(WAM),而 Linux 和 macOS 系統預設會使用瀏覽器型登入。

Connect-AzAccount

登入體驗

從 Az PowerShell 模組 12.0.0 版開始,如果您有多個訂用帳戶的存取權,系統會提示您選取要登入的 Azure 訂用帳戶,如下列範例所示。

Please select the account you want to login with.

Retrieving subscriptions for the selection...
WARNING: To override which subscription Connect-AzAccount selects by default, use
`Update-AzConfig -DefaultSubscriptionForLogin 00000000-0000-0000-0000-000000000000`.
Go to https://go.microsoft.com/fwlink/?linkid=2200610 for more information.
[Tenant and subscription selection]

No    Subscription name                     Subscription ID                           Tenant name
----  ------------------------------------  ----------------------------------------  --------------
[1]   Facility Services Subscription        00000000-0000-0000-0000-000000000000      Contoso
[2]   Finance Department Subscription       00000000-0000-0000-0000-000000000000      Contoso
[3]   Human Resources Subscription          00000000-0000-0000-0000-000000000000      Contoso
[4]   Information Technology Subscription   00000000-0000-0000-0000-000000000000      Contoso

Select a tenant and subscription: 2

Subscription name                       Tenant name
------------------------------------    --------------------------
Finance Department Subscription         Contoso

[Announcements]
With the new Azure PowerShell login experience, you can select the subscription you want to use more easily.
Learn more about it and its configuration at https://go.microsoft.com/fwlink/?linkid=2271236.
Share your feedback regarding your experience with `Connect-AzAccount` at: https://aka.ms/azloginfeedback

If you encounter any problem, please open an issue at: https://aka.ms/azpsissue

Subscription name                      Tenant
-----------------                      ------
Finance Department Subscription        Contoso

下次登入時,先前選取的租用戶和訂用帳戶會以星號 (*) 標示為預設值,其編號旁邊會以青藍色醒目提示。 這可讓您按 Enter 鍵來選取預設值,或輸入數位來選取不同的租用戶和訂用帳戶。

No    Subscription name                     Subscription ID                           Tenant name
----  ------------------------------------  ----------------------------------------  --------------
[1]   Facility Services Subscription        00000000-0000-0000-0000-000000000000      Contoso
[2] * Finance Department Subscription       00000000-0000-0000-0000-000000000000      Contoso
[3]   Human Resources Subscription          00000000-0000-0000-0000-000000000000      Contoso
[4]   Information Technology Subscription   00000000-0000-0000-0000-000000000000      Contoso

The default is marked with an *; the default tenant is 'Contoso' and subscription is
'Finance Department Subscription (00000000-0000-0000-0000-000000000000)'.

Select a tenant and subscription (type a number or Enter to accept default): 4

Subscription name                       Tenant name
------------------------------------    --------------------------
Information Technology Subscription     Contoso

根據預設,命令會針對此訂用帳戶執行。 若要變更使用中的 Set-AzContext 訂用帳戶,請使用 Cmdlet。 如需詳細資訊,請參閱 Azure PowerShell 內容物件

設定登入的預設訂用帳戶

若要避免在每次以互動方式登入時選取訂用帳戶,請使用 Update-AzConfig Cmdlet 來設定您的預設訂用帳戶,如下列範例所示。

Update-AzConfig -DefaultSubscriptionForLogin '<subscription name or id>'

停用新的登入體驗

若要停用新的登入體驗,請使用 Update-AzConfig Cmdlet,如下列範例所示。

Update-AzConfig -LoginExperienceV2 Off

當新的登入體驗停用且您可以存取多個訂用帳戶時,您就會登入第一個 Azure 訂用帳戶。 根據預設,命令會針對此訂用帳戶執行。 若要變更會話的使用中訂用帳戶,請使用 Set-AzContext Cmdlet。 若要變更作用中的訂用帳戶,並在相同系統上的會話之間保存,請使用 Select-AzContext Cmdlet。

Web 帳戶管理員 (WAM)

從 Az PowerShell 模組 12.0.0 版開始,以 Windows 為基礎的系統,Azure PowerShell 的預設登入驗證方法是 Web 帳戶管理員(WAM)。

WAM 是作為驗證代理人的 Windows 10+ 元件。 驗證代理人是在系統上執行的應用程式,可管理連線帳戶的驗證交握和令牌維護。

WAM 的優點

使用 WAM 提供數個優點:

WAM 的限制

在目前開發階段,WAM 有一些已知的限制:

  • WAM 適用於 Windows 10 和更新版本,以及 Windows Server 2019 和更新版本。 在Linux、macOS和舊版 Windows 上,Azure PowerShell 會自動預設為瀏覽器型登入。

  • 目前不支援使用 WAM 登入國家雲端。

  • Microsoft帳戶(例如, @outlook.com 或 @live.com) 必須在搭配 MFA 使用時指定 Tenant 參數。

    Connect-AzAccount -Tenant 00000000-0000-0000-0000-000000000000
    

停用 WAM

若要在 Windows 10 和更新版本或 Windows Server 2019 和更新版本上使用瀏覽器型登入,請使用 Az 12.0.0 和更新版本,您必須停用 WAM 以搭配 Azure PowerShell 使用。 使用下列命令來停用WAM並返回瀏覽器型登入,這是 Az 12.0.0 之前的預設值。

Update-AzConfig -EnableLoginByWam $false

瀏覽器型登入

瀏覽器型登入是 Windows 10 或 Windows Server 2019 之前的 Linux、macOS 和 Windows 系統的預設互動式登入。 從 Az PowerShell 模組 12.0.0 版開始,您必須 停用 Azure PowerShell 的 WAM ,才能在以 Windows 為基礎的系統上使用瀏覽器型登入,這是 Az 12.0.0 之前的預設值。

當您以互動方式使用 Connect-AzAccount Cmdlet 登入時,瀏覽器型登入會開啟預設網頁瀏覽器以載入 Azure 登入頁面。 在瀏覽器中使用您的 Azure 帳戶認證登入。

如果 Azure PowerShell 可以開啟您的預設瀏覽器,它會起始 授權碼流程 ,並開啟預設瀏覽器以載入 Azure 登入頁面。 否則,它會起始裝置程式代碼流程,以指示您在 microsoft.com/devicelogin 開啟瀏覽器頁面,並輸入 PowerShell 會話中顯示的程式代碼。

裝置程式碼驗證

如果 Web 帳戶管理員或網頁瀏覽器無法使用或無法開啟,您可以藉由指定 UseDeviceAuthentication 參數來強制裝置程式代碼流程。

Connect-AzAccount -UseDeviceAuthentication

使用不同的租用戶登入

如果您的帳戶與多個租使用者相關聯,登入需要在 連線時指定 Tenant 參數。 此參數適用於任何登入方法。 登入時,此參數值可以是租使用者的 Azure 對象識別碼(租使用者標識元)或租使用者的完整功能變數名稱。

Connect-AzAccount -Tenant 00000000-0000-0000-0000-000000000000

登入國家雲端

國家雲端(也稱為主權雲端)是 Azure 的實體隔離實例,其設計目的是確保數據落地、主權和合規性需求在地理界限內受到遵守。 針對國家雲端中的帳戶,當您使用 Environment 參數登入時,請設定環境。 此參數適用於任何登入方法。 例如,如果您的帳戶位於 Azure China 21Vianet 中,請使用下列命令:

Connect-AzAccount -Environment AzureChinaCloud

您可以執行下列命令來取得可用的國家雲端環境清單:

Get-AzEnvironment | Select-Object -Property Name

另請參閱