Automation

 View Only
  • 1.  Loop through CSV file

    Posted Jun 10, 2015 01:33 AM

    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.



  • 2.  RE: Loop through CSV file

    Posted Jun 10, 2015 09:11 AM

    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

        }

    }



  • 3.  RE: Loop through CSV file

    Posted Jun 11, 2015 12:23 AM

    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?



  • 4.  RE: Loop through CSV file

    Posted Jun 11, 2015 02:01 PM

    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

        }

    }



  • 5.  RE: Loop through CSV file

    Posted Jun 11, 2015 10:45 PM

    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?



  • 6.  RE: Loop through CSV file

    Broadcom Employee
    Posted Jun 12, 2015 03:01 AM

    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



  • 7.  RE: Loop through CSV file

    Posted Jun 14, 2015 08:35 PM

    Awesome thanks!