Hi all
I'm having some difficulty with a CSV file and scripting the creation of multiple VM's, and was wondering if anyone could help please?
I have a CSV with the following headers:
Server Name,ESX Host,Datastore,NumCPU,MemoryGB,DiskGB,Network Name,Disk Storage Format,GuestID
Test-SVR10,192.168.1.50,ds_01,1,4,40,VM Network,Thin,winnetstandardguest
What I need to add is network adaptor type (I have a mix of vmxnet3 and e1000 to deploy) and multiple disks per VM.
The script I have is:
import-csv "c:\scripts\vm-list.csv" -useculture | %{
new-vm -name $_."Server Name" -VMHost $_."ESX Host" -Datastore $_.Datastore -NumCPU $_.NumCPU -MemoryGB $_.MemoryGB -DiskGB $_."DiskGB" -NetworkName $_."Network Name" -DiskStorageFormat $_."Disk Storage Format" -GuestID $_.GuestID
}
If I was typing this one line at a time |(or using a batch file I suspect) I could use -DiskGB 10,20,30 to add multiple disks to the VM, but I can't seem to add it into the CSV, and the error message mentions something about -parameter which I don't fully understand.
I'm guessing its a case of getting the correct column headers for the information I need to add. Is there a list of CSV headings somewhere?
It might be worth noting that I am simply providing the shell of the VM's being deployed - there is no need to use a customization script or clone an existing VM / deploy from an existing template.
Cheers.
The New-VM cmdlet can handle an array of values on the DiskGB parameter.
Not sure how you defined the multiple disk sizes in the CSV file, but if you placed them in the one column, it would be read as a String.
But you can convert that string back to an array.
For example:
if you have entered "10-20-30" in the DiskGB column in the CSV file, you can do $_.DiskGB.Split('-') to get an array with the 3 values.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Just to clarify - the script works as it is, but I can't add the information to each VM as I need to.
If I try and add something for multiple disks for example, one for the error messages would be "Input string was not in the correct format".
The New-VM cmdlet can handle an array of values on the DiskGB parameter.
Not sure how you defined the multiple disk sizes in the CSV file, but if you placed them in the one column, it would be read as a String.
But you can convert that string back to an array.
For example:
if you have entered "10-20-30" in the DiskGB column in the CSV file, you can do $_.DiskGB.Split('-') to get an array with the 3 values.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD
Thanks! Worked a treat for multiple disks. Appreciate the help.
With the networking adaptors in mind, is there a way to add to the CSV multiple vNIC's and set each network label differently too?
Paul
I've figured out the version and how to add CD and Floppy to every VM. Struggling with multiple vNIC's at the moment.
I've added a second line into the script for new-networkadapter and various switches which I can also put in multiple times and it works, but that will add the same amount of vNIC's to each VM that is created. Is there a simpler way to add a column to my CSV for network adapters, and then use the .split('.') again perhaps?
You can add multiple vNICs on the New-VM cmdlet, the PortGroup parameter accepts an array of names (similar to the DiskGB parameter).
Changing the default vNIC type will need to be done via the Set-NetworkAdapter cmdlet after the VM is created and with the Type parameter (see Example 2)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
So in theory I could add -PortGroup $_.PortGroup.Split('-') to the script, head a column up with PortGroup and a value in one of the cells underneath being "VM Network 1-VM Network 2" ?
Yes, but note that this only allows you to set the portgroup, not the vNIC type
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks for all the help. I went with this in my script in the end:
-NetworkName $_."Network Name".Split('-')
The CSV column is headed with Network Name, and each of the VM's being created has the following in the relevant cell:
"Network1-VNetwork2" (assuming I had two networks I wanted to connect the VM to.
My script changes all vNIC's to VMXNET3 adaptors too.
In case this helps anyone in the future, my CSV headings are:
Server Name,ResourcePool,Datastore,NumCPU,MemoryGB,DiskGB,"Network Name",Disk Storage Format,GuestID,Version
Test-SVR01,192.168.1.50,ds_01,1,2,"10-1","VM Network-VMNetwork2",Thin,winnetstandardguest,v7
The above would then create a VM called Test-SVR01, on the ESXi host x.x.1.50, with a 10 GB and a 1GB thin provisioned disk, attached to the named networks (two separate networks), and set the VM version to 7.
The script itself is:
import-csv "c:\scripts\vm-list.csv" -useculture | %{
new-vm -name $_."Server Name" -ResourcePool $_."ResourcePool" -Datastore $_.Datastore -NumCPU $_.NumCPU -MemoryGB $_.MemoryGB -DiskGB $_.DiskGB.Split('-') -NetworkName $_."Network Name".Split('-') -DiskStorageFormat $_."Disk Storage Format" -GuestID $_.GuestID -Version $_.Version -CD -Floppy
get-vm $_."Server Name" | get-networkadapter | set-networkadapter -type vmxnet3 -wakeonlan:$true -confirm:$false
}
Thanks to the community and LucD for helping.
Thanks for sharing
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference