Hi Luc!
thank you for your reply.
yes I have seen these Properties and try to set conditions to get the target:
$start = (Get-Date).AddDays(-1)
$Events = Get-VIEvent -Start $start -MaxSamples 100
$EventOutput = @()
foreach ($event in $Events){
#creating objects
$tempObj = "" | Select-Object -Property Time, Entity, Message
#creating Property Time
$tempObj.Time = $event.CreatedTime
#creating Property Message
$tempObj.Message = $Event.FullFormattedMessage
#[VMware.Vim.Event].DeclaredProperties #for testing
$EntityEvents = [PSCustomObject]@{
NetEvent = $event.Net -ne $null
NotNetEvent = $event.Net -eq $null
DistributedVirtualSwitchEvent = $event.Dvs -ne $null
NotDistributedVirtualSwitchEvent = $event.Dvs -eq $null
DatacenterEvent = $event.Datacenter -ne $null
NotDatacenterEvent = $event.Datacenter -eq $null
DsEvent = $event.Ds -ne $null
NotDsEvent = $event.Ds -eq $null
ClusterEvent = $event.ComputeResource -ne $null
NotClusterEvent = $event.ComputeResource -eq $null
NotHostEvent = $event.Host -eq $null
HostEvent = $event.Host -ne $null
VmEvent = $event.Vm -ne $null
NotVmEvent = $event.Vm -eq $null
}
if($EntityEvents.HostEvent -and $EntityEvents.NotVmEvent){
$tempObj.Entity = $event.host.name
}elseif($EntityEvents.VmEvent){
try{
$Ipv4 = (Get-Vm -Name $event.VM.Name).ExtensionData.Guest.Ipaddress;
$tempObj.Entity = [System.Net.Dns]::GetHostByAddress($Ipv4).Hostname
}catch{
$tempObj.Entity = (Get-View -ViewType 'VirtualMachine' -Filter @{Name=$event.Vm.Name} -Property Guest).Guest.Hostname
}
}elseif($event.ObjectType -ne $null -and
$EntityEvents.DatacenterEvent -and
$EntityEvents.NotVmEvent ){
if($event.ObjectName -ne $null){$tempObj.Entity = $event.ObjectName}
}else{$tempObj.Entity = $global:DefaultVIServers.Name}
$EventOutput += $tempObj
}
$EventOutput | Ft -AutoSize
Is there a better way to do that?
Thank you!