I’ve been playing around with powershell yesterday. We needed to create over 100 VM’s and there’s no point in doing that all by hand. The customer provided us with a .csv file that contained specific info on these VM’s. It took me a while to figure out how to read the info.csv file and how to actually use it. But as always it’s actually fairly simple and that’s why I decided to write it down:
Before we even start, the CSV should be formatted as follows:
vmname,cluster
VM001,HA-DRS-Yellow-Bricks
Read the complete csv file into a variable:
$csv_info = Import-Csv c:\scripts\info.csv
For every line in the csv variable do something:
foreach ($line in $csv_info) {
write-host " This is virtual machine $($line.vmname) on cluster $($line.cluster)"
}
This line would print something like this:
This is virtual machine VM001 on cluster HA-DRS-Yellow-Bricks
As you noticed in the example above we used $line.vmname to get the name of the VM printed and $line.cluster for the cluster name, cool huh! BTW, Alan’s quick reference guide really helped me out!