I was using the VMware VI Toolkit for Windows for a change and I needed to load VMs from a CSV and my typical stream oriented way of doing this was very inefficient over the WAN (or LAN really). There are many ways of making Get-VM operations faster, but for this change I decided to use the standard Get-VM cmdlet with an array of computer names instead of doing them one by one.
Below is the function I wrote to make this easier for repeated use. The csv should have the computer name under a ‘Name’ column heading. All other columns are ignored.
Example file
Field1,Name,Field3,FieldN
“”,”testvm01″,”",”"
“”,”testvm02″,”",”"
1
2
3
4
5
6
7
8
9
10
| function LoadVMs-FromFile
{
param
(
$file = $(throw "A file must be specified.")
)
$vmNames = @()
Import-Csv $file | % {$vmNames += $_.Name}
Get-VM $vmNames
} |