Thursday, May 17, 2012

Automate VM deployment from templates

The title is a little pretentious, but the next piece of PowerCLI does the job fairly enough. The only thing I have to do is to have the templates ready and modify the input file.
The script takes as input a csv file, parses each row and initializes variables that are used by New-Vm powercli command:

$input = Import-Csv "deploy_from_template.csv"
foreach ($row in $file) {
$vmname = $row.VmName
$respool = $row.ResPool
$location = $row.Location
$datastore = $row.Datastore
$template = $row.Template
echo "deploying " $vmname
New-Vm -Name $vmname -ResourcePool $respool -Location $location -Datastore $datastore -Template $template -RunAsync
}

RunAsync starts allows the execution of the next line in the script without waiting for current task to end. So use it wisely.

The csv file has the following structure, but you can put any other parameters in it and modify the script accordingly:
VmName,ResPool,Location,Datastore,Template
websrv01,Pool_Web,Client1,DS001,Template_RHEL6_Web
dbsrv01,Pool_DB,Client1,DS005,Template_RHEL5.5_DB

No comments: