VMware Cloud Community
avagham
Enthusiast
Enthusiast
Jump to solution

Need to get Vcenter info

Hello - I'm trying to get vcenter name as i'm running below script across multiple vcenter.

Command: $csvline.vcenter = $event.VC.name not giving me vcenter name. please advise.

Script:

Import-Module VMware.VimAutomation.Core

Connect-VIServer vcenter

$report = @()

$events = Get-VIEvent -maxsamples 10000 -Start (Get-Date).AddDays(–5) | where {$_.Gettype().Name-eq "VmCreatedEvent" -or $_.Gettype().Name-eq "VmBeingClonedEvent" -or $_.Gettype().Name-eq "VmBeingDeployedEvent" }

foreach ($event in $events)

{

$csvline = "" | Select EventTime, Datacenter, Cluster, VM, Description, vcenter

$csvline.EventTime = $event.CreatedTime

$csvline.Datacenter = $event.Datacenter.Name

$csvline.Cluster = $event.ComputeResource.Name

$csvline.VM = $event.VM.Name

$csvline.Description = $event.FullFormattedMessage

$csvline.vcenter = $event.VC.name

$report += $csvline

}

$report | Export-csv -NoTypeInformation E:\output_file.csv

Thank you!!

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try with this line

$csvline.vcenter = ([uri](Get-View -Id $event.VM.VM).Client.ServiceUrl).Host


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

8 Replies
LucD
Leadership
Leadership
Jump to solution

Try with the follwoing.
Note that this only works when the entity on which the event was fired still exist.

$csvline.vcenter = ([uri](Get-View -Id $event.Entity.Entity -Property Client).Client.ServiceUrl).Host


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

avagham
Enthusiast
Enthusiast
Jump to solution

Thank you LucD for your quick response.

I'm getting below error:

Get-View : Cannot validate argument on parameter 'Id'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

At line:15 char:40

+ $csvline.vcenter = ([uri](Get-View -id $_.Entity.Entity -Property Cli ...

+                                        ~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidData: (:) [Get-View], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You are mixing my reply from two threads.
In this thread it is $event.Entity.Entity.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

avagham
Enthusiast
Enthusiast
Jump to solution

oh sorry, i was trying both commands. I'm still getting same error:

Get-View : Cannot validate argument on parameter 'Id'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.

At line:15 char:40

+ $csvline.vcenter = ([uri](Get-View -Id $event.Entity.Entity -Property ...

+                                        ~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidData: (:) [Get-View], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView

Script I'm running:

Import-Module VMware.VimAutomation.Core

Connect-VIServer vcenter1, vcenter2, vcenter3

$report = @()

$events = Get-VIEvent -maxsamples 10000 -Start (Get-Date).AddDays(–5) | where {$_.Gettype().Name-eq "VmCreatedEvent" -or $_.Gettype().Name-eq "VmBeingClonedEvent" -or $_.Gettype().Name-eq "VmBeingDeployedEvent" }

foreach ($event in $events)

{

$csvline = "" | Select EventTime, Datacenter, Cluster, VM, Description, vcenter

$csvline.EventTime = $event.CreatedTime

$csvline.Datacenter = $event.Datacenter.Name

$csvline.Cluster = $event.ComputeResource.Name

$csvline.VM = $event.VM.Name

$csvline.Description = $event.FullFormattedMessage

$csvline.vcenter = ([uri](Get-View -Id $event.Entity.Entity -Property Client).Client.ServiceUrl).Host

$report += $csvline

}

$report | Export-csv -NoTypeInformation E:\output_file.csv

Thank you!!!

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Try with this line

$csvline.vcenter = ([uri](Get-View -Id $event.VM.VM).Client.ServiceUrl).Host


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

avagham
Enthusiast
Enthusiast
Jump to solution

Thank much LucD, this command is working fine.

However, for few VMs it is giving below error, any idea why i'm getting below error? I tried to find "vm-258333" and it is running in one of vCenter.

Get-View : 9/23/2020 2:13:48 PM Get-View View with Id  'VirtualMachine-vm-258333' was not found on the server(s).

At line:15 char:27

+ $csvline.vcenter = ([uri](Get-View -Id $event.VM.VM).Client.ServiceUr ...

+                           ~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : ObjectNotFound: (:) [Get-View], VimException

    + FullyQualifiedErrorId : Core_GetView_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.DotNetInterop.GetVIView

0 Kudos
LucD
Leadership
Leadership
Jump to solution

That is what I meant with my earlier remark.
When that VM in the meantime doesn't exist anymore, you will see this error.

There are 2 options:

- you ignore the error by adding -ErrorAction SilentlyContinue on the Get-View cmdlet

- you use another property, for example Datacenter, and hope that that one still exists when running the script


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

avagham
Enthusiast
Enthusiast
Jump to solution

I understood your point. I will choose option 1 and continue to run the script.

Thanks for your help as always!!

0 Kudos