在Azure门户中更改Azure虚拟机名称

时间:2018-04-10 14:25:17

标签: azure azure-powershell azureportal

我们可以在Azure门户中更改Azure虚拟机的名称吗?我确信我们无法通过门户网站更改它,我们是否有任何PowerShell cmdlet来更改虚拟机名称?

注意:我不是指VM中的VM名称,而是指Azure Portal中显示的名称。

7 个答案:

答案 0 :(得分:4)

虚拟机的资源名称是不可变的。因此,您需要重新部署虚拟机。

执行此操作的最佳方法是删除当前的磁盘,维护磁盘,然后使用指向这些磁盘的正确名称创建一个新磁盘。

答案 1 :(得分:2)

此非破坏性天蓝色Powershell脚本的创建者是Oliver Miossec,该脚本创建了一个新的VM,并对参数进行了一些更改。您可以找到他的帖子here。真棒,因为它还可以修复与机器关联的名字不正确的光盘和NIC。

#Deletes and recreates a VM
param (
    [Parameter(Mandatory=$true)]
    [string]$vmOldName,
    [Parameter(Mandatory=$true)]
    [string]$vmNewName,
    [Parameter(Mandatory=$true)]
    [string]$rgName,
    [string]$newLocation #Optional, moves the VM to a new Geographic Location
)

$ErrorActionPreference = "Stop"

Write-Host "Get and Shutdown old VM, Create new config"
$SourceVmObject = get-azvm -Name $vmOldName -ResourceGroupName $rgName

if ([string]::IsNullOrEmpty($newLocation)) {    
    $newLocation = $SourceVmObject.Location
    Write-Host "No new location specified, using the current VM's existing location: $newLocation"
}

$SourceVmPowerStatus = (get-azvm -Name $SourceVmObject.Name -ResourceGroupName $SourceVmObject.ResourceGroupName -Status).Statuses | where-object code -like "PowerState*"

if ($SourceVmPowerStatus -ne "VM deallocated") {
    stop-azVm -Name $SourceVmObject.Name -ResourceGroupName $SourceVmObject.ResourceGroupName -Force
    Start-Sleep -Seconds 30 #Wait to ensure VM is shutdown.
}

$NewVmObject = New-AzVMConfig -VMName $vmNewName -VMSize $SourceVmObject.HardwareProfile.VmSize 

Write-Host "Create new Network Objects"
$subnetID = (Get-AzNetworkInterface -ResourceId $SourceVmObject.NetworkProfile.NetworkInterfaces[0].id).IpConfigurations.Subnet.id

$nic = New-AzNetworkInterface -Name "$($vmNewName.ToLower())-0-nic" -ResourceGroupName $SourceVmObject.ResourceGroupName  -Location $SourceVmObject.Location -SubnetId $SubnetId 

Add-AzVMNetworkInterface -VM $NewVmObject -Id $nic.Id

Write-Host "Move OS Disk"
$SourceOsDiskSku = (get-azdisk -ResourceGroupName $SourceVmObject.ResourceGroupName -DiskName $SourceVmObject.StorageProfile.OsDisk.name).Sku.Name

$SourceOsDiskSnapConfig =  New-AzSnapshotConfig  -SourceUri $SourceVmObject.StorageProfile.OsDisk.ManagedDisk.Id -Location $SourceVmObject.Location -CreateOption copy

$SourceOsDiskSnap = New-AzSnapshot -Snapshot $SourceOsDiskSnapConfig  -SnapshotName "$($SourceVmObject.Name)-os-snap"  -ResourceGroupName $SourceVmObject.ResourceGroupName

$TargetOsDiskConfig = New-AzDiskConfig -AccountType $SourceOsDiskSku -Location $SourceVmObject.Location -CreateOption Copy -SourceResourceId $SourceOsDiskSnap.Id

$TargetOsDisk = New-AzDisk -Disk $TargetOsDiskConfig -ResourceGroupName $SourceVmObject.ResourceGroupName -DiskName "$($vmNewName.ToLower())-os-vhd"

Set-AzVMOSDisk -VM $NewVmObject -ManagedDiskId $TargetOsDisk.Id -CreateOption Attach -Windows

