VMware Cloud Community
itsolutions4you
Contributor
Contributor

Powercli to Find Unused VM's

I have an interesting scenario. I have inherited a mid sized VMware Environment (ESX 3.5, vSphere 2.5, VMware View 3.1) and need to be able to determine what VDI sessions have not been used in at least 10 days.

I know don't flame me over the out of date builds, that is on my list after I stabilize the environment. I have quite a few VDI sessions that I feel are not ever used, but management will not allow me to power them off to save contrained resources unless I can prove they are not being used.

I would like to be able to say the VM is powered on but the last login was x days ago.

I would like to be able to run this per Pool.

Thanks in advance for any assistance.

6 Replies
Zsoldier
Expert
Expert

There really isn't a way via PowerCLI to find the information you are looking for.  View 3.1 doesn't make this task any easier.  The only way I can think you can do it is by checking the VM's Guest OS Audit Logs to determine when a system was last in use.

Chris Nakagaki (中垣浩一)
Blog: https://tech.zsoldier.com
Twitter: @zsoldier
0 Kudos
RyanMcL
Enthusiast
Enthusiast

you could use the Invoke-VMScript command to check the logs / WMI

For a linux vm something like

$vm = get-vm X

$cred = get-credential

Invoke-VMScript -VM $vm -guestcredenital $cred -scripttype bash -scripttext "last | head -n 2 | sort -r | head -n1"

For Windows

$vm = get-vm X
$cred = get-credential

Invoke-VMScript -VM $vm -guestcredenital $cred -scripttype powershell -scripttext <See Script Below>

$data = @()
$profiles = GWMI Win32_NetworkLoginProfile -ComputerName .
foreach ($profile in $profiles){
    $date = $profile.LastLogon
    if ($date -ne $null -and $date -ne "**************.******+***") {
        $row = "" | Select User,LogonTime
        $row.User = $Profile.Name
        $tdate = [datetime]::ParseExact($date.substring(0,14), "yyyyMMddHHmmss", $null)
        $row.logonTime = Get-Date $tdate -UFormat "%Y-%m-%d %T"
        $data += $row
    }
}
$data | Sort -Descending LogonTime

I've only just thrown this together so a bit of tweaking will be needed but should point you in the right direction.

Ryan

Ugotmail8
Contributor
Contributor

schepp
Leadership
Leadership

You think he didn't solve his problem during the last 3 years when this question was posted? Smiley Wink

0 Kudos
Ugotmail8
Contributor
Contributor

Yeah I thought that but I've been looking for a similar script and found that link which works perfectly so thought I'd reply more to share with everyone else to be honest.

Cheers

AlbertWT
Virtuoso
Virtuoso

Cool, thanks for sharing.

/* Please feel free to provide any comments or input you may have. */
0 Kudos