VMware Cloud Community
ben1985
Contributor
Contributor

Loop through CSV file

Hi,

I have 5 VMs within a folder in vCenter which I would like to make some changes to. I have these VMs stored in an array called $vms. I also have a CSV file stored in $csvImport which is in the format: VM,NICNumber,NICType,MACAddress

The name values of the $vms are not the same as the name values in $csvImport.VM but will contain part of name. Ie. $vms = Prod - server1, $csvImport = server1

To make things a little more complicated, the CSV file does not hold as many VMs that are stored in $vms.

What I would like to do is loop through $csvImport.VM one by one and check if part of the name matches $vms. If it does, I want to make some changes to the VM, otherwise move onto the next in the CSV.

Reply
0 Kudos
6 Replies
LucD
Leadership
Leadership

If I understood the question correctly, you could try something like this.

It takes the name from the CSV, places meta-characters before and after and then does a Get-VM with that name string.

foreach($vm in Import-Csv -Path .\import.csv -UseCulture){

    # Fetch all VMs that match the name from the CSV

    Get-VM -Name "*$($vm.VM)*" | %{

        # Do you stuff

    }

}

An alternative, which can give you much finer control thanks to RegEx expressions and which should be faster due to less Get-VM cmdlets, would be to use a Where-clause with a match operator.

$vms = Get-VM

foreach($vm in Import-Csv -Path .\import.csv -UseCulture){

    # Fetch all VMs that match the name from the CSV

    $vms | where{$_.Name -match $vm.VM} | %{

        # Do you stuff

    }

}


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

Reply
0 Kudos
ben1985
Contributor
Contributor

Thanks! I tried the top example which is perfect for what I need! Inside the "# Do your stuff" though I would like to run a Set-NetworkAdapter but reference the actual names of the VMs, not the ones from the CSV. How would I do this?

Reply
0 Kudos
LucD
Leadership
Leadership

Try something like this.

The pipeline variable ($_) will contain 1 of the VMs returned by the Get-VM cmdlet.

foreach($vm in Import-Csv -Path .\import.csv -UseCulture){

    # Fetch all VMs that match the name from the CSV

    Get-VM -Name "*$($vm.VM)*" | %{

        Get-NetworkAdapter -VM $_ |

        Set-NetworkAdapter -StartConnected:$true

    }

}


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

Reply
0 Kudos
ben1985
Contributor
Contributor

Awesome! that should work for me. One last thing, as I loop through I would like to be able to use the contents of $vm later on to display some results. However once the code has run I'm only left with the contents of the last VM in the loop. Is there a way for me to save all VMs that were looped through?

Reply
0 Kudos
VinithMenon
VMware Employee
VMware Employee

You can create a small array before the foreach loop gived by @lucd and add the value from $vm to the array.

$vmarray = @()

foreach (...)

{

$vmarray += $vm

}

$vmarray

Reply
0 Kudos
ben1985
Contributor
Contributor

Awesome thanks!

Reply
0 Kudos