Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I am new to Powershell and Powercli... but i manager few ESX environments and really would like to do as much as possible via powershell. I am trying to do as much as i can via Powershell. On with the help I need:

I used this one liner to create VMs from templates. But the problem is there has to be some user input after each new VM is created. New-VM name -Template template -VMHost VMHost -Datastore Datastore

What i would like to do is be able to import via CSV the name of the new vm, the template to use, the host to put the new vm and the datastore all from a CSV. I don't know if it is as easy as below, but i kept getting errors.

Import-Csv "C:\powershell\Data\VM2Create.csv" | Foreach-object{
    New-VM $_.name -Template $_.template -VMHost $_.VMHost -Datastore $_.Datastore
}

I know there some () or {} or possibly | that need... just don't know where to put them...

The csv i think would look like this:

name, template, vmhost, datastore

Any help or thoughts would be much appreciated...

share|improve this question
    
@EdConde - it looks like the -Name specifier is missing for New-VM which could cause problems (I don't have the VMware powershell tools, and can't check if this is the proper position for the name) Other than that this looks pretty good - if this isn't the issue, can you post a sample CSV and whatever errors you are getting. –  MattB Mar 17 '10 at 14:45

2 Answers 2

This worked for me:

param ( $CsvFile = "D:\foo\bar.csv" )

$worksheet = Import-Csv $CsvFile
$worksheet |ForEach-Object {
    New-VM -VMHost $_.VMHost `
        -Name $_.Name `
        -MemoryMB $_.MemoryMB `
        -DiskMB $_.DiskMB `
        -NetworkName $_.NetworkName `
        -Datastore $_.Datastore `
        -CD:$FALSE `
        -ResourcePool $_.ResourcePool `
        -Description $_.Description `
        -GuestId $_.GuestId `
        -NumCPU $_.NumCPU
}
share|improve this answer

Check out my script here- http://poshcode.org/2051.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.