如何通过 Powershell 获取新创建的 Azure VM 的 IP 地址

How to get the IP address of the newly created Azure VM via Powershell

我正在尝试自动化我们项目中的一些流程,其中包括一些步骤,例如创建 VM、连接到新创建的 VM 和 运行 一些远程命令。

现在我以前做的是运行手动按顺序执行此命令。

1.Create 一个虚拟机

New-AzureRmResourceGroupDeployment -Name VmDeployment -ResourceGroupName XYZ`
  -TemplateFile "C:\Templates\template.json" `
  -TemplateParameterFile "C:\Templates\parameters.json"

2.Connect 到虚拟机。

Set-Item WSMan:\localhost\Client\TrustedHosts -Value  100.9.4.12     
$UserName = "100.9.4.12\admin"
$Password = ConvertTo-SecureString "admin@123" -AsPlainText -Force
$psCred = New-Object System.Management.Automation.PSCredential($UserName, $Password)
$s = New-PSSession -ComputerName 100.9.4.12 -Credential $psCred
Invoke-Command -Session $s -ScriptBlock {Get-Service 'ServiceName'}

在此 IP 地址用于添加 client.I 上的受信任主机,用于检查 Azure 门户上生成的 IP 地址,在命令中替换该 IP 并 运行 它们手动。但是现在,由于我是自动化的,所以过程中不会有人工干预。

那么我该如何获取新创建的虚拟机的IP地址呢?

你为什么关心IP地址?只使用 dns 名称?你总是知道那个(因为你在创建虚拟机时定义了它,至少你可以做到这一点)。另一种选择是将 ip 地址作为 arm 模板的一部分输出。

或者在powershell中查询ip地址:

Get-AzureRmVM -ResourceGroupName ‘HSG-ResourceGroup’ -Name ‘HSG-LinuxVM’ | Get-AzureRmPublicIpAddress

与您的问题没有直接关系,但您是否考虑过使用 Terraform 自动创建资源? http://terraform.io

Terraform 与 A​​RM 非常相似(只是更好)这是创建 VM 和 public IP 导出的示例:



    resource "azurerm_resource_group" "main" {
      name     = "test-resources"
      location = "West US 2"
    }

    resource "azurerm_virtual_network" "main" {
      name                = "test-network"
      address_space       = ["10.0.0.0/16"]
      location            = "${azurerm_resource_group.main.location}"
      resource_group_name = "${azurerm_resource_group.main.name}"
    }

    resource "azurerm_subnet" "internal" {
      name                 = "internal"
      resource_group_name  = "${azurerm_resource_group.main.name}"
      virtual_network_name = "${azurerm_virtual_network.main.name}"
      address_prefix       = "10.0.2.0/24"
    }

    resource "azurerm_public_ip" "test" {
      name                         = "test-public-ip"
      location                     = "${azurerm_resource_group.main.location}"
      resource_group_name          = "${azurerm_resource_group.main.name}"
      public_ip_address_allocation = "dynamic"

      tags {
        environment = "production"
      }
    }

    resource "azurerm_network_interface" "main" {
      name                = "test-nic"
      location            = "${azurerm_resource_group.main.location}"
      resource_group_name = "${azurerm_resource_group.main.name}"

      ip_configuration {
        name                          = "testconfiguration1"
        subnet_id                     = "${azurerm_subnet.internal.id}"
        private_ip_address_allocation = "dynamic"
        public_ip_address_id = "${azurerm_public_ip.test.id}"
      }
    }

    resource "azurerm_virtual_machine" "main" {
      name                  = "test-vm"
      location              = "${azurerm_resource_group.main.location}"
      resource_group_name   = "${azurerm_resource_group.main.name}"
      network_interface_ids = ["${azurerm_network_interface.main.id}"]
      vm_size               = "Standard_DS1_v2"

      # Uncomment this line to delete the OS disk automatically when deleting the VM
      # delete_os_disk_on_termination = true


      # Uncomment this line to delete the data disks automatically when deleting the VM
      # delete_data_disks_on_termination = true

      storage_image_reference {
        publisher = "Canonical"
        offer     = "UbuntuServer"
        sku       = "16.04-LTS"
        version   = "latest"
      }

      storage_os_disk {
        name              = "myosdisk1"
        caching           = "ReadWrite"
        create_option     = "FromImage"
        managed_disk_type = "Standard_LRS"
      }

      os_profile {
        computer_name  = "hostname"
        admin_username = "testadmin"
        admin_password = "Password1234!"
      }

      os_profile_linux_config {
        disable_password_authentication = false
      }

      tags {
          environment = "production"
      }
    }

    output "vm-ip" {
        value = "${azurerm_public_ip.test.ip_address}"
    }

您可以使用Powershell命令获取所有虚拟机的私有IP。命令将像这样:

Get-AzureRmNetworkInterface -ResourceGroupName resourceGroupName | Select-Object {$_.IpConfigurations.PrivateIpAddress}

更新

还有像这样设置变量的命令:

$vm = Get-AzureRmNetworkInterface -ResourceGroupName charles | Select-Object -Property  @{Name="Ip"; Expression = {$_.IpConfigurations.PrivateIpAddress}}
$vm[0].Ip

那么你只会得到IP地址。