VMware Cloud Community
TheVMinator
Expert
Expert
Jump to solution

Converting output from one report to input for another report

I'm attempting to get a number of past events related to creation of VMs.  After getting all the data I can about those VMs from the event objects, I'm attempting to pipe the output into another section of the report that queries vCenter for each VM and gets current sizing information from the results of get-vm.

Get-VIEvent -maxsamples ([int]::MaxValue) -Start (Get-Date).AddDays(-8) -finish (Get-Date).adddays(-4) |

where {"VmCreatedEvent","VmClonedEvent","VmDeployedEvent" -contains $_.Gettype().Name} |

Select createdTime, userName,@{N="vmname";E={$_.VM.Name}},@{N="ESXiHost";E={$_.Host.Name}} |

%{get-vm -name $_.vmname} | select name, numcpu, memoryGB, provisionedSpaceGB, UsedSpaceGB, createdTime, username, ESXiHost | export-csv -path "c:\test.txt"

I need to tweak the part in orange to get it operational - so that I can take the properties that I created in the first part of the report, make them available in the second part of the report along with the new properties I'm adding.  How can I get all of these properties combined into my output file?

Thanks for help!

0 Kudos
1 Solution

Accepted Solutions
mattboren
Expert
Expert
Jump to solution

Hello, -

You should be able to get such info with just a bit of tweaking, like:

Get-VIEvent -MaxSamples ([int]::MaxValue) -Start (Get-Date).AddDays(-8) -Finish (Get-Date).AddDays(-4) |
Where-Object {"VmCreatedEvent","VmClonedEvent","VmDeployedEvent" -contains $_.Gettype().Name} | %{
   
$evtThisNewVMEvent = $_
   
Get-VM -Id $_.Vm.VM | Select Name, NumCPU, MemoryGB, ProvisionedSpaceGB, UsedSpaceGB, VMHost,
        @{n
="CreatedTime"; e={$evtThisNewVMEvent.createdTime}},
        @{n
="CreatedBy"; e={$evtThisNewVMEvent.UserName}},
        @{n
="OrigVMHost"; e={$_.VMHost.Name}}
}
## end foreach-object

This combines the select statements into one, and with the use of an additional variable, allows for getting the VM _and_ creation-event info.  I also added the "current" VMHost and differentiated that the VMHost from the event is the "OrigVMHost", just for fun.  How does that do for you?

View solution in original post

0 Kudos
4 Replies
mattboren
Expert
Expert
Jump to solution

Hello, -

You should be able to get such info with just a bit of tweaking, like:

Get-VIEvent -MaxSamples ([int]::MaxValue) -Start (Get-Date).AddDays(-8) -Finish (Get-Date).AddDays(-4) |
Where-Object {"VmCreatedEvent","VmClonedEvent","VmDeployedEvent" -contains $_.Gettype().Name} | %{
   
$evtThisNewVMEvent = $_
   
Get-VM -Id $_.Vm.VM | Select Name, NumCPU, MemoryGB, ProvisionedSpaceGB, UsedSpaceGB, VMHost,
        @{n
="CreatedTime"; e={$evtThisNewVMEvent.createdTime}},
        @{n
="CreatedBy"; e={$evtThisNewVMEvent.UserName}},
        @{n
="OrigVMHost"; e={$_.VMHost.Name}}
}
## end foreach-object

This combines the select statements into one, and with the use of an additional variable, allows for getting the VM _and_ creation-event info.  I also added the "current" VMHost and differentiated that the VMHost from the event is the "OrigVMHost", just for fun.  How does that do for you?

0 Kudos
TheVMinator
Expert
Expert
Jump to solution

OK great thanks.  Works great. Just out of curiosity, is there a reason why you used the -id parameter rather than the name of the VM?  Do they both work equally well?

If the name of the VM changes does the ID remain the same?

0 Kudos
mattboren
Expert
Expert
Jump to solution

Hello-

Good, glad to hear it.  As for "why use -id param?" -- speed and precision.

0)  speed:  retrieving a VM with Get-VM using an ID instead of a name is generally faster, especially as the environment grows larger

1)  precision:  there might be thirteen VMs of name "myFirstVM" sprinkled throughout the inventory of a vCenter -- there is only one of ID "VirtualMachine-vm-7547"

An illustration of the speed difference:  I retrieve the same VM by ID and by name with Get-VM in a vCenter with ~5,000 VMs.  It took 0.5 seconds by ID, and about 18 seconds by name.

On the topic of precision -- not that having multiple VMs of the same name should be / is common practice, but, duplicate VM names do happen.  So, "by ID" removes any concern about such things.  Of course, if you do not already have a VM's ID, it'll be a matter of getting it somehow, and likely "by name".  But, since your example already has that ID -- hurray!

And, yes -- a VM's ID does not change when changing the VM's name (or by any configuration change on the VM, AFAIK).  The ID persists as long as the object is in inventory, I believe.  So, if a VM is removed from inventory (not from disk) and then re-registered, it would get a new ID.  Otherwise, its ID is static.

TheVMinator
Expert
Expert
Jump to solution

OK great thanks

0 Kudos