Using VMware PowerCLI (#003) – Deploy Template

This script will help users/admins for deploying multiple VMs from one of available templates and unlike other scripts I saw so far which are file (answer file) based, this script is interactive and need and interaction of admin.
In next version I’d try to add some more options like “adding to resource pool or vApp”, adding custom description and …; for this script you can simply copy/paste it to new “.ps1” file or download below file and for running this script you need to have “VMware vSphere PowerCLI” on your system.

Feel free to send your comment and idea and if you find any bug/problem in this script please let me know.

[wpdm_file id=5]

#####################################################
Write-Host &quot;Name:<code>tDeploy Template Script
By:</code>tSohrab Kasraeian Fard (@Kasraeian)
Ver:`t0.1&quot;
#####################################################

<h1>Enabling multiple connection</h1>

$PCC = get-PowerCLIConfiguration
if ($test.DefaultVIServerMode -eq &quot;single&quot;) {
    Set-PowerCLIConfiguration -DefaultVIServerMode Multiple
}

<h1>vCenter Server Connection</h1>

Write-Host &quot;&quot;
$Target = Read-Host (&quot;Which vCenter Do you want&quot;)
if ($global:DefaultVIServer.Name -eq $Target) {
    Write-Host &quot;You already connected!&quot;
    $CRMB = &quot;Yes&quot;
} else {
    $VICSI = Get-VICredentialStoreItem | Sort-Object Host
    $AI = &quot;No&quot;
    foreach ($item in $VICSI) {
        if ($Target -eq $item.Host) {
            $AI = &quot;Yes&quot;
        }
    }
    if ($AI -eq &quot;No&quot;) {
        $cred = Get-Credential
        $NCR = Connect-VIServer -Server $Target -Credential $cred
    } else {
        $NCR = Connect-VIServer -Server $Target
    }
    if ($NCR.IsConnected -eq $true) {
        $CRMB = &quot;Yes&quot;
    } else {
        $CRMB = &quot;No&quot;
    }
}

function MainBody {
    # Template Selection
    Write-Host &quot;&quot;
    $Templates = Get-Template | Select Name | Sort-Object Name
    $i = 1
    $Templates | %{Write-Host $i&quot;:&quot; $_.Name; $i++}
    $SIndex = Read-Host &quot;Enter a number ( 1 -&quot; $Templates.Count &quot;)&quot;
    $STemplate = $Templates[$SIndex - 1].Name
    Write-Host &quot;You have selected&quot; $STemplate

<pre><code># Customization Profile Selection
Write-Host &amp;quot;&amp;quot;
$OSCPs = Get-OSCustomizationSpec | Select Name | Sort-Object
$j = 1
$OSCPs | %{Write-Host $j&amp;quot;:&amp;quot; $_.Name; $j++}
Write-Host $j&amp;quot;: None&amp;quot;
$OSIndex = Read-Host &amp;quot;Enter a number ( 1 -&amp;quot; $OSCPs.Count &amp;quot;)&amp;quot;
$SOSCP = $OSCPs[$OSIndex - 1].Name
if ($OSIndex -eq $j) {
    Write-Host &amp;quot;You didn't select any OS Customization Profile&amp;quot;
    $SOSCP = &amp;quot;None&amp;quot;
} else {
    Write-Host &amp;quot;You have selected&amp;quot; $SOSCP
}

# Host Selection
Write-Host &amp;quot;&amp;quot;
$VMHosts = Get-VMHost | Select Name | Sort-Object
$x = 1
$VMHosts | %{Write-Host $x&amp;quot;:&amp;quot; $_.Name; $x++}
$VMHIndex = Read-Host &amp;quot;Enter a number ( 1 -&amp;quot; $VMHosts.Count &amp;quot;)&amp;quot;
$SVMHost = $VMHosts[$VMHIndex - 1].Name
Write-Host &amp;quot;You have selected&amp;quot; $SVMHost

# Datastore Selection
write-Host &amp;quot;&amp;quot;
$Datastores = Get-VMHost $SVMHost | Get-Datastore | Select Name,FreeSpaceGB,CapacityGB | Sort-Object CapacityGB
$y = 1
$Datastores | %{Write-Host $y&amp;quot;:&amp;quot; $_.Name&amp;quot;`t`tCapacity (GB):&amp;quot; $_.CapacityGB&amp;quot;`t`tFree Space (GB):&amp;quot; $_.FreeSpaceGB; $y++}
$DSIndex = Read-Host &amp;quot;Enter a number ( 1 -&amp;quot; $Datastores.count &amp;quot;)&amp;quot;
$SDatastore = $Datastores[$DSIndex - 1].Name
Write-Host &amp;quot;You have selected&amp;quot; $SDatastore

# Name Selection
Write-Host &amp;quot;&amp;quot;
$SName = Read-Host &amp;quot;Please enter VMs Naming Patern&amp;quot;

# Getting VM count
Write-Host &amp;quot;&amp;quot;
$NVMTD = Read-Host &amp;quot;How many VMs do you want to deploy&amp;quot;

# Getting confirmation and deploying
Write-Host &amp;quot;`nPlease confirm if you want to deploy $NVMTD by bellow settings
Source Template: $STemplate
Target Host: $SVMHost
Target Datastore: $SDatastore
OS Customization Profile: $SOSCP
VM naming pattern: $SName&amp;quot;

$NVMDConf = Read-Host &amp;quot;It's confirmed (Y/N)&amp;quot;
if ($NVMDConf -eq &amp;quot;Y&amp;quot;) {
    if ($SOSCP -eq &amp;quot;None&amp;quot;) {
        $NVMs = 1..$NVMTD | foreach { New-VM -VMHost $SVMHost -Name $SName$_ -Template $STemplate -Datastore $SDatastore }
    } else {
        $NVMs = 1..$NVMTD | foreach { New-VM -VMHost $SVMHost -Name $SName$_ -Template $STemplate -Datastore $SDatastore -OSCustomizationSpec $SOSCP }
    }
    Write-Host &amp;quot;`n----- Created VMs -----&amp;quot;
    Get-VM | Where {$_.Name -like &amp;quot;$SName*&amp;quot;} | Select Name,NumCPU,MemoryMB | Format-Table -AutoSize
} else {
    Write-Host &amp;quot;All process aborted by user!&amp;quot;
}

# Power On Option
Write-Host &amp;quot;&amp;quot;
$SPOO = Read-Host &amp;quot;Do you want to power on all created VMs (Y/N)&amp;quot;
if ($SPOO -eq &amp;quot;Y&amp;quot;) {
    $SVMs = Start-VM -VM $NVMs
    Write-Host &amp;quot;`n----- Powered On VMs -----&amp;quot;
    Get-VM | Where {$_.Name -like &amp;quot;$SName*&amp;quot;} | Select Name,NumCPU,MemoryMB,PowerState | Format-Table -AutoSize
} else {
    Write-Host &amp;quot;You need to manually start your VMs.&amp;quot;
}
</code></pre>

}

if ($CRMB -eq &quot;Yes&quot;) {
    MainBody{}
} else {
    Write-Host &quot;Error on connecting to vCenter Server&quot;
}

Resources I’ve used so far for creation of this script:
1. Community answer from LUC Dekens@LUCD22 )
2. Comment from Alan Renouf@alanrenouf )

1 Comment

Add a Comment

It's your kindness to leave a reply/feedback