Javascript required
Skip to content Skip to sidebar Skip to footer

Powercli Script Read Variables From File Vmware

Past Jitendra | Jun 7, 2018 | In Articles | Update: Jul 5, 2018 | Total Views [ 5014 ]

Powercli script to deploy bulk of VMs with customized configuration.

Promise this will be helpful.

Use the script on powerclie x86 module only.

There are few pre requisites as below:

1. Use x86 powercli modume simply.

two. Create a csv file as mentioned beneath.

three. Create bone customization contour in Vcenter server. (Refer the commodity for creating os customization profile in Vcenter)

4. Run the script with appropriate user rights on Vcenter.

Observe the beneath powerclie script code.

body

<#
.SYNOPSIS
   Script automates deployment of multiple vms loaded from pre-defined .csv file
.DESCRIPTION
   Script reads the .csv file, deploys vms from template, applies os customization spec then attempts to set upward IP configuration
.PARAMETER
   none
.EXAMPLE
   trivia
#>

 #variables
$ScriptRoot = Split-Path $MyInvocation.MyCommand.Path
$csvfile = "$ScriptRoot\vms.csv"
$vcenter_srv = 'vmwarediary.com'
$timeout = 180
$loop_control = 0

 $vmsnapin = Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue
$Error.Clear()
if ($vmsnapin -eq $null)
    {
    Add together-PSSnapin VMware.VimAutomation.Core
    if ($mistake.Count -eq 0)
        {
        write-host "PowerCLI VimAutomation.Cadre Snap-in was successfully enabled." -ForegroundColor Green
        }
    else
        {
        write-host "ERROR: Could not enable PowerCLI VimAutomation.Core Snap-in, exiting script" -ForegroundColor Red
        Go out
        }
    }
else
    {
    Write-Host "PowerCLI VimAutomation.Core Snap-in is already enabled" -ForegroundColor Green
    }

 # 32-bit PowerCLI is required to run *-OSCustomizationSpec cmdlets
if ($env:Processor_Architecture -eq "x86") {

     #Connect to vCenter
    Connect-VIServer -Server $vcenter_srv

     $vms2deploy = Import-Csv -Path $csvfile

        #deploy vms as per data in each line, look for customization (a reboot) then wait for vmware tools to alter ip settings
    foreach ($vm in $vms2deploy) {

                #validate input, at least vm proper name, template name and Bone Customization Spec name should be provided
        if (($vm.name -ne "") -and ($vm.template -ne "") -and ($vm.oscust -ne "")){

                        #check if vm with this name already exists (some funny results are produced once we deploy vm with duplicate name)
            if (!(get-vm $vm.Proper name -erroraction 0)){
                $vmhost = get-cluster $vm.cluster | get-vmhost -state continued | Get-Random

                                #check if we desire to attempt IP configuration, if "none" is written in .csv file (because nosotros use DHCP for instance) we deploy immediately, otherwise we insert IP into CustomizationSpec, and then deploy
                if ($vm.ip -match 'none'){
                    Write-Host "No IP configuration in .csv file, moving on" -ForegroundColor Yellow
                    write-Host "Deploying VM $($vm.name) to datastore cluster $.($vm.datastore-cluster)"
                       new-vm -proper noun $vm.proper noun -template $(get-template -name $vm.template) -vmhost $vmhost -oscustomizationspec $(get-oscustomizationspec -name $vm.oscust) -datastore $(get-datastorecluster -proper name $vm.datastore-cluster) -location $(get-binder -name $vm.folder) | Out-Aught
                }
                else {

                                        #clone the "master" Os Customization Spec, then use it to utilise vm specific IP configuration
                    $cloned_oscust = Get-OSCustomizationSpec $vm.oscust | New-OSCustomizationSpec -proper name "$($vm.oscust)_$($vm.name)"

                                         Set-OSCustomizationNicMapping -OSCustomizationNicMapping ($cloned_oscust | Get-OscustomizationNicMapping) -Position 1 -IpMode UseStaticIp -IpAddress $vm.ip -SubnetMask $vm.mask -DefaultGateway $vm.gw -Dns $vm.dns1,$vm.dns2 | Out-Null
                    write-Host "Deploying VM $($vm.proper noun) to datastore cluster $($vm.datastore)"
                       new-vm -proper noun $vm.name -template $(get-template -name $vm.template) -vmhost $vmhost -oscustomizationspec $cloned_oscust -datastore $(get-datastore -proper name $vm.datastore) -location $(become-folder -proper noun $vm.folder) | Out-Null
                }

                                #this is where we try to rail deployment progress
                $loop_control = 0
                write-host "Starting VM $($vm.name)"
                start-vm -vm $vm.name -confirm:$false | Out-Null

                 write-host "Waiting for outset boot of $($vm.name)" -ForegroundColor Yellow
                practice {
                    $toolsStatus = (Become-VM -name $vm.name).extensiondata.Guest.ToolsStatus
                    Start-Slumber 3
                    $loop_control++
                } until ( ($toolsStatus -match 'toolsOk') -or ($loop_control -gt $timeout) )

                 #look another infinitesimal "simply in case" feel free to remove this line
                Start-Sleep 60

                                #clean-upwards the cloned OS Customization spec
                Remove-OSCustomizationSpec -CustomizationSpec $cloned_oscust -Confirm:$false | Out-Zip

                                if ($loop_control -gt $timeout){
                    Write-Host "Deployment of $($vm.proper name) took more than $($timeout/20) minutes, check if everything OK" -ForegroundColor red
                }
                else {
                Write-Host "$($vm.proper noun) successfully deployed, moving on" -ForegroundColor Light-green
                }
            }
            else {
                Write-Host "$($vm.name) already exists, moving on" -ForegroundColor Red
            }
        }
        else {
            Write-Host "Cheque input file $csvfile for line with empty vm, template or Bone customization spec value" -ForegroundColor Ruby
        }
    }

     Write-Host "All vms deployed, exiting" -ForegroundColor Green
    #disconnect vCenter
    Disconnect-VIServer -Confirm:$false
}
else {
Write-Host "This script should be run from 32-fleck version of PowerCLI only, Open 32-bit PowerCLI window and offset once more" -ForegroundColor Carmine
}

Make the .ps1 file using above powercli script

Name,template,oscust,cluster,folder,datastore,ip,mask,gw,dns1,dns2

Create a csv file using above headers with value given for each header.

Share this

About the Author

Jitendra

User's Comments


Powercli Script Read Variables From File Vmware

Source: http://vmwarediary.com/Articles/2018/6/5800810/Powercli-script-to-deploy-bulk-of-VMs-with-customized-configuration