вторник, 29 мая 2012 г.

Создание виртуальных машин

Скрипт автоматического создания виртуальных машин


#Usage: powershell.exe ./CreateVM.ps1 ./machines.csv

# include VmWare
add-pssnapin VMware*

# get file name with VMs content
$InputFileName = $args[0]

#storage maximums
$storageMaximum = '0.2'

# vCenter name
$vCenter='192.168.0.2'

# import content
$InputFile = Import-Csv $InputFileName

echo 'Connect to vCenter'
$vccred = Get-VICredentialStoreItem -host $vCenter -file 'c:\vmware\credentials.xml'
Connect-VIServer $vCenter -User $vccred.user -Password $vccred.Password
echo '------------------------------------'

echo 'Create VMs'
foreach ($item in $inputFile) {
    echo 'current item'
    echo $item
    echo '------------------------------------'

    echo 'parser of item'
    echo $item.folder
    echo $item.name
    echo "CPU: "$item.cpu
    echo "RAM: "$item.ram
    echo $item.template
    echo '------------------------------------'
 
# create new folder for current VM
    $VMFolder = '0'
    $VMFolder = Get-Folder $item.folder
    if ($VMFolder.Name -eq $item.folder) {
        echo 'folder already exists'
    }
    else {
        echo 'create folder'
        $VMFolder = Get-Folder 'Export URG' | New-Folder -Name $item.folder
    }
    echo $VMFolder
    echo '------------------------------------'
 
# get template for current VM
    $template = '0'
    $template = Get-Template -Name $item.template
    echo $template
 
# get specification for current VM
    $specification = '0'
    $specification = Get-OSCustomizationSpec -Name $item.specification
    echo $specification
 
# get datastore with maximum free space
    $bad = 0
    $datastore = 0
    $stores = Get-DataStore
    foreach ($store in $stores) {
        if ($store.FreeSpaceMB -gt $bad) {
            $bad = $store.FreeSpaceMB
            $datastore = $store
        }
    }
# get datastore with minimum RAM usage
    $bad = 1000000000000000000000
    $esxihost = 0
    $hosts = Get-DataStore -name $datastore.name | get-vmhost
    foreach ($hosti in $hosts) {
        if ($hosti.MemoryUsageMB -lt $bad) {
            $bad = $hosti.MemoryUsageMB
            $esxihost = $hosti
        }
    }

echo 'choosed esxi host (name)'$esxihost.name
echo 'choosed datastore (name)'$datastore.name
     
    if (-not($datastore -eq '0')) {
    if (($datastore.freespacemb / $datastore.capacitymb) -gt $storageMaximum) {

# create new VM
    if (-not($item.ram -eq 0)) {
        if (-not($item.cpu -eq 0)) {
            New-VM -Name $item.name -Template $template -VMHost $esxihost -Datastore $datastore -OSCustomizationSpec $specification -location $VMFolder -MemoryMB $item.ram -NumCPU $item.cpu
        }
        else {
            New-VM -Name $item.name -Template $template -VMHost $esxihost -Datastore $datastore -OSCustomizationSpec $specification -location $VMFolder -MemoryMB $item.ram
        }
    }
    else {
        if (-not($item.cpu -eq 0)) {
            New-VM -Name $item.name -Template $template -VMHost $esxihost -Datastore $datastore -OSCustomizationSpec $specification -location $VMFolder -NumCPU $item.cpu
        }
        else {
            New-VM -Name $item.name -Template $template -VMHost $esxihost -Datastore $datastore -OSCustomizationSpec $specification -location $VMFolder
        }
    }
}
    }
}

Disconnect-VIServer -server $vCenter -Confirm:$false

Пример файла-таблицы к скрипту

folder,name,cpu,ram,template,specification,theend
foldername1,vmName1,1,4,temp1*,temp1,theend
foldername2,vmName2,2,4,temp2*,temp2,theend 
foldername2,vmName3,2,4,temp2*,temp2,theend 
foldername3,vmName4,4,4,temp3*,temp3,theend 

Комментариев нет: