VMware Cloud Community
ximpl1
Contributor
Contributor
Jump to solution

CSV File not working with Get-View

Hi All,

I am a bit new to the Get-View cmdlet.  Can someone help me in figuring out why the following lines of code are not giving me what I want?  The csv file has a limited number of VMs in it by name.  However, when the Get-View cmdlet is invoked against $vms variable I am getting the entire cluster of VM names assigned to the varible from the entire vCenter Server.

These are the lines of code I am using.  What am I doing wrong or missing?

Plz help!

The $vms variable is not getting assigned the information from the $vm variable properly.

$vm = Import-Csv C:\Scripts\throttledsvmotion.csv

$vms = Get-View -SearchRoot $vm.Extentiondata.MoRef -ViewType VirtualMachine | Export-Csv 'C:\Scripts\vmdata.csv'

Thanks!

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

What exactly do you have in C:\Scripts\throttledsvmotion.csv?
If that is a single column, named Name, with the names of the VMs, like this

Name
VM1
VM2
VM3

You could use the Filter parameter.

The right-hand operator of each entry in the Filter hash table is a RegEx expression.
We construct that RegEx expression with the OR operator (|).

It looks like this: 'VM1|VM2|VM3', meaning the name of the returned VMs should match any of the entries in that expression

Something like this

$filter = (Import-Csv -Path .\vmnames.csv -UseCulture).Name -join '|'

$vms = Get-View -ViewType VirtualMachine -Filter @{Name=$filter}


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

View solution in original post

4 Replies
LucD
Leadership
Leadership
Jump to solution

What exactly do you have in C:\Scripts\throttledsvmotion.csv?
If that is a single column, named Name, with the names of the VMs, like this

Name
VM1
VM2
VM3

You could use the Filter parameter.

The right-hand operator of each entry in the Filter hash table is a RegEx expression.
We construct that RegEx expression with the OR operator (|).

It looks like this: 'VM1|VM2|VM3', meaning the name of the returned VMs should match any of the entries in that expression

Something like this

$filter = (Import-Csv -Path .\vmnames.csv -UseCulture).Name -join '|'

$vms = Get-View -ViewType VirtualMachine -Filter @{Name=$filter}


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

ximpl1
Contributor
Contributor
Jump to solution

Ahhh, I see.  Yeah the csv is exactly like you layed out.  Header is actually vmname then each element follows as such.  So, the csv is as follwos vmname,vm1,vm2,vm3 ...

Let me give this a try and I will get back to you.

Thanks Luc!

Reply
0 Kudos
ximpl1
Contributor
Contributor
Jump to solution

I am still having an issue with the output that I am receiving.

I used your code in the following way.

$filter = (Import-Csv -Path C:\Scripts\throttledsvmotion.csv -UseCulture).Name -join '|'

$vms = Get-View -ViewType VirtualMachine -Filter @{Name=$filter} | Export-Csv 'C:\Scripts\vmsoutput.csv'

When I look into the output .csv file for $vms I am getting the following: Note: I removed the other output elements for ease of reading.  The one I need correct is "System.String[]" with the VM name from the source .csv file.

For the "System.String[]" I am getting this output:

#TYPE VMware.Vim.VirtualMachine

"vC65-01","System.String[]"

"AD-DNS-2016","System.String[]"

"OpenVPN","System.String[]"

"Ubuntu18-OpenVPN","System.String[]"

"PiHole_01","System.String[]"

"PiHole_02","System.String[]"

"VM3","System.String[]"

"VM4","System.String[]"

"VM5","System.String[]"

"VM6","System.String[]"

"VM7","System.String[]"

"VM8","System.String[]"

"VM9","System.String[]"

"VM10","System.String[]"

"VM3","System.String[]"

"VM3","System.String[]"

"VM3","System.String[]"

"VM3","System.String[]"

"VM3","System.String[]"

It needs to be this output for "system.String[]"

#TYPE VMware.Vim.VirtualMachine

"VM3","System.String[]"

"VM4","System.String[]"

"VM5","System.String[]"

"VM6","System.String[]"

"VM7","System.String[]"

"VM8","System.String[]"

"VM9","System.String[]"

"VM10","System.String[]"

"VM3","System.String[]"

"VM3","System.String[]"

"VM3","System.String[]"

"VM3","System.String[]"

"VM3","System.String[]"

Is there another way?

Thanks again LucD for your time!

Reply
0 Kudos
ximpl1
Contributor
Contributor
Jump to solution

Hi Luc,

Nevermind, I figured it out.  I re-read your reply as I mis-read it.  I renamed my header from vmname to Name as you did in your reply.  The -filter was not picking it up and was giving me everything as opposed to what was in my source .csv file.

So, it is working now!

You rock!

Thanks Luc!

Reply
0 Kudos