Using VMware PowerCLI (#005) – Deploy Template (v0.3)

Thanks “nWill” for his kind comment on my previous post, I found a problem on “v0.2” and in “v0.3” I tried to fixed some problems previous script had such as not being able to work properly in environment with only one “Template, Customization Specification, Host, Resource Pool or vApp”.
I’ve added some description as well as defining some other condition but might still got some problem, so please let me know if you find any.  😉

[wpdm_file id=7]

#####################################################
function Header {
    CLS
    Write-Host "Name:`tDeploy Template Script (v0.3)"
}
#####################################################

<h1>Adding PSSnapin</h1>

$pssnap = Get-PSSnapin | Sort-Object Name
$APS = &quot;No&quot;
$PSIndex = 1
1..$pssnap.Count | foreach {
    if ($pssnap[$PSIndex].Name -like &quot;vmware.vimautomation.core&quot;) {
        $APS = &quot;Yes&quot;
    }
    $PSIndex++
}
if ($APS -eq &quot;No&quot;) { Add-PSSnapin VMware.VimAutomation.Core }

<h1>Enabling multiple connection</h1>

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

<h1>vCenter Server Connection</h1>

Header
Write-Host &quot;&quot;
$Target = Read-Host (&quot;Which vCenter Do you want connect to&quot;)
if ($global:DefaultVIServer.Name -eq $Target) {
    $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
    Header
    Write-Host &quot;&quot;
    $Templates = Get-Template | Select Name | Sort-Object Name
    if ($Templates.count -ge 2) {
        $i = 1
        $Templates | %{Write-Host $i&quot;:&quot; $<em>.Name; $i++}
        $SIndex = Read-Host &quot;Enter a number ( 1 -&quot; $Templates.Count &quot;)&quot;
        $STemplate = $Templates[$SIndex - 1].Name
    } elseif ($Templates.Name -notlike &quot;&quot;) {
        $i = 1
        $Templates | %{Write-Host $i&quot;:&quot; $</em>.Name; $i++}
        $TSMC = Read-Host (&quot;You only have one template, do you want to use it (Y/N) [default = Y]&quot;)
        Switch ($TSMC) {
            &quot;n&quot; {
                Write-Host (&quot;<code>nAll process aborted by user!</code>n&quot;) -BackgroundColor Yellow -ForegroundColor DarkRed
                exit
            }
            default {
                $STemplate = $Templates.Name
            }
        }
    } else {
        Write-Host &quot;No template found, please check vCenter server and try again.`n&quot; -BackgroundColor Yellow -ForegroundColor DarkRed
        exit
    }

<pre><code># Customization Profile Selection
Header
Write-Host &amp;quot;&amp;quot;
$OSCPs = Get-OSCustomizationSpec | Select Name | Sort-Object
if ($OSCPs.count -ge 2) {
    $j = 1
    $OSCPs | %{Write-Host $j&amp;quot;:&amp;quot; $_.Name; $j++}
    Write-Host $j&amp;quot;: None&amp;quot;
    $OSCPRC = $OSCPs.count + 1
    $OSIndex = Read-Host &amp;quot;Enter a number ( 1 -&amp;quot; $OSCPRC &amp;quot;)&amp;quot;
    $SOSCP = $OSCPs[$OSIndex - 1].Name
    if ($OSIndex -eq $j) { $SOSCP = &amp;quot;None&amp;quot; }
} elseif ($OSCPs.Name -notlike &amp;quot;&amp;quot;) {
    $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 - 2 )&amp;quot;
    Switch ($OSIndex) {
        1 { $SOSCP = $OSCPs.Name }
        2 { $SOSCP = &amp;quot;None&amp;quot; }
    }
} else {
    Write-Host (&amp;quot;Currently, you don't have any customization profile available.&amp;quot;)
    $CPSMC = Read-Host (&amp;quot;[C] continue the process`n[A] Abort and came back latter`nPlease select from above list [default = C]&amp;quot;)
    Switch ($CPSMC) {
        &amp;quot;a&amp;quot; {
            Write-Host (&amp;quot;`nAll process aborted by user!`n&amp;quot;) -BackgroundColor Yellow -ForegroundColor DarkRed
            exit
        }
        default {       
            $SOSCP = &amp;quot;None&amp;quot;
        }
    }
}

# Host Selection
Header
Write-Host &amp;quot;&amp;quot;
$VMHosts = Get-VMHost | Select Name | Sort-Object
if ($VMHosts.count -ge 2) {
    $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
} elseif ($VMHosts.Name -notlike &amp;quot;&amp;quot;) {
    $x = 1
    $VMHosts | %{Write-Host $x&amp;quot;:&amp;quot; $_.Name; $x++}
    $HSMC = Read-Host (&amp;quot;You only have one host, do you want to use it (Y/N)&amp;quot;)
    if ($HSMC -like &amp;quot;y&amp;quot;) {
        $SVMHost = $VMHosts.Name
    } elseif ($HSMC -like &amp;quot;n&amp;quot;) {
        Write-Host (&amp;quot;`nAll process aborted by user!&amp;quot;) -BackgroundColor Yellow -ForegroundColor DarkRed
        Read-Host (&amp;quot;`nPress any key to exit&amp;quot;) -BackgroundColor Yellow -ForegroundColor DarkRed
        exit
    }
} else {
    Write-Host &amp;quot;No host found, please check vCenter server and try again.&amp;quot;
    Read-Host (&amp;quot;`nPress any key to exit&amp;quot;) -BackgroundColor Yellow -ForegroundColor DarkRed
    exit
}

# Datastore Selection
Header
Write-Host &amp;quot;&amp;quot;
$Datastores = Get-VMHost $SVMHost | Get-Datastore | Select Name,FreeSpaceGB,CapacityGB | Sort-Object CapacityGB
if ($Datastores.count -ge 2) {
    $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
} elseif ($Datastores.Name -notlike &amp;quot;&amp;quot;) {
    $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++}
    $DSSMC = Read-Host (&amp;quot;You only have one host, do you want to use it (Y/N) [default = Y]&amp;quot;)
    Switch ($DSSMC) {
        &amp;quot;n&amp;quot; {
            Write-Host (&amp;quot;`nAll process aborted by user!&amp;quot;) -BackgroundColor Yellow -ForegroundColor DarkRed
            exit
        }
        default {
            $SDatastore = $Datastores.Name
        }
    }
} else {
    Write-Host &amp;quot;No datastore found, please check vCenter server and try again.&amp;quot; -BackgroundColor Yellow -ForegroundColor DarkRed
    exit
}

# Location Selection
Header
Write-Host &amp;quot;&amp;quot;
$LSA = Read-Host &amp;quot;[R] Resource Pool`n[V] vApp`n[Other Keys] No Config (simple)`nPlease select location for placing new VM(s)&amp;quot;
$SLocation = &amp;quot;Root&amp;quot;
Switch ($LSA) {
    &amp;quot;r&amp;quot; {
        Write-Host &amp;quot;&amp;quot;
        $ARPs = Get-ResourcePool | Sort-Object Name
        if ($ARPs.count -ge 2) {
            $z = 1
            $ARPs | %{Write-Host $z&amp;quot;:&amp;quot; $_.Name; $z++}
            $RPIndex = Read-Host &amp;quot;Enter a number ( 1 -&amp;quot; $ARPs.count &amp;quot;)&amp;quot;
            $SRP = $ARPs[$RPIndex - 1].Name
            $SLocation = $ARPs[$RPIndex - 1]
        } elseif ($ARPs.Name -notlike &amp;quot;&amp;quot;) {
            $RPSMC = Read-Host (&amp;quot;You only have one resource pool ($ARPs.Name), do you want to use it (Y/N) [default = Y]&amp;quot;)
            Switch ($RPSMC) {
                &amp;quot;n&amp;quot; {
                    $SLocation = &amp;quot;Root&amp;quot;
                }
                default {
                    $SLocation = $ARPs.Name
                }
            }
        } else {
            Write-Host (&amp;quot;Currently, you don't have any resource pool available.&amp;quot;)
            $RPSMC = Read-Host (&amp;quot;[C] continue the process`n[A] Abort and came back latter`nPlease select from above list [default = C]&amp;quot;)
            Switch ($RPSMC) {
                &amp;quot;a&amp;quot; {
                    Write-Host (&amp;quot;`nAll process aborted by user!`n&amp;quot;) -BackgroundColor Yellow -ForegroundColor DarkRed
                    exit
                }
                default {
                    $SLocation = &amp;quot;Root&amp;quot;
                }
            }
        }
    }
    &amp;quot;v&amp;quot; {
        Write-Host &amp;quot;&amp;quot;
        $AVAs = Get-VApp | Sort-Object Name
        if ($AVAs.count -ge 2) {
            $k = 1
            $AVAs | %{Write-Host $k&amp;quot;:&amp;quot; $_.Name;$k++}
            $VAIndex = Read-Host &amp;quot;Enter a number ( 1 -&amp;quot; $AVAs.count &amp;quot;)&amp;quot;
            $SVA = $AVAs[$VAIndex - 1].Name
            $SLocation = $AVAs[$VAIndex - 1]
        } elseif ($AVAs.Name -notlike &amp;quot;&amp;quot;) {
            $VASMC = Read-Host (&amp;quot;You only have one resource pool ($AVAs.Name), do you want to use it (Y/N) [default = Y]&amp;quot;)
            Switch ($VASMC) {
                &amp;quot;n&amp;quot; {
                    $SLocation = &amp;quot;Root&amp;quot;
                }
                default {
                    $SLocation = $AVAs.Name
                }
            }
        } else {
            Write-Host (&amp;quot;Currently, you don't have any vApp available.&amp;quot;)
            $VASMC = Read-Host (&amp;quot;[C] continue the process`n[A] Abort and came back latter`nPlease select from above list [default = C]&amp;quot;)
            Switch ($VASMC) {
                &amp;quot;a&amp;quot; {
                    Write-Host (&amp;quot;`nAll process aborted by user!`n&amp;quot;) -BackgroundColor Yellow -ForegroundColor DarkRed
                    exit
                }
                default {
                    $SLocation = &amp;quot;Root&amp;quot;
                }
            }
        }
    }
    default {
        $SLocation = &amp;quot;Root&amp;quot;
    }
}

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

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

# Getting confirmation and deploying
Header
Write-Host &amp;quot;`nNew VM(s) would be created by bellow configuration
</code></pre>

Source Template: $STemplate
Target Host: $SVMHost
Target Datastore: $SDatastore
OS Customization Profile: $SOSCP
VM naming pattern: $SName
Number of VM(s): $NVMTD
Would place on: $SLocation&quot;

<pre><code># Action Menu
$NVMDConf = Read-Host &amp;quot;`n[C] Create VM(s)`n[R] Create and run VM(s)`n[B] Restart whole process`n[Other Keys] Exit`nPlease select action from list [default = Exit]&amp;quot;
switch ($NVMDConf) {
    &amp;quot;c&amp;quot; {
        Switch ($SOSCP) {
            &amp;quot;None&amp;quot; { $NVMs = 1..$NVMTD | foreach { New-VM -VMHost $SVMHost -Name $SName$_ -Template $STemplate -Datastore $SDatastore } }
            default { $NVMs = 1..$NVMTD | foreach { New-VM -VMHost $SVMHost -Name $SName$_ -Template $STemplate -Datastore $SDatastore -OSCustomizationSpec $SOSCP } }
        }
        Switch ($SLocation) {
            &amp;quot;Root&amp;quot; { break }
            default { $NoOut = Get-VM | Where {$_.Name -like &amp;quot;$SName*&amp;quot;} | Move-VM -Destination $SLocation }
        }
        Write-Host &amp;quot;`n----- Created VMs -----&amp;quot; -NoNewline
        Get-VM | Where {$_.Name -like &amp;quot;$SName*&amp;quot;} | Select Name,PowerState,NumCPU,MemoryMB | Format-Table -AutoSize
        Write-Host &amp;quot;You need to manually start your VMs.`n&amp;quot; -BackgroundColor Yellow -ForegroundColor Blue
        $TOS = Read-Host (&amp;quot;Would you like to create other series (Y/N)&amp;quot;)
        if ($TOS -like &amp;quot;y&amp;quot;) {
            MainBody{}
        }
    }
    &amp;quot;r&amp;quot; {
        Switch ($SOSCP) {
            &amp;quot;None&amp;quot; { $NVMs = 1..$NVMTD | foreach { New-VM -VMHost $SVMHost -Name $SName$_ -Template $STemplate -Datastore $SDatastore | Start-VM } }
            default { $NVMs = 1..$NVMTD | foreach { New-VM -VMHost $SVMHost -Name $SName$_ -Template $STemplate -Datastore $SDatastore -OSCustomizationSpec $SOSCP | Start-VM } }
        }
        Switch ($SLocation) {
            &amp;quot;Root&amp;quot; { break }
            default { $NoOut = Get-VM | Where {$_.Name -like &amp;quot;$SName*&amp;quot;} | Move-VM -Destination $SLocation }
        }
        Write-Host &amp;quot;`n----- Created VMs -----&amp;quot;
        Get-VM | Where {$_.Name -like &amp;quot;$SName*&amp;quot;} | Select Name,PowerState,NumCPU,MemoryMB | Format-Table -AutoSize
        $TOS = Read-Host (&amp;quot;Would you like to create other series (Y/N)&amp;quot;)
        if ($TOS -like &amp;quot;y&amp;quot;) {
            MainBody{}
        }
    }
    &amp;quot;b&amp;quot; {
        MainBody{}
    }
    default {
        Write-Host (&amp;quot;`nAll process aborted by user!`n&amp;quot;) -BackgroundColor Yellow -ForegroundColor DarkRed
        break
    }
}
</code></pre>

}

if ($CRMB -eq &quot;Yes&quot;) {
    MainBody{}
    Write-Host &quot;<code>nThanks for using</code>&quot;Deploy Template Script (v0.3)<code>&amp;quot;</code>nSohrab Kasraeian Fard (@Kasraeian)`n&quot;
} else {
    Write-Host (&quot;Error on connecting to vCenter Server&quot;) -BackgroundColor Yellow -ForegroundColor DarkRed
}

3 Comments

Add a Comment
  1. Works perfectly.

    Thank you, this helped me a lot 🙂

    1. Glad to hear it works fine this time 😉
      Your welcome and thanks for your comment

It's your kindness to leave a reply/feedback