快速入门:使用 PowerShell 在 Azure 中创建 Windows 虚拟机Quickstart: Create a Windows virtual machine in Azure with PowerShell

Azure PowerShell 模块用于从 PowerShell 命令行或脚本创建和管理 Azure 资源。The Azure PowerShell module is used to create and manage Azure resources from the PowerShell command line or in scripts. 本快速入门演示如何使用 Azure PowerShell 模块在 Azure 中部署运行 Windows Server 2016 的虚拟机 (VM)。This quickstart shows you how to use the Azure PowerShell module to deploy a virtual machine (VM) in Azure that runs Windows Server 2016. 若要显示运行中的 VM,也可通过 RDP 登录到该 VM 并安装 IIS Web 服务器。You will also RDP to the VM and install the IIS web server, to show the VM in action.

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

启动 Azure 本地 ShellLaunch Azure Local Shell

打开 Powershell 控制台,以管理员权限运行以下脚本。Open the Powershell console to run the following scripts with Administrator priviledge.

Note

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

创建资源组Create resource group

使用 New-AzResourceGroup 创建 Azure 资源组。Create an Azure resource group with New-AzResourceGroup. 资源组是在其中部署和管理 Azure 资源的逻辑容器。A resource group is a logical container into which Azure resources are deployed and managed.

New-AzResourceGroup -Name myResourceGroup -Location ChinaEast

创建虚拟机Create virtual machine

使用 New-AzVM 创建 VM。Create a VM with New-AzVM. 请提供每个资源的名称,如果这些资源不存在,New-AzVM cmdlet 会创建它们。Provide names for each of the resources and the New-AzVM cmdlet creates if they don't already exist.

出现提示时,请提供用作 VM 登录凭据的用户名和密码:When prompted, provide a username and password to be used as the sign-in credentials for the VM:

New-AzVm `
    -ResourceGroupName "myResourceGroup" `
    -Name "myVM" `
    -Location "China East" `
    -VirtualNetworkName "myVnet" `
    -SubnetName "mySubnet" `
    -SecurityGroupName "myNetworkSecurityGroup" `
    -PublicIpAddressName "myPublicIpAddress" `
    -OpenPorts 80,3389

连接到虚拟机Connect to virtual machine

在部署完成后,通过 RDP 登录到 VM。After the deployment has completed, RDP to the VM. 若要查看运行中的 VM,请安装 IIS Web 服务器。To see your VM in action, the IIS web server is then installed.

若要查看 VM 的公用 IP 地址,请使用 Get-AzPublicIpAddress cmdlet:To see the public IP address of the VM, use the Get-AzPublicIpAddress cmdlet:

Get-AzPublicIpAddress -ResourceGroupName "myResourceGroup" | Select "IpAddress"

使用以下命令从本地计算机创建远程桌面会话。Use the following command to create a remote desktop session from your local computer. 将 IP 地址替换为您的虚拟机的公用 IP 地址。Replace the IP address with the public IP address of your VM.

mstsc /v:publicIpAddress

在“Windows 安全性” 窗口中,依次选择“更多选择” 、“使用其他帐户” 。In the Windows Security window, select More choices, and then select Use a different account. localhost\username 的形式键入用户名,输入为虚拟机创建的密码,然后单击“确定”。 Type the username as localhost\username, enter password you created for the virtual machine, and then click OK.

你可能会在登录过程中收到证书警告。You may receive a certificate warning during the sign-in process. 单击“是”或“继续”以创建连接 Click Yes or Continue to create the connection

安装 Web 服务器Install web server

若要查看运行中的 VM,请安装 IIS Web 服务器。To see your VM in action, install the IIS web server. 在 VM 中打开 PowerShell 提示符并运行以下命令:Open a PowerShell prompt on the VM and run the following command:

Install-WindowsFeature -name Web-Server -IncludeManagementTools

完成后,关闭到 VM 的 RDP 连接。When done, close the RDP connection to the VM.

查看运行中的 Web 服务器View the web server in action

如果 IIS 已安装,并且 VM 上的端口 80 已对 Internet 开放, 则可以使用所选的 Web 浏览器查看默认的 IIS 欢迎页。With IIS installed and port 80 now open on your VM from the Internet, use a web browser of your choice to view the default IIS welcome page. 使用上一步中获取的 VM 的公用 IP 地址。Use the public IP address of your VM obtained in a previous step. 以下示例展示了默认 IIS 网站:The following example shows the default IIS web site:

IIS 默认站点

清理资源Clean up resources

不再需要时,可以使用 Remove-AzResourceGroup cmdlet 删除资源组、VM 和所有相关资源:When no longer needed, you can use the Remove-AzResourceGroup cmdlet to remove the resource group, VM, and all related resources:

Remove-AzResourceGroup -Name myResourceGroup

后续步骤Next steps

在本快速入门中,你部署了简单的虚拟机,打开了 Web 流量的网络端口,并安装了一个基本 Web 服务器。In this quickstart, you deployed a simple virtual machine, open a network port for web traffic, and installed a basic web server. 若要详细了解 Azure 虚拟机,请继续学习 Windows VM 的教程。To learn more about Azure virtual machines, continue to the tutorial for Windows VMs.