2018-03-30

How to Kill a Hanging Hyper-V VM


$VMName = Read-Host "Enter VM Name"

$VM = $(Get-VM | where {$_.Name -eq "test"} | select-object  -ExpandProperty VMId).Guid
if ($VM.Count -ne 1) {
write-host "Not Found"
} else {
$ProcessId = $(Get-WmiObject win32_process -Filter "name like '%vmwp%'" | where {$_.CommandLine -like "*$($VM)*"} | select ProcessId).ProcessId
$ForceKill = Read-Host "Found ProcessID: $ProcessId, Force Kill? (Y/N)"
if ($ForceKill -eq "y" -or $ForceKill -eq "Y") {
Stop-Process $ProcessId -Force
}
}

沒有留言: