如果从 Azure Stack Hub 中删除 VM,则组件依赖项为 ;即数据磁盘、虚拟网络接口和诊断容器保留在资源组中。 这些项不会随 OS 磁盘一起自动删除。 按照本文中的步骤删除 OS 光盘和组件依赖项。
创建新 VM 时,通常创建一个新资源组,并在该资源组中放入所有依赖项。 如果想删除 VM 及其所有依赖项,可以删除整个资源组。 Azure 资源管理器已成功删除依赖项。 有时无法删除资源组以删除 VM;例如,VM 可能包含不是要保留的 VM 依赖项的资源。
如果无法删除资源组,则依赖项不在同一资源组中,或者存在其他资源。 执行以下步骤:
- 打开 Azure Stack 用户门户。
- 选择“虚拟机”。 查找虚拟机,然后选择计算机以打开虚拟机边栏选项卡。
- 记下包含 VM 和 VM 依赖项的资源组。
- 选择“网络”并记下网络接口。
- 选择“磁盘”并记下 OS 磁盘和数据磁盘。
- 返回到“虚拟机”边栏选项卡,并选择“删除” 。
- 键入 yes 以确认删除,然后选择“ 删除”。
- 选择“ 资源组”,然后选择资源组。
- 通过手动选择已记录的项来删除依赖项。 对于每个项目,选择“ 删除”:
- 键入 “是 ”以确认删除,然后选择“ 删除”。
- 等待资源完全删除。
- 然后,你可以删除下一个依赖项。
如果无法删除资源组,则依赖项不在同一资源组中,或者存在其他资源。 按照本部分中的步骤操作。
连接到 Azure Stack Hub 环境,然后使用 VM 名称和资源组更新以下变量。 有关将 PowerShell 会话连接到 Azure Stack Hub 的说明,请参阅 以用户身份使用 PowerShell 连接到 Azure Stack Hub。
$machineName = 'VM_TO_DELETE'
$resGroupName = 'RESOURCE_GROUP'
$machine = Get-AzVM -Name $machineName -ResourceGroupName $resGroupName
检索依赖项的 VM 信息和名称。 请在同一会话中运行以下 cmdlet:
$azResParams = @{
'ResourceName' = $machineName
'ResourceType' = 'Microsoft.Compute/virtualMachines'
'ResourceGroupName' = $resGroupName
}
$vmRes = Get-AzResource @azResParams
$vmId = $vmRes.Properties.VmId
删除启动诊断存储容器。 如果计算机名称短于 9 个字符,请在创建 $diagContainer
变量时将索引更改为子字符串中的字符串长度。
请在同一会话中运行以下 cmdlet:
$container = [regex]::match($machine.DiagnosticsProfile.bootDiagnostics.storageUri, '^http[s]?://(.+?)\.').groups[1].value
$diagContainer = ('bootdiagnostics-{0}-{1}' -f $machine.Name.ToLower().Substring(0, 9), $vmId)
$containerRg = (Get-AzStorageAccount | where { $_.StorageAccountName -eq $container }).ResourceGroupName
$storeParams = @{
'ResourceGroupName' = $containerRg
'Name' = $container }
Get-AzStorageAccount @storeParams | Get-AzStorageContainer | where { $_.Name-eq $diagContainer } | Remove-AzStorageContainer -Force
删除虚拟网络接口:
$machine | Remove-AzNetworkInterface -Force
删除操作系统磁盘:
$osVhdUri = $machine.StorageProfile.OSDisk.Vhd.Uri
$osDiskConName = $osVhdUri.Split('/')[-2]
$osDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $osVhdUri.Split('/')[2].Split('.')[0] }
$osDiskStorageAcct | Remove-AzStorageBlob -Container $osDiskConName -Blob $osVhdUri.Split('/')[-1] -Confirm:$true
删除附加到 VM 的数据磁盘:
if ($machine.StorageProfile.DataDisks.Name.Count -gt 0)
{
Write-Verbose -Message 'Deleting disks...'
foreach ($uri in $machine.StorageProfile.DataDisks.Vhd.Uri )
{
$dataDiskStorageAcct = Get-AzStorageAccount | where { $_.StorageAccountName -eq $uri.Split('/')[2].Split('.')[0]
$dataDiskStorageAcct | Remove-AzStorageBlob -Container $uri.Split('/')[-2] -Blob $uri.Split('/')[-1] -ea Ignore -Confirm:$true
}
}
最后,删除 VM。 该 cmdlet 需要一些时间才能运行。 可以通过查看 PowerShell 中的 VM 对象来审核附加到 VM 的组件。 若要查看对象,请参阅包含 VM 对象的变量。 键入 $machine
。
若要删除 VM,请在同一会话中运行以下命令:
$machine | Remove-AzVM -Force
如果无法删除资源组,则依赖项不在同一资源组中,或者存在其他资源。 按照本部分中的步骤操作。
连接到 Azure Stack Hub 环境,然后使用 VM 名称和资源组更新以下变量。 有关将 PowerShell 会话连接到 Azure Stack Hub 的说明,请参阅 以用户身份使用 PowerShell 连接到 Azure Stack Hub。
$machineName = 'VM_TO_DELETE'
$resGroupName = 'RESOURCE_GROUP'
$machine = Get-AzureRmVM -Name $machineName -ResourceGroupName $resGroupName
检索依赖项的 VM 信息和名称。 请在同一会话中运行以下 cmdlet:
$azResParams = @{
'ResourceName' = $machineName
'ResourceType' = 'Microsoft.Compute/virtualMachines'
'ResourceGroupName' = $resGroupName
}
$vmRes = Get-AzureRmResource @azResParams
$vmId = $vmRes.Properties.VmId
删除启动诊断存储容器。 如果计算机名称短于 9 个字符,请在创建 $diagContainer
变量时将索引更改为子字符串中的字符串长度。
请在同一会话中运行以下 cmdlet:
$container = [regex]::match($machine.DiagnosticsProfile.bootDiagnostics.storageUri, '^http[s]?://(.+?)\.').groups[1].value
$diagContainer = ('bootdiagnostics-{0}-{1}' -f $machine.Name.ToLower().Substring(0, 9), $vmId)
$containerRg = (Get-AzureRmStorageAccount | where { $_.StorageAccountName -eq $container }).ResourceGroupName
$storeParams = @{
'ResourceGroupName' = $containerRg
'Name' = $container }
Get-AzureRmStorageAccount @storeParams | Get-AzureStorageContainer | where { $_.Name-eq $diagContainer } | Remove-AzureStorageContainer -Force
删除虚拟网络接口:
$machine | Remove-AzureRmNetworkInterface -Force
删除操作系统磁盘:
$osVhdUri = $machine.StorageProfile.OSDisk.Vhd.Uri
$osDiskConName = $osVhdUri.Split('/')[-2]
$osDiskStorageAcct = Get-AzureRmStorageAccount | where { $_.StorageAccountName -eq $osVhdUri.Split('/')[2].Split('.')[0] }
$osDiskStorageAcct | Remove-AzureStorageBlob -Container $osDiskConName -Blob $osVhdUri.Split('/')[-1]
删除附加到 VM 的数据磁盘:
if ($machine.DataDiskNames.Count -gt 0)
{
Write-Verbose -Message 'Deleting disks...'
foreach ($uri in $machine.StorageProfile.DataDisks.Vhd.Uri )
{
$dataDiskStorageAcct = Get-AzureRmStorageAccount -Name $uri.Split('/')[2].Split('.')[0]
$dataDiskStorageAcct | Remove-AzureStorageBlob -Container $uri.Split('/')[-2] -Blob $uri.Split('/')[-1] -ea Ignore
}
}
最后,删除 VM。 该 cmdlet 需要一些时间才能运行。 可以通过查看 PowerShell 中的 VM 对象来审核附加到 VM 的组件。 若要查看对象,请参阅包含 VM 对象的变量。 键入 $machine
。
若要删除 VM,请在同一会话中运行以下命令:
$machine | Remove-AzureRmVM -Force