jclarkfdc
Contributor
Contributor

Generate a report of VM's created, deployed and removed from VC.

I am trying to generate a script to list all VM's created/deployed/removed from the VC. I am able to get it to the console the way I want it, however I am having trouble coding it to export to csv.

Run Command:

.\VMsCreatedLastMonth.ps1 -LastDays 30

Script

#requires -pssnapin VMware.Vimautomation.Core

Param(

$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","VmRemovedEvent"

$EventManager = Get-View EventManager

$NewVmTasks = $EventManager.QueryEvents($EventFilterSpec)

Foreach ($Task in $NewVmTasks)

{

  1. 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}

}

}

}

Tags (2)
0 Kudos
6 Replies
LucD
Leadership
Leadership

Export-Csv expects an array of objects.

The easiest way is to create an array with the results and then feed that array to the Export-Csv cmdlet.

____________

Blog: LucD notes

Twitter: lucd22


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

sbrad001
Contributor
Contributor

Nice script!! Works nicely.

Is there a way to have the script add up the number of cloned, created and deployed events and then include that number along with the number of removed events in the report?

Basically looking for a way to tally up the number of events to generate a chart to track.

0 Kudos
pmeqix
Contributor
Contributor

Hi LucD

I know i have run this script in the past successfully but now i am getting the following error. i am not sure what is causing this error now.. any insights??

[vSphere PowerCLI] U:\Scripts> .\VMs-created-last-month.ps1 -LastDays 210
Method invocation failed because [System.Object[]] doesn't contain a method nam
ed 'QueryEvents'.
At U:\Scripts\VMs-created-last-month.ps1:18 char:44
+     $NewVmTasks = $EventManager.QueryEvents <<<< ($EventFilterSpec)
    + CategoryInfo          : InvalidOperation: (QueryEvents:String) [], Runti
   meException
    + FullyQualifiedErrorId : MethodNotFound
You cannot call a method on a null-valued expression.
At U:\Scripts\VMs-created-last-month.ps1:39 char:30
+                 $row.Type = $Task.gettype <<<< ().name
    + CategoryInfo          : InvalidOperation: (gettype:String) [], RuntimeEx
   ception
    + FullyQualifiedErrorId : InvokeMethodOnNull
0 Kudos
jingleharish
Contributor
Contributor

Hi Luc,

We have 2 virtual center in our enviroment and if I execute this script connecting to both the VC i am getting the below error;

Method invocation failed because [System.Object[]] doesn't contain a method named 'QueryE
vents'.
At C:\Documents and Settings\d40229g\Desktop\Harish\Deployed_VM.ps1:37 char:44
+     $NewVmTasks = $EventManager.QueryEvents <<<< ($EventFilterSpec)
    + CategoryInfo          : InvalidOperation: (QueryEvents:String) [], RuntimeExceptio
   n
    + FullyQualifiedErrorId : MethodNotFound

If the script is executed individually on each VC, Report is getting generated.

Also, Some modications are required.

>>The report should also contain the VC name & CLuster the VM's are hosted.

>>Script should run across both the VCs and provide a single output file.

Thanks in advance.

0 Kudos
RvdNieuwendijk
Leadership
Leadership

I modified Luc's version of the script to handle multiple connected vCenter servers.

#requires -pssnapin VMware.Vimautomation.Core
param(
    [int]
    $LastDays
)
process
{
    $report = @()
    $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","VmRemovedEvent"

    $EventManagers = Get-View EventManager
    foreach ($EventManager in $EventManagers) {
          $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
            }
            $row = "" | Select Name,Created,UserName,Type,Template
            $row.Name = $Task.Vm.name
            $row.Created = $Task.CreatedTime
            $row.UserName = $Task.UserName
            $row.Type = $Task.gettype().name
            $row.Template = $srcTemplate
            $report += $row
        }
    }
    $report | Export-Csv "report.csv" -NoTypeInformation -UseCulture
}

 

Regards, Robert

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
jingleharish
Contributor
Contributor

Thanks Robert, How about the other query, Including VC Name and Cluster name in the report.

0 Kudos