Highlighted
Contributor
Contributor

Script to find active VMs

Hi,

I have VMWare ESXi 5.5 and VCenter 6.0 version implemented in my company with around 300 vm's. On most of the VMs we are running out of disk space but we don't know how many are active and how many are not used since last xx days. we have not implemented VCenter Operations manager.

Is there a script where I can get below information:

Server Name: (name of the VM)

HDD spce:

RAM:

CPU:

Last login/Inactive since: xx days?

Please do help here, i am struggling to get a solution.

Regards,

Dhruv

0 Kudos
2 Replies
Highlighted
Expert
Expert

do you know the username and password to the vm's? (hopefully they are the same for each vm?)

0 Kudos
Highlighted
VMware Employee
VMware Employee

You can try the below script.

The script assumes the following :

  1. VM OS is either Windows or Linux

  2. password for all Linux VMs is same

Connect-VIServer -Server 'x.x.x.x' -User 'username' -Password 'passwd'
$vms = Get-VM
$report = @()
foreach($vm in $vms)
   {
     $row = "" | Select Name, "HDD Size", UsedSpace, RAM, CPU, "Last Login Date"
     $row.Name = $vm.Name
     $row."HDD Size" = ($vm | Get-HardDisk | Measure-Object CapacityGB -Sum).Sum
     $row.UsedSpace = "{0:N2}" -f $vm.UsedSpaceGB
     $row.RAM = $vm.ExtensionData.Config.Hardware.MemoryMB
     $row.CPU = $vm.ExtensionData.Config.Hardware.NumCPU

     $ip = $vm.ExtensionData.Guest.IpAddress

     if ((Get-VMGuest -VM $vm).OSFullName -match 'Windows')
         {

            $QueryString = Get-WinEvent -comp $ip -FilterHashtable @{Logname='Security';ID=4672} -MaxEvents 1
            $last_login = $QueryString.TimeCreated

         }

      else
         {

           New-SshSession -ComputerName $ip -Username root -Password 'password'
           $out = Invoke-SshCommand -ComputerName $ip -Command "last | head -n1"
           $last_login = ($out -split "\s+")[5] + '-' + ($out -split "\s+")[4] + '-' + '2017'
         }

      $row."Last Login Date" = (New-TimeSpan -Start $last_login).Days

      $report += $row
    }

$report | Export-Csv New.csv

0 Kudos