gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 04:46:21 25 4
gpt4 key购买 nike

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

以前我所做的就是手动按顺序运行这些命令。

1.创建虚拟机

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

2.连接到虚拟机。

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 地址用于将其添加到客户端的受信任主机中。我曾经在 Azure 门户上检查生成的 IP 地址,替换命令中的该 IP 并手动运行它们。但现在,由于我正在自动化,因此该过程中不会有人工干预。

那么我应该如何检索新创建的虚拟机的 IP 地址?

最佳答案

与您的问题没有直接关系,但您是否考虑过使用 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}"
}

关于azure - 如何通过Powershell获取新创建的Azure VM的IP地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52070549/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com