如果您從 Azure Stack Hub 移除 VM,則元件相依性;也就是說,資料磁片、虛擬網路介面和診斷容器會保留在資源群組中。 這些專案不會隨著您的 OS 磁片自動刪除。 請遵循本文中的步驟來移除您的 OS 光碟和元件相依性。
當您建立新的 VM 時,您通常會建立新的資源群組,並將所有相依性放在該資源群組中。 當您想要刪除 VM 及其所有相依性時,可以刪除資源群組。 Azure Resource Manager已成功刪除相依性。 有時候您無法刪除資源群組來移除 VM;例如,VM 可能包含的資源不是您想要保留之 VM 的相依性。
如果您無法刪除資源群組,則相依性不在相同的資源群組中,或還有其他資源。 請遵循下列步驟:
- 開啟 Azure Stack 使用者入口網站。
- 選取 [虛擬機器]。 尋找您的虛擬機器,然後選取您的機器以開啟 [虛擬機器] 分頁。
- 記下包含 VM 和 VM 相依性的資源群組。
- 選取網路功能,並記下網路介面。
- 選取磁碟並記下 OS 磁碟和資料磁碟。
- 返回 [虛擬機器] 分頁,然後選取 [刪除]。
- 輸入 yes 以確認刪除,然後選取 [ 刪除]。
- 選取 [資源群組],然後選取資源群組。
- 手動選取您附注的專案,以刪除相依性。 針對每個專案,選取 [刪除]:
- 輸入 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