快速入門:使用 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. 您也會以 RDP 連線至 VM 並安裝 IIS 網頁伺服器,以顯示作用中的 VM。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 free account before you begin.

啟動 Azure Cloud ShellLaunch Azure Cloud Shell

Azure Cloud Shell 是免費的互動式 Shell,可讓您用來執行本文中的步驟。The Azure Cloud Shell is a free interactive shell that you can use to run the steps in this article. 它具有預先安裝和設定的共用 Azure 工具,可與您的帳戶搭配使用。It has common Azure tools preinstalled and configured to use with your account.

若要開啟 Cloud Shell,只要選取程式碼區塊右上角的 [試試看] 即可。To open the Cloud Shell, just select Try it from the upper right corner of a code block. 您也可以移至 https://shell.azure.com/powershell,從另一個瀏覽器索引標籤啟動 Cloud Shell。You can also launch Cloud Shell in a separate browser tab by going to https://shell.azure.com/powershell. 選取 [複製] 即可複製程式碼區塊,將它貼到 Cloud Shell 中,然後按 enter 鍵加以執行。Select Copy to copy the blocks of code, paste it into the Cloud Shell, and press enter to run it.

建立資源群組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 EastUS

建立虛擬機器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 "East US" `
    -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 網頁伺服器。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. 以 VM 的公用 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 網頁伺服器。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.

檢視作用中的網頁伺服器View the web server in action

安裝 IIS 後,現在經由網際網路在您的 VM 上開啟連接埠 80,請使用所選的網頁瀏覽器來檢視預設 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 命令來移除資源群組、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

在此快速入門中,您已部署簡單的虛擬機器、對網路流量開啟網路連接埠,以及安裝基本的網頁伺服器。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.