使用 PowerShell 建立具有靜態公用 IP 位址的虛擬機器Create a virtual machine with a static public IP address using PowerShell

注意

本文已更新為使用新的 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.

您可以建立具有靜態公用 IP 位址的虛擬機器。You can create a virtual machine with a static public IP address. 公用 IP 位址可讓您從網際網路與虛擬機器通訊。A public IP address enables you to communicate to a virtual machine from the internet. 指派靜態公用 IP 位址 (而非動態位址),以確保位址永遠不會變更。Assign a static public IP address, rather than a dynamic address, to ensure that the address never changes. 深入了解靜態公用 IP 位址Learn more about static public IP addresses. 若要將指派給現有虛擬機器的公用 IP 位址從動態變更為靜態,或要處理私人 IP 位址,請參閱新增、變更或移除 IP 位址To change a public IP address assigned to an existing virtual machine from dynamic to static, or to work with private IP addresses, see Add, change, or remove IP addresses. 公用 IP 位址有象徵性費用,而每個訂用帳戶可用的公用 IP 位址數目都有限制Public IP addresses have a nominal charge, and there is a limit to the number of public IP addresses that you can use per subscription.

建立虛擬機器Create a virtual machine

您可以從本機電腦或使用 Azure Cloud Shell 來完成下列步驟。You can complete the following steps from your local computer or by using the Azure Cloud Shell. 若要使用您的本機電腦,請確定已安裝 Azure PowerShellTo use your local computer, ensure you have the Azure PowerShell installed. 若要使用 Azure Cloud Shell,請選取後續任何命令方塊右上角的 [試試看]。To use the Azure Cloud Shell, select Try It in the top right corner of any command box that follows. Cloud Shell 可讓您登入 Azure。The Cloud Shell signs you into Azure.

  1. 如果使用 Cloud Shell,請跳至步驟 2。If using the Cloud Shell, skip to step 2. 開啟命令工作階段,然後使用 Connect-AzAccount 登入 Azure。Open a command session and sign into Azure with Connect-AzAccount.

  2. 使用 New-AzResourceGroup 命令來建立資源群組。Create a resource group with the New-AzResourceGroup command. 下列範例會在美國東部 Azure 區域中建立一個資源群組:The following example creates a resource group in the East US Azure region:

    New-AzResourceGroup -Name myResourceGroup -Location EastUS
    
  3. 使用 new-azvm 命令來建立虛擬機器。Create a virtual machine with the New-AzVM command. -AllocationMethod "Static" 選項可將靜態公用 IP 位址指派給虛擬機器。The -AllocationMethod "Static" option assigns a static public IP address to the virtual machine. 下列範例會建立 Windows Server 虛擬機器,其具有名為 myPublicIpAddress 的靜態、基本 SKU 公用 IP 位址。The following example creates a Windows Server virtual machine with a static, basic SKU public IP address named myPublicIpAddress. 出現提示時,請提供使用者名稱與密碼以作為虛擬機器的登入認證:When prompted, provide a username and password to be used as the sign in credentials for the virtual machine:

    New-AzVm `
      -ResourceGroupName "myResourceGroup" `
      -Name "myVM" `
      -Location "East US" `
      -PublicIpAddressName "myPublicIpAddress" `
      -AllocationMethod "Static"
    

    如果公用 IP 位址必須是標準 SKU,您必須在不同的步驟中建立公用 IP 位址建立網路介面將公用 IP 位址指派給網路介面,然後透過網路介面建立虛擬機器If the public IP address must be a standard SKU, you have to create a public IP address, create a network interface, assign the public IP address to the network interface, and then create a virtual machine with the network interface, in separate steps. 深入了解公用 IP 位址 SKULearn more about Public IP address SKUs. 如果虛擬機器將會新增至公用 Azure Load Balancer 的後端集區,則虛擬機器公用 IP 位址的 SKU 必須符合負載平衡器公用 IP 位址的 SKU。If the virtual machine will be added to the back-end pool of a public Azure Load Balancer, the SKU of the virtual machine's public IP address must match the SKU of the load balancer's public IP address. 如需詳細資訊,請參閱 Azure Load BalancerFor details, see Azure Load Balancer.

  4. 查看指派的公用 IP 位址,並確認它已建立為靜態位址,並具有 >get-azpublicipaddressView the public IP address assigned and confirm that it was created as a static address, with Get-AzPublicIpAddress:

    Get-AzPublicIpAddress `
      -ResourceGroupName "myResourceGroup" `
      -Name "myPublicIpAddress" `
      | Select "IpAddress", "PublicIpAllocationMethod" `
      | Format-Table
    

    Azure 已從您建立虛擬機器所在區域使用的位址中指派公用 IP 位址。Azure assigned a public IP address from addresses used in the region you created the virtual machine in. 您可以針對 Azure 公開US Gov中國德國雲端,下載範圍 (前置詞) 清單。You can download the list of ranges (prefixes) for the Azure Public, US government, China, and Germany clouds.

警告

請勿修改虛擬機器的作業系統內的 IP 位址設定。Do not modify the IP address settings within the virtual machine's operating system. 作業系統不會察覺 Azure 公用 IP 位址。The operating system is unaware of Azure public IP addresses. 雖然您可以將私人 IP 位址設定新增至作業系統,但是建議不要這麼做 (除非有需要),而且在閱讀將私人 IP 位址新增至作業系統之後才能這麼做。Though you can add private IP address settings to the operating system, we recommend not doing so unless necessary, and not until after reading Add a private IP address to an operating system.

清除資源Clean up resources

您可以使用 Remove-AzResourceGroup 來移除不再需要的資源群組,以及其所包含的所有資源:When no longer needed, you can use Remove-AzResourceGroup to remove the resource group and all of the resources it contains:

Remove-AzResourceGroup -Name myResourceGroup -Force

後續步驟Next steps