使用 PowerShell 连接到 Azure Stack HubConnect to Azure Stack Hub with PowerShell

可以将 Azure Stack Hub 配置为使用 PowerShell 来管理资源,例如创建套餐、计划、配额以及警报。You can configure Azure Stack Hub to use PowerShell to manage resources like creating offers, plans, quotas, and alerts. 本主题有助于配置操作员环境This topic helps you configure the operator environment.

先决条件Prerequisites

如果已通过 VPN 连接到 ASDK,请通过 Azure Stack 开发工具包 (ASDK) 或基于 Windows 的外部客户端运行以下先决条件操作。Run the following prerequisites either from the Azure Stack Development Kit (ASDK) or from a Windows-based external client if you're connected to the ASDK through VPN.

与 Azure AD 连接Connect with Azure AD

若要使用 PowerShell 配置 Azure Stack Hub 操作员环境,请运行以下脚本之一。To configure the Azure Stack Hub operator environment with PowerShell, run one of the scripts below. 将 Azure Active Directory (Azure AD) tenantName 和 Azure 资源管理器终结点值替换为你自己的环境配置。Replace the Azure Active Directory (Azure AD) tenantName and Azure Resource Manager endpoint values with your own environment configuration.

备注

如果会话过期,密码已更改,或者只是希望切换帐户,请在使用 Add-AzureRmAccount 登录之前运行以下 cmdlet:Remove-AzureRmAccount -Scope ProcessIf your session expires, your password has changed, or you simply wish to switch accounts, run the following cmdlet before you sign in using Add-AzureRmAccount: Remove-AzureRmAccount -Scope Process

    # Register an Azure Resource Manager environment that targets your Azure Stack Hub instance. Get your Azure Resource Manager endpoint value from your service provider.
    Add-AzureRMEnvironment -Name "AzureStackAdmin" -ArmEndpoint "https://adminmanagement.local.azurestack.external" `
      -AzureKeyVaultDnsSuffix adminvault.local.azurestack.external `
      -AzureKeyVaultServiceEndpointResourceId https://adminvault.local.azurestack.external

    # Set your tenant name.
    $AuthEndpoint = (Get-AzureRmEnvironment -Name "AzureStackAdmin").ActiveDirectoryAuthority.TrimEnd('/')
    $AADTenantName = "<myDirectoryTenantName>.partner.onmschina.cn"
    $TenantId = (invoke-restmethod "$($AuthEndpoint)/$($AADTenantName)/.well-known/openid-configuration").issuer.TrimEnd('/').Split('/')[-1]

    # After signing in to your environment, Azure Stack Hub cmdlets
    # can be easily targeted at your Azure Stack Hub instance.
    Add-AzureRmAccount -EnvironmentName "AzureStackAdmin" -TenantId $TenantId

与 AD FS 连接Connect with AD FS

使用 PowerShell 通过 Active Directory 联合身份验证服务 (Azure AD FS) 连接到 Azure Stack Hub 操作员环境。Connect to the Azure Stack Hub operator environment with PowerShell with Azure Active Directory Federated Services (Azure AD FS). 对于 ASDK,此 Azure 资源管理器终结点设置为 https://adminmanagement.local.azurestack.externalFor the ASDK, this Azure Resource Manager endpoint is set to https://adminmanagement.local.azurestack.external. 若要获取 Azure Stack Hub 集成系统的 Azure 资源管理器终结点,请与服务提供商联系。To get the Azure Resource Manager endpoint for Azure Stack Hub integrated systems, contact your service provider.

# Register an Azure Resource Manager environment that targets your Azure Stack Hub instance. Get your Azure Resource Manager endpoint value from your service provider.
  Add-AzureRMEnvironment -Name "AzureStackAdmin" -ArmEndpoint "https://adminmanagement.local.azurestack.external" `
    -AzureKeyVaultDnsSuffix adminvault.local.azurestack.external `
    -AzureKeyVaultServiceEndpointResourceId https://adminvault.local.azurestack.external

# Sign in to your environment.
Login-AzureRmAccount -EnvironmentName "AzureStackAdmin"

备注

AD FS 仅支持通过用户标识进行交互式身份验证。AD FS only supports interactive authentication with user identities. 如果需要凭据对象,则必须使用服务主体 (SPN)。If a credential object is required, you must use a service principal (SPN). 若要详细了解如何在设置服务主体时将 Azure Stack Hub 和 AD FS 作为标识管理服务,请参阅管理 AD FS 应用标识For more information on setting up a service principal with Azure Stack Hub and AD FS as your identity management service, see Manage an AD FS app identity.

测试连接Test the connectivity

完成所有设置后,请使用 PowerShell 在 Azure Stack Hub 中创建资源。Now that you've got everything set-up, use PowerShell to create resources within Azure Stack Hub. 例如,可以为应用创建资源组并添加虚拟机。For example, you can create a resource group for an app and add a virtual machine. 使用以下命令创建名为“MyResourceGroup”的资源组。Use the following command to create a resource group named MyResourceGroup.

New-AzureRmResourceGroup -Name "MyResourceGroup" -Location "Local"

后续步骤Next steps