The Script:
# removeMissingResourcesInVmm.ps1
$noConfirmations=$false # $true = no confirmations, $false=confirm each stale record removal
function removeMissingResourcesInVmm($noConfirmations=$false){
function confirmation($content,$testValue="I confirm",$maxAttempts=3){
$confirmed=$false
$cancelCondition=@('cancel','no','exit','nope')
$attempts=0
clear-host
write-host $($content|out-string).trim()
write-host "`r`nPlease review this content for accuracy.`r`n"
while ($attempts -le $maxAttempts){
if($attempts++ -ge $maxAttempts){
write-host "A maximum number of attempts have reached. No confirmations received!`r`n"
break;
}
$userInput = Read-Host -Prompt "Please type in this value => $testValue <= to confirm. Input CANCEL to skip this item";
if ($userInput.ToLower() -eq $testValue.ToLower()){
$confirmed=$true;
write-host "Confirmed!`r`n";
break;
}elseif($userInput.tolower() -in $cancelCondition){
write-host 'Cancel command received.'
$confirmed=$false
break
}else{
clear-host
$content|write-host
write-host "Attempt number $attempts of $maxAttempts`: $userInput does not match $testValue. Try again or Input CANCEL to skip this item`r`n"
}
}
return $confirmed
}
try{
write-host "Removing all stale ISO records of resources from the VMM Library"
if($noConfirmations){
Get-SCISO | where {$_.State -eq "missing"} | Remove-SCISO
}else{
$confirmed=confirmation "Remove orphaned ISO records from VMM"
if($confirmed){
Get-SCISO | where {$_.State -eq "missing"} | Remove-SCISO
}else{
write-warning "Skipped orphaned ISO records removal"
}
}
write-host "Removing all stale Custom Script records of resources from the VMM Library"
if($noConfirmations){
Get-SCScript | where {$_.State -eq "missing"} | Remove-SCScript
}else{
$confirmed=confirmation "Remove orphaned Custom Script records from VMM"
if($confirmed){
Get-SCScript | where {$_.State -eq "missing"} | Remove-SCScript
}else{
write-warning "Skipped orphaned Custom Script records removal"
}
}
write-host "Removing all stale Driver records of resources from the VMM Library"
if($noConfirmations){
Get-SCDriverPackage | where {$_.State -eq "missing"} | Remove-SCDriverPackage
}else{
$confirmed=confirmation "Remove orphaned Driver records from VMM"
if($confirmed){
Get-SCDriverPackage | where {$_.State -eq "missing"} | Remove-SCDriverPackage
}else{
write-warning "Skipped orphaned Driver records removal"
}
}
write-host "Removing all stale Application records of resources from the VMM Library"
if($noConfirmations){
Get-SCApplicationPackage | where {$_.State -eq "missing"} | Remove-SCApplicationPackage
}else{
$confirmed=confirmation "Remove orphaned Application records from VMM"
if($confirmed){
Get-SCApplicationPackage | where {$_.State -eq "missing"} | Remove-SCApplicationPackage
}else{
write-warning "Skipped orphaned Application records removal"
}
}
write-host "Removing all stale Custom Resource records of resources from the VMM Library"
if($noConfirmations){
Get-SCCustomResource | where {$_.State -eq "missing"} | Remove-SCCustomResource
}else{
$confirmed=confirmation "Remove orphaned Custom Resource records from VMM"
if($confirmed){
Get-SCCustomResource | where {$_.State -eq "missing"} | Remove-SCCustomResource
}else{
write-warning "Skipped orphaned Custom Resource records removal"
}
}
write-host "Removing all stale Virtual Disk records of resources from the VMM Library"
if($noConfirmations){
Get-SCVirtualHardDisk | where {$_.State -eq "missing"} | Remove-SCVirtualHardDisk
}else{
$confirmed=confirmation "Remove orphaned Virtual Disk records from VMM"
if($confirmed){
Get-SCVirtualHardDisk | where {$_.State -eq "missing"} | Remove-SCVirtualHardDisk
}else{
write-warning "Skipped orphaned Virtual Disk records removal"
}
}
# Remove stale virtual machine records in VMM
# Note: this does not delete the VMs from Hyper-V
$missingVms=Get-SCVirtualMachine|?{$_.status -eq 'Missing'}
write-host "There are $($missingVms.count) missing VMs being detected."
if($missingVms){
foreach ($missingVm in $missingVms){
if($noconfirmation){
Remove-SCVirtualMachine -vm $missingVm -Force
}else{
$confirmed=confirmation "Remove VM $($missingVm.Name) from VMM"
if($confirmed){
Remove-SCVirtualMachine -vm $missingVm -Force
}
}
}
}else{
write-host "There are no virtual machines with current status of 'missing' to clear." -ForegroundColor Green
}
return $true
}catch{
write-warning $_
return $false
}
}
removeMissingResourcesInVmm $noConfirmations
The Individual Commands:
# How to purge all erroneous records of resources from the VMM Library
# ISOs
Get-SCISO | where {$_.State -eq "missing"} | Remove-SCISO
# Custom Scripts
Get-SCScript | where {$_.State -eq "missing"} | Remove-SCScript
# Drivers
Get-SCDriverPackage | where {$_.State -eq "missing"} | Remove-SCDriverPackage
# Applications
Get-SCApplicationPackage | where {$_.State -eq "missing"} | Remove-SCApplicationPackage
# Custom Resources
Get-SCCustomResource | where {$_.State -eq "missing"} | Remove-SCCustomResource
# Virtual Disks
Get-SCVirtualHardDisk | where {$_.State -eq "missing"} | Remove-SCVirtualHardDisk
# Virtual Machines
# Remove stale virtual machine records in VMM: VMs with
# Note: this does not delete the VMs from Hyper-V
$missingVms=Get-SCVirtualMachine|?{$_.status -eq 'Missing'}
function confirmation($content,$testValue="I confirm",$maxAttempts=3){
$confirmed=$false
$cancelCondition=@('cancel','no','exit','nope')
$attempts=0
clear-host
write-host $($content|out-string).trim()
write-host "`r`nPlease review this content for accuracy.`r`n"
while ($attempts -le $maxAttempts){
if($attempts++ -ge $maxAttempts){
write-host "A maximum number of attempts have reached. No confirmations received!`r`n"
break;
}
$userInput = Read-Host -Prompt "Please type in this value => $testValue <= to confirm. Input CANCEL to skip this item";
if ($userInput.ToLower() -eq $testValue.ToLower()){
$confirmed=$true;
write-host "Confirmed!`r`n";
break;
}elseif($userInput.tolower() -in $cancelCondition){
write-host 'Cancel command received.'
$confirmed=$false
break
}else{
clear-host
$content|write-host
write-host "Attempt number $attempts of $maxAttempts`: $userInput does not match $testValue. Try again or Input CANCEL to skip this item`r`n"
}
}
return $confirmed
}
write-host "There are $($missingVms.count) missing VMs being detected."
foreach ($missingVm in $missingVms){
$confirmed=confirmation "Remove VM $($missingVm.Name) from VMM"
if($confirmed){
Remove-SCVirtualMachine -vm $missingVm -Force
}
}
Sample Outputs:
PS C:\Windows\system32> Get-SCISO | where {$_.State -eq "missing"} | Remove-SCISO
Release :
State : Missing
LibraryShareId : 00000000-0000-0000-0000-000000000000
SharePath : C:\Windows\system32\vmguest.iso
FileShare :
Directory : C:\Windows\system32
Size : 0
IsOrphaned : False
FamilyName :
Namespace :
ReleaseTime :
HostVolumeId :
HostVolume :
Classification :
HostId :
HostType : VMHost
HostName : hv1.intranet.kimconnect.com
VMHost : hv1.intranet.kimconnect.com
LibraryServer :
CloudId :
Cloud :
LibraryGroup :
GrantedToList : {}
UserRoleID : 00000000-0000-0000-0000-000000000000
UserRole :
Owner :
ObjectType : ISO
Accessibility : Public
Name : vmguest
IsViewOnly : False
Description :
AddedTime : 7/22/1920 9:04:52 AM
ModifiedTime : 7/22/1920 9:04:52 AM
Enabled : True
MostRecentTask :
ServerConnection : Microsoft.SystemCenter.VirtualMachineManager.Remoting.ServerConnection
ID : 862c2f67-4c2c-4588-8a4f-16ed3c64366f
MarkedForDeletion : True
IsFullyCached : True
MostRecentTaskIfLocal :
### Truncated similar outputs ###
# Checking custom resources
PS C:\Windows\system32> Get-SCCustomResource|select name
Name
----
SAV_x86_en-US_4.9.305.198.cr
WebDeploy_x86_en-US_3.1237.1764.cr
WebDeploy_x64_en-US_3.1237.1764.cr
SAV_x64_en-US_4.9.305.198.cr
Categories: