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)
{
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}
}
}
}
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
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.
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??
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.
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
Thanks Robert, How about the other query, Including VC Name and Cluster name in the report.