Skip to content

Latest commit

 

History

History
73 lines (64 loc) · 2 KB

File metadata and controls

73 lines (64 loc) · 2 KB
pid 1085
author glnsize
title FindNewVirtualMachines
date 2009-05-08 04:57:08 -0700
format posh
parent 1084

FindNewVirtualMachines

Script to find any VM's that we're added to vCenter in the last x days.

Example: ./FindNewVirtualMachines.ps1 -LastDays 30

fixed a typo that caused source template enumeration to fail.

#requires -pssnapin VMware.Vimautomation.Core
Param(
    [int]
    $LastDays
)
Process
{
    $EventFilterSpecByTime = New-Object VMware.Vim.EventFilterSpecByTime
    If ($LastDays)
    {
        $EventFilterSpecByTime.BeginTime = (get-date).AddDays(-$($LastDays))
    }
    $EventFilterSpec = New-Object VMware.Vim.EventFilterSpec
    $EventFilterSpec.Time = $EventFilterSpecByTime
    $EventFilterSpec.DisableFullMessage = $False
    $EventFilterSpec.Type = "VmCreatedEvent","VmDeployedEvent","VmClonedEvent","VmDiscoveredEvent","VmRegisteredEvent"
    $EventManager = Get-View EventManager
    $NewVmTasks = $EventManager.QueryEvents($EventFilterSpec)

    Foreach ($Task in $NewVmTasks)
    {
        # If VM was deployed from a template then record which template.
        If ($Task.Template -and ($Task.SrcTemplate.Vm))
        {
            
            $srcTemplate = Get-View $Task.SrcTemplate.Vm -Property name | 
                Select -ExpandProperty Name
        }
        Else
        {
            $srcTemplate = $null
        }
        write-output ""|Select-Object @{
                Name="Name"
                Expression={$Task.Vm.name}
            }, @{
                Name="Created"
                Expression={$Task.CreatedTime}
            }, @{
                Name="UserName"
                Expression={$Task.UserName}
            }, @{        
                Name="Type"
                Expression={$Task.gettype().name}
            }, @{
                Name="Template"
                Expression={$srcTemplate}
            }
    } 
}