Write-Host "Create new Data Disks"
Foreach ($SourceDataDisk in $SourceVmObject.StorageProfile.DataDisks) { 

    $SourceDataDiskSku = (get-azdisk -ResourceGroupName $SourceVmObject.ResourceGroupName -DiskName $SourceDataDisk.name).Sku.Name

    $SourceDataDiskSnapConfig =  New-AzSnapshotConfig  -SourceUri $SourceDataDisk.ManagedDisk.Id -Location $SourceVmObject.Location -CreateOption copy

    $SourceDataDiskSnap = New-AzSnapshot -Snapshot $SourceDataDiskSnapConfig  -SnapshotName "$($SourceVmObject.Name)-$($SourceDataDisk.name)-snap"  -ResourceGroupName $SourceVmObject.ResourceGroupName

    $TargetDataDiskConfig = New-AzDiskConfig -AccountType $SourceDataDiskSku -Location $SourceVmObject.Location -CreateOption Copy -SourceResourceId $SourceDataDiskSnap.Id

    $TargetDataDisk = New-AzDisk -Disk $TargetDataDiskConfig -ResourceGroupName $SourceVmObject.ResourceGroupName -DiskName "$($vmNewName.ToLower())-$($SourceDataDisk.lun)-vhd"


    Add-AzVMDataDisk -VM $NewVmObject -Name "$($vmNewName.ToLower())-$($SourceDataDisk.lun)-vhd" -ManagedDiskId $TargetDataDisk.Id -Lun $SourceDataDisk.lun -CreateOption "Attach"
}

Write-Host "Creating..."
New-AzVM -VM $NewVmObject -ResourceGroupName $SourceVmObject.ResourceGroupName -Location $SourceVmObject.Location
Write-Host "VM Created..."

答案 2 :(得分:2)

Ryanman already posted一个最初由Oliver Miossec编写的脚本。但是,此脚本仅适用于Windows VM。

我添加了一个参数(-osType)来指定osType,因此它也适用于Linux VM。

#Deletes and recreates a VM
param (
    [Parameter(Mandatory=$true)]
    [string]$vmOldName,
    [Parameter(Mandatory=$true)]
    [string]$vmNewName,
    [Parameter(Mandatory=$true)]
    [string]$rgName,
    [string]$osType,
    [string]$newLocation #Optional, moves the VM to a new Geographic Location
)

$ErrorActionPreference = "Stop"

if ([string]::IsNullOrEmpty($osType)) {    
    $osType = "Windows"
    Write-Host "No OsType specified, using the default option: $osType"
}

Write-Host "Get and shutdown old VM, Create new config"
$SourceVmObject = get-azvm -Name $vmOldName -ResourceGroupName $rgName

if ([string]::IsNullOrEmpty($newLocation)) {    
    $newLocation = $SourceVmObject.Location
    Write-Host "No new location specified, using the current VM's existing location: $newLocation"
}

$SourceVmPowerStatus = (get-azvm -Name $SourceVmObject.Name -ResourceGroupName $SourceVmObject.ResourceGroupName -Status).Statuses | where-object code -like "PowerState*"

if ($SourceVmPowerStatus -ne "VM deallocated") {
    stop-azVm -Name $SourceVmObject.Name -ResourceGroupName $SourceVmObject.ResourceGroupName -Force
    Start-Sleep -Seconds 30 #Wait to ensure VM is shutdown.
}

$NewVmObject = New-AzVMConfig -VMName $vmNewName -VMSize $SourceVmObject.HardwareProfile.VmSize 

Write-Host "Create new Network Objects"
$subnetID = (Get-AzNetworkInterface -ResourceId $SourceVmObject.NetworkProfile.NetworkInterfaces[0].id).IpConfigurations.Subnet.id

$nic = New-AzNetworkInterface -Name "$($vmNewName.ToLower())-0-nic" -ResourceGroupName $SourceVmObject.ResourceGroupName  -Location $SourceVmObject.Location -SubnetId $SubnetId 

Add-AzVMNetworkInterface -VM $NewVmObject -Id $nic.Id

Write-Host "Move OS Disk"
$SourceOsDiskSku = (get-azdisk -ResourceGroupName $SourceVmObject.ResourceGroupName -DiskName $SourceVmObject.StorageProfile.OsDisk.name).Sku.Name

