快速入門:在 Azure Stack Hub 中使用 PowerShell 建立 Windows Server VMQuickstart: Create a Windows Server VM by using PowerShell in Azure Stack Hub

您可以使用 Azure Stack Hub PowerShell 建立 Windows Server 2016 虛擬機器 (VM)。You can create a Windows Server 2016 virtual machine (VM) by using Azure Stack Hub PowerShell. 請依照本文中的步驟建立和使用 VM。Follow the steps in this article to create and use a VM. 本文也提供下列操作的步驟:This article also gives you the steps to:

  • 使用遠端用戶端連線至 VM。Connect to the VM with a remote client.
  • 安裝 IIS 網頁伺服器,並且檢視預設首頁。Install the IIS web server and view the default home page.
  • 清除資源。Clean up your resources.

注意

您可以從 Azure Stack 開發套件,或從 Windows 型外部用戶端 (如果您透過 VPN 連線) 來執行這篇文章中所述的步驟。You can run the steps described in this article from the Azure Stack Development Kit, or from a Windows-based external client if you are connected over a VPN.

Windows Server VM 的必要條件Prerequisites for Windows Server VM

  • 請確定您的 Azure Stack Hub 操作員已將 Windows Server 2016 映像新增到 Azure Stack Hub Marketplace。Make sure that your Azure Stack Hub operator has added the Windows Server 2016 image to the Azure Stack Hub Marketplace.

  • Azure Stack Hub 需要特定版本的 Azure PowerShell,才能建立和管理資源。Azure Stack Hub requires a specific version of Azure PowerShell to create and manage the resources. 如果您尚未針對 Azure Stack Hub 設定 PowerShell,請依照步驟來安裝 PowerShell。If you don't have PowerShell configured for Azure Stack Hub, follow the steps to install PowerShell.

  • 設定好 Azure Stack Hub PowerShell 後,您必須連線到 Azure Stack Hub 環境。With the Azure Stack Hub PowerShell set up, you'll need to connect to your Azure Stack Hub environment. 如需指示,請參閱以使用者的身分使用 PowerShell 連線到 Azure Stack HubFor instruction, see Connect to Azure Stack Hub with PowerShell as a user.

建立資源群組Create a resource group

資源群組是在其中部署與管理 Azure Stack Hub 資源的邏輯容器。A resource group is a logical container into which Azure Stack Hub resources are deployed and managed. 從您的開發套件或 Azure Stack Hub 整合系統,執行下列程式碼區塊來建立資源群組。From your development kit or the Azure Stack Hub integrated system, run the following code block to create a resource group.

注意

系統會為程式碼範例中的所有變數指派值。Values are assigned for all variables in the code examples. 不過,您可以視需要指派新值。However, you can assign new values if you want to.

# Create variables to store the location and resource group names.
$location = "local"
$ResourceGroupName = "myResourceGroup"

New-AzResourceGroup `
  -Name $ResourceGroupName `
  -Location $location

建立儲存體資源Create storage resources

建立儲存體帳戶來儲存開機診斷的輸出。Create a storage account to store the output of boot diagnostics.

# Create variables to store the storage account name and the storage account SKU information
$StorageAccountName = "mystorageaccount"
$SkuName = "Standard_LRS"

# Create a new storage account
$StorageAccount = New-AzStorageAccount `
  -Location $location `
  -ResourceGroupName $ResourceGroupName `
  -Type $SkuName `
  -Name $StorageAccountName

Set-AzCurrentStorageAccount `
  -StorageAccountName $storageAccountName `
  -ResourceGroupName $resourceGroupName

建立網路資源Create networking resources

建立虛擬網路、子網路和公用 IP 位址。Create a virtual network, subnet, and a public IP address. 這些資源可用來提供 VM 的網路連線能力。These resources are used to provide network connectivity to the VM.

# Create a subnet configuration
$subnetConfig = New-AzVirtualNetworkSubnetConfig `
  -Name mySubnet `
  -AddressPrefix 192.168.1.0/24

# Create a virtual network
$vnet = New-AzVirtualNetwork `
  -ResourceGroupName $ResourceGroupName `
  -Location $location `
  -Name MyVnet `
  -AddressPrefix 192.168.0.0/16 `
  -Subnet $subnetConfig

# Create a public IP address and specify a DNS name
$pip = New-AzPublicIpAddress `
  -ResourceGroupName $ResourceGroupName `
  -Location $location `
  -AllocationMethod Static `
  -IdleTimeoutInMinutes 4 `
  -Name "mypublicdns$(Get-Random)"

建立網路安全性群組和網路安全性群組規則Create a network security group and a network security group rule

網路安全性群組可使用輸入和輸出規則來保護 VM。The network security group secures the VM by using inbound and outbound rules. 讓我們建立連接埠 3389 的輸入規則以允許傳入的遠端桌面連線,並建立連接埠 80 的輸入規則以允許傳入的 Web 流量。Let's create an inbound rule for port 3389 to allow incoming Remote Desktop connections and an inbound rule for port 80 to allow incoming web traffic.

