I see now I get it. Thank you, Sir.
I have a question: Today I saw in the Report that a VM which was deployed by a user the Script didn't find the User, who created this vm
What would be the cause?
this is the code:
Write-Host "Gathering VM statistics"
Measure-Command{
$view = Get-View -ViewType Virtualmachine
$cnt = 1
Write-Host "Found $($view.count) virtual machines"
$events = Get-VIEvent -Entity $view.Name -MaxSamples ([int]::MaxValue) |
Where-Object { $_ -is [VMware.Vim.VmDeployedEvent] -or
$_ -is [VMware.Vim.VmClonedEvent]-or
$_ -is [VMware.Vim.VmCreatedEvent]-or
$_ -is [VMware.Vim.VmPoweredOffEvent]-or
$_ -is [VMware.Vim.VmRegisteredEvent]}
Write-Host "Found $($view.count) virtual machines"
foreach($vm in $view ) {
if (!$vm.Config) { continue }
Write-Host "Processing $($cnt) - $($vm.Name)"
$vms = [PSCustomObject]@{
VMName = $vm.Name
Responsible_Team = ""
Hostname = $vm.guest.hostname
OS = $vm.Config.GuestFullName
IPAddress = $vm.guest.ipAddress
Boottime = $vm.Runtime.BootTime
PoweredOff = ($events | where{$_.VM.Name -eq $vm.Name} | Sort-Object -Property CreatedTime -Descending | Select -First 1).CreatedTime
VMState = $vm.summary.runtime.powerState
UsedSpaceGB = [math]::Round($vm.Summary.Storage.Committed/1GB,2)
ProvisionedSpaceGB = [math]::Round(($vm.Summary.Storage.Committed + $vm.Summary.Storage.UnCommitted)/1GB,2)
MemoryUsage = $vm.summary.quickStats.guestMemoryUsage
Datastore = $vm.Config.DatastoreUrl[0].Name
CreatedDate = $vm.Config.CreateDate
Unit = ""
Backup_Strategy = ""
To_Archive = ""
Notes = $vm.Config.Annotation -replace '(?:\r|\n)',''
Creator = ($events | where{$_.VM.Name -eq $vm.Name} | Sort-Object -Property CreatedTime -Descending | Select -First 1).UserName
}
foreach ($c in $vm.CustomValue) {
switch ($c.Key) {
"901" { $vms.Unit = $c.Value }
"913" { $vms.To_Archive = $c.Value }
"909" { $vms.Backup_Strategy = $c.Value }
"904" { $vms.Responsible_Team = $c.Value }
}
}
$Report.Add($vms) | Out-Null
$cnt++
}
}
$reportName = "C:\Users\Administrator\Downloads\VReport\VMInventory_$(Get-Date -Format 'yyyy_MM_dd').csv"
$Report | Export-Csv -Path $reportName -NoTypeInformation -Delimiter ";"
((Get-Content -Path $reportName |
ForEach-Object -Process {
$_ -replace '"', '' })) |
Out-File -FilePath $reportName -Force -Encoding ascii
Result: (the result is filtered)

see the first row Creator is not in the right cell, instead of user is VMWare Perl SDK 6.7.
and second VM has no creator because it was deployed as i saw in the Vsphere (events and Tasks)
can you please tell me what would be the cause?