$SourceOsDiskSnapConfig =  New-AzSnapshotConfig  -SourceUri $SourceVmObject.StorageProfile.OsDisk.ManagedDisk.Id -Location $SourceVmObject.Location -CreateOption copy

$SourceOsDiskSnap = New-AzSnapshot -Snapshot $SourceOsDiskSnapConfig  -SnapshotName "$($SourceVmObject.Name)-os-snap"  -ResourceGroupName $SourceVmObject.ResourceGroupName

$TargetOsDiskConfig = New-AzDiskConfig -AccountType $SourceOsDiskSku -Location $SourceVmObject.Location -CreateOption Copy -SourceResourceId $SourceOsDiskSnap.Id

$TargetOsDisk = New-AzDisk -Disk $TargetOsDiskConfig -ResourceGroupName $SourceVmObject.ResourceGroupName -DiskName "$($vmNewName.ToLower())-os-vhd"

Set-AzVMOSDisk -VM $NewVmObject -ManagedDiskId $TargetOsDisk.Id -CreateOption Attach -$osType

Write-Host "Create new Data Disks"
Foreach ($SourceDataDisk in $SourceVmObject.StorageProfile.DataDisks) { 

    $SourceDataDiskSku = (get-azdisk -ResourceGroupName $SourceVmObject.ResourceGroupName -DiskName $SourceDataDisk.name).Sku.Name

    $SourceDataDiskSnapConfig =  New-AzSnapshotConfig  -SourceUri $SourceDataDisk.ManagedDisk.Id -Location $SourceVmObject.Location -CreateOption copy

    $SourceDataDiskSnap = New-AzSnapshot -Snapshot $SourceDataDiskSnapConfig  -SnapshotName "$($SourceVmObject.Name)-$($SourceDataDisk.name)-snap"  -ResourceGroupName $SourceVmObject.ResourceGroupName

    $TargetDataDiskConfig = New-AzDiskConfig -AccountType $SourceDataDiskSku -Location $SourceVmObject.Location -CreateOption Copy -SourceResourceId $SourceDataDiskSnap.Id

    $TargetDataDisk = New-AzDisk -Disk $TargetDataDiskConfig -ResourceGroupName $SourceVmObject.ResourceGroupName -DiskName "$($vmNewName.ToLower())-$($SourceDataDisk.lun)-vhd"


    Add-AzVMDataDisk -VM $NewVmObject -Name "$($vmNewName.ToLower())-$($SourceDataDisk.lun)-vhd" -ManagedDiskId $TargetDataDisk.Id -Lun $SourceDataDisk.lun -CreateOption "Attach"
}

Write-Host "Creating..."
New-AzVM -VM $NewVmObject -ResourceGroupName $SourceVmObject.ResourceGroupName -Location $SourceVmObject.Location
Write-Host "VM Created..."

答案 3 :(得分:0)

您无法重命名VM名称,但您可以更新VM大小,甚至可以将VM移动到其他/更改资源组的一个订阅。

答案 4 :(得分:0)

如果从内部VM /应用程序实例到VM的连接遇到任何问题。您可以在/ etc / hosts和/ etc / hostname中更改名称。 编辑这两个文件后,您需要重新启动实例。

vi / etc / hosts

127.0.0.1本地主机localhost.localdomain

vi / etc / hostname

localhost.localdomain

答案 5 :(得分:0)

接受的答案不正确。

在Ubuntu Linux上更改计算机名称的过程:

键入以下命令,使用nano或vi文本编辑器编辑/ etc / hostname:

sudo nano /etc/hostname

删除旧名称并设置新名称。 接下来编辑/ etc / hosts文件:

sudo nano /etc/hosts

将所有出现的现有计算机名称替换为新名称。 重新启动系统以使更改生效:

sudo reboot

来源:https://www.cyberciti.biz/faq/ubuntu-change-hostname-command/

我们能够成功更改主机名,而无需删除任何VM:)

答案 6 :(得分:0)

在 Azure VM 中,资源名称一旦部署就无法修改,因此最佳做法是保留磁盘并在之前的磁盘之后创建一个具有适当名称的新 VM。