# Create an inbound network security group rule for port 3389
$nsgRuleRDP = New-AzNetworkSecurityRuleConfig `
  -Name myNetworkSecurityGroupRuleRDP `
  -Protocol Tcp `
  -Direction Inbound `
  -Priority 1000 `
  -SourceAddressPrefix * `
  -SourcePortRange * `
  -DestinationAddressPrefix * `
  -DestinationPortRange 3389 `
  -Access Allow

# Create an inbound network security group rule for port 80
$nsgRuleWeb = New-AzNetworkSecurityRuleConfig `
  -Name myNetworkSecurityGroupRuleWWW `
  -Protocol Tcp `
  -Direction Inbound `
  -Priority 1001 `
  -SourceAddressPrefix * `
  -SourcePortRange * `
  -DestinationAddressPrefix * `
  -DestinationPortRange 80 `
  -Access Allow

# Create a network security group
$nsg = New-AzNetworkSecurityGroup `
  -ResourceGroupName $ResourceGroupName `
  -Location $location `
  -Name myNetworkSecurityGroup `
  -SecurityRules $nsgRuleRDP,$nsgRuleWeb

建立 VM 的網路卡Create a network card for the VM

網路卡會將 VM 連線至子網路、網路安全性群組和公用 IP 位址。The network card connects the VM to a subnet, network security group, and public IP address.

# Create a virtual network card and associate it with public IP address and NSG
$nic = New-AzNetworkInterface `
  -Name myNic `
  -ResourceGroupName $ResourceGroupName `
  -Location $location `
  -SubnetId $vnet.Subnets[0].Id `
  -PublicIpAddressId $pip.Id `
  -NetworkSecurityGroupId $nsg.Id

建立 VMCreate a VM

建立 VM 組態。Create a VM configuration. 此組態包含部署 VM 時使用的設定。This configuration includes the settings used when deploying the VM. 例如:認證、大小和 VM 映像。For example: credentials, size, and the VM image.

# Define a credential object to store the username and password for the VM
$UserName='demouser'
$Password='Password@123'| ConvertTo-SecureString -Force -AsPlainText
$Credential=New-Object PSCredential($UserName,$Password)

# Create the VM configuration object
$VmName = "VirtualMachinelatest"
$VmSize = "Standard_A1"
$VirtualMachine = New-AzVMConfig `
  -VMName $VmName `
  -VMSize $VmSize

$VirtualMachine = Set-AzVMOperatingSystem `
  -VM $VirtualMachine `
  -Windows `
  -ComputerName "MainComputer" `
  -Credential $Credential -ProvisionVMAgent

$VirtualMachine = Set-AzVMSourceImage `
  -VM $VirtualMachine `
  -PublisherName "MicrosoftWindowsServer" `
  -Offer "WindowsServer" `
  -Skus "2016-Datacenter" `
  -Version "latest"

# Sets the operating system disk properties on a VM.
$VirtualMachine = Set-AzVMOSDisk `
  -VM $VirtualMachine `
  -CreateOption FromImage | `
  Set-AzVMBootDiagnostics -ResourceGroupName $ResourceGroupName `
  -StorageAccountName $StorageAccountName -Enable |`
  Add-AzVMNetworkInterface -Id $nic.Id


# Create the VM.
New-AzVM `
  -ResourceGroupName $ResourceGroupName `
  -Location $location `
  -VM $VirtualMachine

連接至 VMConnect to the VM

若要從遠端存取您在上一個步驟中建立的 VM,則需要其公用 IP 位址。To remote into the VM that you created in the previous step, you need its public IP address. 執行下列命令,以取得 VM 的公用 IP 位址:Run the following command to get the public IP address of the VM:

Get-AzPublicIpAddress `
  -ResourceGroupName $ResourceGroupName | Select IpAddress

使用下列命令建立 VM 的遠端桌面工作階段。Use the following command to create a Remote Desktop session with the VM. 請將 IP 位址取代為 VM 的 publicIPAddress 。Replace the IP address with the publicIPAddress of your VM. 出現提示時,請輸入您在建立 VM 時所使用的使用者名稱和密碼。When prompted, enter the username and password used when creating the VM.

mstsc /v <publicIpAddress>

透過 PowerShell 安裝 IISInstall IIS via PowerShell

您現已登入 Azure VM,可使用一行 PowerShell 來安裝 IIS,並啟用本機防火牆規則以允許 Web 流量通過。Now that you have signed in to the Azure VM, you can use a single line of PowerShell to install IIS and enable the local firewall rule to allow web traffic. 開啟 PowerShell 提示字元並執行下列命令:Open a PowerShell prompt and run the following command:

Install-WindowsFeature -name Web-Server -IncludeManagementTools

檢視 IIS 歡迎使用頁面View the IIS welcome page

安裝 IIS 後,在您的 VM 上開啟連接埠 80,即可使用任何瀏覽器來檢視預設的 IIS 歡迎使用畫面。With IIS installed, and with port 80 open on your VM, you can use any browser to view the default IIS welcome page. 使用您在前一節中記載的 publicIpAddress 來瀏覽預設網頁。Use the publicIpAddress you documented in the previous section to visit the default page.

IIS 預設網站

刪除 VMDelete the VM

當不再需要時,請使用下列命令來移除包含 VM 及其相關資源的資源群組:When no longer needed, use the following command to remove the resource group that contains the VM and its related resources:

Remove-AzResourceGroup `
  -Name $ResourceGroupName

後續步驟Next steps

在本快速入門中,您部署了簡單的 Windows VM。In this quickstart, you've deployed a simple Windows VM. 若要深入了解 Azure Stack Hub VM,請參閱 Azure Stack Hub VM 功能To learn more about Azure Stack Hub VMs, continue to Azure Stack Hub VM features.