Given this awesome script from PowerCLI: Get every VM added to vCenter in the last 30 Days! - The Practical Administrator
I modified it to output to a CSV, now I'd like to add fields for CPU, memory, disk allocated- how is that best accomplished?
thanks!
#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","VMNumCpu","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).Name
}
Else
{
$srcTemplate = $null
}
write-output ""|Select-Object @{
Name="Name"
Expression={$Task.Vm.name}
}, @{
Name="CPU"
Expression={$Task.Vm.NumCpu}
}, @{
Name="Created"
Expression={$Task.CreatedTime}
}, @{
Name="UserName"
Expression={$Task.UserName}
}, @{
Name="Type"
Expression={$Task.gettype().name}
}, @{
Name="Template"
Expression={$srcTemplate}
} | export-csv -append SAN30dayreport.csv
}
}
You are looking at Events, in such an event there is a MoRef (pointer) to a VM.
See for example the VmClonedEVent, this object inherits properties from a number of other objects.
If you drill down to the Event, you will notice that this object has a property Vm.Vm.
In there you will find the MoRef. With Get-View you can get the VirtualMachine object to which this MoRef is pointing.
And in the object, you will find the Config.Hardware.NumCpu property.
A possible script could look something like this
Note that this kind of scrips require a certain knowledge of the vSphere SDK.
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","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).Name
}
Else
{
$srcTemplate = $null
}
$numcpu = $null
if($Task.VM.VM){
$vm = Get-View -Id $Task.VM.VM -ErrorAction SilentlyContinue
if($vm){
$numcpu = $vm.Config.Hardware.NumCpu
}
}
$report += '' | 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}
}, @{
Name="NumCPU"
Expression={$numcpu}
}
}
$report | Export-Csv report.csv -NoTypeInformation -UseCulture
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Tried your code - results in an empty report.csv
Sorry, my bad, typo.
I forgot to pipe a dummy into the Select, it's corrected now.
Please try again.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Great - CPU count is returned - how can I add memory (memoryMB returns null) and disk?:
}, @{
Name="NumCPU"
Expression={$numcpu}
}, @{
Name="MemoryMB"
Expression={$memorymb}
# }, @{
# Name="Disk"
# @{n="SpaceUsedGB"; e={[math]::round( $_.UsedSpaceGB )}
thanks Luc
You have access to the VirtualMachine object in $vm, so it's a matter of picking the properties.
Something like this.
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","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).Name
}
Else
{
$srcTemplate = $null
}
$numcpu = $null
if($Task.VM.VM){
$vm = Get-View -Id $Task.VM.VM -ErrorAction SilentlyContinue
if($vm){
$numcpu = $vm.Config.Hardware.NumCpu
$mem = $vm.Config.Hardware.MemoryMB
$dssum = $vm.Summary.Storage.Committed
}
}
$report += '' | 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}
}, @{
Name="NumCPU"
Expression={$numcpu}
}, @{
Name="MemoryMB"
Expression={$mem}
}, @{
Name="StorageCommitted"
Expression={$dssum}
}
}
$report | Export-Csv report.csv -NoTypeInformation -UseCulture
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Can the $dssum be better presented with @{n="SpaceUsedGB"; e={[math]::round( $_.UsedSpaceGB )} ?
Prime movitation for this report is funding more hardware to grow clusters - showing allocated disk space vs thin/used is better chargeback
thanks
You're dealing with a vSphere VirtualMachine object (obtained through the MoRef in the event), not a .Net VirtualMachine object (returned by Get-VM).
Under the cover the PowerCLI cmdlet is getting the values from the vSphere objects as well, but it will create a new .Net object, in which a number of selected properties are present.
Btw, do you see different values for space used from both objects?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Yes - the space reported by
@{n="SpaceUsedGB"; e={[math]::round( $_.UsedSpaceGB )}} is human readable - allocated disk space in GB
The $vm.Summary.Storage.Committed does not seem to match allocated disk at all - and its in 10^20 bits
We can't report from the two context's you mentioned in the same script?
thanks
You can divide by the constant 1GB and do a [math]::round
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference