Friday, July 8, 2011

Get VMSS Files

VMSS files are suspended state files which store the state of a suspended VMware's virtual machine. Sometimes they linger on even when the VM has been resumed from suspension. They can take up a lot of space. This script will find those files and write them out to a file.

#clear screen
cls

#remove any lingering variables
Remove-Variable -Name * -Force -ErrorAction SilentlyContinue

$vCenters = 'vCenter1', 'vCenter2'
foreach ($vCenter in $vCenters)
{

 $VIServer = Connect-VIServer $vCenter
 foreach ($VM in (Get-View -ViewType VirtualMachine ))
  {
  $x = $VM.LayoutEx.File
  Foreach ($File in $x)
   {
   if ($File.Name -like '*.vmss')
    {
    $output = $File.Name
    $output
    $output | Out-File c:\vmss.txt -Append
    }
   }
  }
}

Tuesday, July 5, 2011

Find VMs with Raw Device Mappings

#Clear screen
cls

# remove any lingering variables
Remove-Variable -Name * -Force -ErrorAction SilentlyContinue

#connect to Virtual Center Server
Connect-VIServer vCenter1

#get VMs
foreach($vm in (get-view -ViewType VirtualMachine))
{
  foreach($rdm in $vm.Config.Hardware.Device)
  {
   if(($rdm.gettype()).Name -eq "VirtualDisk")
   {
    if(($rdm.Backing.CompatibilityMode -eq "physicalMode") -or ($rdm.Backing.CompatibilityMode -eq "virtualMode"))
    {
     $Details = "" | select VMName, VMDK, Mode
     $Details.VMName = $vm.Name
     $Details.VMDK = $rdm.Backing.FileName
     $Details.Mode = $rdm.Backing.CompatibilityMode
     $Details.PSTypeNames.Clear()
     $Details
    }
   }
  }
}
    
Disconnect-VIServer vCenter1 -Confirm:$false