Hi,
I am unable to get the vCenter details from the below, please help
Get-VIEvent -MaxSamples ([int]::MaxValue) -Start (Get-Date).AddDays(-7) | Where-Object {$_ -is [VMware.Vim.VmCreatedEvent] -or $_ -is [VMware.Vim.VmRemovedEvent]} | Select | Sort -Property CreatedTime -Descending | Select @{N='vCenter';E={([uri]$_.ExtensionData.Client.ServiceUrl).Host}}, @{N='VM Name';E={$_.VM.Name}}, CreatedTime,UserName,FullformattedMessage
Like I said earlier, use the Server parameter
$vCenters = "vCenter1","vCenter2"
Connect-viserver -Server $vCenters
$global:defaultviservers |
ForEach-Object -Process {
$vc = $_
Get-VIEvent -MaxSamples ([int]::MaxValue) -Start (Get-Date).AddDays(-1) -Server $vc |
Where-Object {$_ -is [VMware.Vim.VmCreatedEvent] -or $_ -is [VMware.Vim.VmRemovedEvent]} |
Sort -Property CreatedTime -Descending |
Select @{N='vCenter';E={([System.Uri]$vc.ServiceUri.AbsoluteUri).Host}},
@{N='VM Name';E={$_.VM.Name}},
CreatedTime,UserName,FullformattedMessage
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Ciao
I tried your script and in my infrastructure, it seems to work (i deleted a test VM) and it detects deletion
what's the problem?
I am unable to get the vCenter Name in the output
@{N='vCenter';E={([uri]$_.ExtensionData.Client.ServiceUrl).Host} shows blank
Even in your output, vCenter Name shows blank 🙂
An Event object does not have an ExtensionData property.
The only way to get the vCenter in there is to launch the Get-VIEvent when you are connected to 1 vCenter, or by using the Server parameter.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Ciao
Try this
Get-VIEvent -MaxSamples ([int]::MaxValue) -Start (Get-Date).AddDays(-1) | Where-Object {$_ -is [VMware.Vim.VmCreatedEvent] -or $_ -is [VMware.Vim.VmRemovedEvent]} | Select | Sort -Property CreatedTime -Descending | Select @{N='vCenter';E={([System.Uri]$global:defaultviserver.ServiceUri.AbsoluteUri).Host}}, @{N='VM Name';E={$_.VM.Name}}, CreatedTime,UserName,FullformattedMessage
Hi Fabio,
I am able to get the vCenter Name now. 🙂 But If I have connected to multiple vCenters then it is not providing the correct vCenter name.
$vCenters = @(
"vCenter1"
"vCenter2"
)
Connect-viserver -Server $vCenters
Both VMs shows the same vCenter Name as vCenter2 rather than respective one`s
Like I said earlier, use the Server parameter
$vCenters = "vCenter1","vCenter2"
Connect-viserver -Server $vCenters
$global:defaultviservers |
ForEach-Object -Process {
$vc = $_
Get-VIEvent -MaxSamples ([int]::MaxValue) -Start (Get-Date).AddDays(-1) -Server $vc |
Where-Object {$_ -is [VMware.Vim.VmCreatedEvent] -or $_ -is [VMware.Vim.VmRemovedEvent]} |
Sort -Property CreatedTime -Descending |
Select @{N='vCenter';E={([System.Uri]$vc.ServiceUri.AbsoluteUri).Host}},
@{N='VM Name';E={$_.VM.Name}},
CreatedTime,UserName,FullformattedMessage
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you very much LucD 🙂