使用 Docker 运行适用于 Azure Stack Hub 的 PowerShellUse Docker to run PowerShell for Azure Stack Hub

在本文中,你可使用 Docker 创建容器,在其中运行使用各种接口所需的 PowerShell 版本。In this article, you can use Docker to create a container on which to run the version of PowerShell that's required for working with the various interfaces. 你可以找到使用 AzureRM 模块和最新 Az 模块的说明。You can find instructions for using both AzureRM modules and the latest Az modules. AzureRM 要求使用基于 Windows 的容器。AzureRM requires a Windows-based container. Az 使用基于 Linux 的容器。Az uses a Linux-based container.

Docker 先决条件Docker prerequisites

安装 DockerInstall Docker

  1. 安装 DockerInstall Docker.

  2. 在命令行程序(例如 PowerShell 或 Bash)中输入以下内容:In a command-line program, such as PowerShell or Bash, enter:

    docker --version
    

使用 PowerShell 设置服务主体Set up a service principal for using PowerShell

若要使用 PowerShell 访问 Azure Stack Hub 中的资源,需要在 Azure Active Directory (Azure AD) 租户中有一个服务主体。To use PowerShell to access resources in Azure Stack Hub, you need a service principal in your Azure Active Directory (Azure AD) tenant. 通过基于角色的访问控制 (RBAC) 来委派权限。You delegate permissions with user role-based access control (RBAC). 可能需要向云运营商请求服务主体。You may need to request the service principal from your cloud operator.

  1. 若要设置服务主体,请按通过创建服务主体向应用程序授予对 Azure Stack Hub 资源的访问权限中的说明操作。To set up your service principal, follow the instructions in Give applications access to Azure Stack Hub resources by creating service principals.

  2. 记下应用程序 ID、机密、租户 ID 和对象 ID 供以后使用。Note the application ID, the secret, your tenant ID, and object ID for later use.

在 Docker 中运行 PowerShellRun PowerShell in Docker

在这些说明中,你将运行基于 Windows 的容器映像,并安装 PowerShell 和 Azure Stack Hub 所需的模块。In these instructions, you will run a Windows-based container image and install the PowerShell and the required modules for Azure Stack Hub.

  1. 需要使用需要 Windows 10 的 Windows 容器来运行 Docker。You need to run Docker by using Windows containers that require Windows 10. 在运行 Docker 时,请切换到 Windows 容器。When you run Docker, switch to Windows containers. 支持 Az 模块的映像将需要 Docker 17.05 或更高版本。The images supporting the Az module will require Docker 17.05 or newer.

  2. 从已加入 Azure Stack Hub 所在的域的计算机运行 Docker。Run Docker from a machine that's joined to the same domain as Azure Stack Hub. 如果使用 Azure Stack 开发工具包 (ASDK),需在远程计算机上安装 VPNIf you are using the Azure Stack Development Kit (ASDK), you need to install the VPN on your remote machine.

在 Windows 容器上安装 Azure Stack Hub AzureRM 模块Install Azure Stack Hub AzureRM module on a windows container

Dockerfile 打开 Microsoft 映像 microsoft/windowsservercore,其中已安装 Windows PowerShell 5.1。The Dockerfile opens the Microsoft image microsoft/windowsservercore, which has Windows PowerShell 5.1 installed. 该文件然后会加载 NuGet 和 Azure Stack Hub PowerShell 模块,并从 Azure Stack Hub Tools 下载工具。The file then loads NuGet and the Azure Stack Hub PowerShell modules, and downloads the tools from Azure Stack Hub Tools.

  1. 以 ZIP 文件形式下载 azure-stack-powershell 存储库,或者克隆该存储库。Download the azure-stack-powershell repository as a ZIP file, or clone the repository.

  2. 从终端打开存储库文件夹。Open the repository folder from your terminal.

  3. 在存储库中打开命令行界面,然后输入以下命令:Open a command-line interface in your repository, and then enter the following command:

    docker build --tag azure-stack-powershell .
    
  4. 生成映像以后,请输入以下内容,以便启动交互式容器:When the image has been built, start an interactive container by entering:

        docker run -it azure-stack-powershell powershell
    
  5. 可以将此 shell 用于 cmdlet 了。The shell is ready for your cmdlets.

    Windows PowerShell
    Copyright (C) 2016 Microsoft Corporation. All rights reserved.
    
    PS C:\>
    
  6. 使用服务主体连接到 Azure Stack Hub 实例。Connect to your Azure Stack Hub instance by using the service principal. 现在使用 Docker 中的 PowerShell 提示符。You are now using a PowerShell prompt in Docker.

    $passwd = ConvertTo-SecureString <Secret> -AsPlainText -Force
    $pscredential = New-Object System.Management.Automation.PSCredential('<ApplicationID>', $passwd)
    Connect-AzureRmAccount -ServicePrincipal -Credential $pscredential -TenantId <TenantID>
    

    PowerShell 返回帐户对象:PowerShell returns your account object:

    Account    SubscriptionName    TenantId    Environment
    -------    ----------------    --------    -----------
    <AccountID>    <SubName>       <TenantID>  AzureChinaCloud
    
  7. 通过在 Azure Stack Hub 中创建资源组,测试连接性。Test your connectivity by creating a resource group in Azure Stack Hub.

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

后续步骤Next steps