Kasraeian

vWorld v.3

Using VMware PowerCLI (#006) – Deploy Template (v0.4)

I’ve edited “DTS (v0.3)” so it can generate “.csv” and accept input from “.csv” file as well.
Feel free to send your comment and idea and if you find any bug/problem in this script please let me know, I hope this version can do better work and be useful.

[wpdm_file id=8]

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

<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;) {
    Write-Host &quot;Adding required PowerShell Snapin&quot;
    Add-PSSnapin VMware.VimAutomation.Core
}

<h1>Enabling multiple connection</h1>

$PCC = get-PowerCLIConfiguration
if ($test.DefaultVIServerMode -eq &quot;single&quot;) {
    Write-Host &quot;Changing <code>&amp;quot;Default VI Server Mode</code>&quot;&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
        Write-Host &quot;Connecting to $Target&quot;
        $NCR = Connect-VIServer -Server $Target -Credential $cred
    } else {
        Write-Host &quot;Connecting to $Target&quot;
        $NCR = Connect-VIServer -Server $Target
    }
    if ($NCR.IsConnected -eq $true) {
        $CRMB = &quot;Yes&quot;
    } else {
        $CRMB = &quot;No&quot;
    }
}

<h1>Automated Mode functions and process</h1>

function AutomatedMode {
    Header
    Write-Host &quot;<code>nFor automated mode you need to present</code>&quot;.csv<code>&amp;quot; file in bellow format</code>nName,Template,VMHost,Datastore,ResourcePool,vApp,CustomizationProfile,StartState&quot;
    $AFEA = Read-Host (&quot;<code>nDo you have csv file matching above format (Y/N) [default = N]&amp;quot;)
    Switch ($AFEA) {
        &amp;quot;y&amp;quot; {
            $CFP = Read-Host (&amp;quot;Please give the full path of</code>&quot;.csv<code>&amp;quot; file&amp;quot;)
            if ($CFP -notlike &amp;quot;&amp;quot;) {
                Import-Csv -Path $CFP | foreach {
                    if ($($_.Name) -and $($_.Template) -and $($_.VMHost) -and $($_.Datastore) -notlike &amp;quot;&amp;quot;) {
                        if ($_.StartState -eq &amp;quot;Yes&amp;quot;) {
                            if ($_.CustomizationProfile -notlike &amp;quot;&amp;quot; -and $_.CustomizationProfile -notlike &amp;quot;none&amp;quot;) {
                                if ($($_.ResourcePool) -notlike &amp;quot;&amp;quot; -and $($_.ResourcePool) -notlike &amp;quot;none&amp;quot;) {
                                    New-VM -Name $($_.Name) -Template $($_.Template) -VMHost $($_.VMHost) -Datastore $($_.Datastore) -OSCustomizationSpec $($_.CustomizationProfile) -ResourcePool $($_.ResourcePool) | Start-VM
                                } elseif ($($_.vApp) -notlike &amp;quot;&amp;quot; -and $($_.vApp) -notlike &amp;quot;none&amp;quot;) {
                                    New-VM -Name $($_.Name) -Template $($_.Template) -VMHost $($_.VMHost) -Datastore $($_.Datastore) -OSCustomizationSpec $($_.CustomizationProfile) | Move-VM -Destination $($_.vApp) | Start-VM
                                } else {
                                    New-VM -Name $($_.Name) -Template $($_.Template) -VMHost $($_.VMHost) -Datastore $($_.Datastore) -OSCustomizationSpec $($_.CustomizationProfile) | Start-VM
                                }
                            } else {
                                if ($($_.ResourcePool) -notlike &amp;quot;&amp;quot; -and $($_.ResourcePool) -notlike &amp;quot;none&amp;quot;) {
                                    New-VM -Name $($_.Name) -Template $($_.Template) -VMHost $($_.VMHost) -Datastore $($_.Datastore) -ResourcePool $($_.ResourcePool) | Start-VM
                                } elseif ($($_.vApp) -notlike &amp;quot;&amp;quot; -and $($_.vApp) -notlike &amp;quot;none&amp;quot;) {
                                    New-VM -Name $($_.Name) -Template $($_.Template) -VMHost $($_.VMHost) -Datastore $($_.Datastore) | Move-VM -Destination $($_.vApp) | Start-VM
                                } else {
                                    New-VM -Name $($_.Name) -Template $($_.Template) -VMHost $($_.VMHost) -Datastore $($_.Datastore) | Start-VM
                                }
                            }
                        } else {
                            if ($_.CustomizationProfile -notlike &amp;quot;&amp;quot; -and $_.CustomizationProfile -notlike &amp;quot;none&amp;quot;) {
                                if ($($_.ResourcePool) -notlike &amp;quot;&amp;quot; -and $($_.ResourcePool) -notlike &amp;quot;none&amp;quot;) {
                                    New-VM -Name $($_.Name) -Template $($_.Template) -VMHost $($_.VMHost) -Datastore $($_.Datastore) -OSCustomizationSpec $($_.CustomizationProfile) -ResourcePool $($_.ResourcePool)
                                } elseif ($($_.vApp) -notlike &amp;quot;&amp;quot; -and $($_.vApp) -notlike &amp;quot;none&amp;quot;) {
                                    New-VM -Name $($_.Name) -Template $($_.Template) -VMHost $($_.VMHost) -Datastore $($_.Datastore) -OSCustomizationSpec $($_.CustomizationProfile) | Move-VM -Destination $($_.vApp)
                                } else {
                                    New-VM -Name $($_.Name) -Template $($_.Template) -VMHost $($_.VMHost) -Datastore $($_.Datastore) -OSCustomizationSpec $($_.CustomizationProfile)
                                }
                            } else {
                                if ($($_.ResourcePool) -notlike &amp;quot;&amp;quot; -and $($_.ResourcePool) -notlike &amp;quot;none&amp;quot;) {
                                    New-VM -Name $($_.Name) -Template $($_.Template) -VMHost $($_.VMHost) -Datastore $($_.Datastore) -ResourcePool $($_.ResourcePool)
                                } elseif ($($_.vApp) -notlike &amp;quot;&amp;quot; -and $($_.vApp) -notlike &amp;quot;none&amp;quot;) {
                                    New-VM -Name $($_.Name) -Template $($_.Template) -VMHost $($_.VMHost) -Datastore $($_.Datastore) | Move-VM -Destination $($_.vApp)
                                } else {
                                    New-VM -Name $($_.Name) -Template $($_.Template) -VMHost $($_.VMHost) -Datastore $($_.Datastore)
                                }
                            }
                        }
                    }
                }
                $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{}
                } else {
                    Write-Host &amp;quot;</code>nThanks for using <code>&amp;quot;Deploy Template Script (v0.4)</code>&quot;<code>nSohrab Kasraeian Fard (@Kasraeian)</code>n&quot;
                    Exit
                }
            } else {
                Write-Host (&quot;<code>nProvided information is not correct&amp;quot;) -BackgroundColor Yellow -ForegroundColor DarkRed
                $Answer = Read-Host (&amp;quot;</code>nDo you want to try again (Y/N) [default = Y]&quot;)
                Switch ($Answer) {
                    &quot;n&quot; {
                        Header
                        Write-Host (&quot;<code>nAll process aborted by user!</code>n&quot;) -BackgroundColor Yellow -ForegroundColor DarkRed
                        Write-Host &quot;<code>nThanks for using</code>&quot;Deploy Template Script (v0.4)<code>&amp;quot;</code>nSohrab Kasraeian Fard (@Kasraeian)<code>n&amp;quot;
                        Exit
                    }
                    default {
                        AutomatedMode{}
                    }
                }
            }
        }
        default {
            Header
            $CNCFA = Read-Host (&amp;quot;</code>n[M] Manually create new <code>&amp;quot;.csv</code>&quot; file<code>n[Other keys] Exit</code>nPlease select from above items [default = Exit]&quot;)
            Switch ($CNCFA) {
                &quot;m&quot; {
                    Header
                    Write-Host &quot;<code>nPlease provide below information, items with * are required in order to create new series of VMs&amp;quot;
                    $Name = Read-Host (&amp;quot;Please enter naming patern*&amp;quot;)
                    $Template = Read-Host (&amp;quot;Please enter source template name*&amp;quot;)
                    $VMHost = Read-Host (&amp;quot;Please define target host*&amp;quot;)
                    $Datastore = Read-Host (&amp;quot;Please define target datastore*&amp;quot;)
                    $ResourcePool = Read-Host (&amp;quot;Please enter resource pool to place new VM(s)&amp;quot;)
                    $vApp = Read-Host (&amp;quot;Please enter vApp to place new VM(s)&amp;quot;)
                    $OSCP = Read-Host (&amp;quot;Please select OS customization specification&amp;quot;)
                    $StartState = Read-Host (&amp;quot;Please type</code>&quot;Yes<code>&amp;quot; if you want to power on your VMs after their creation or</code>&quot;No<code>&amp;quot; for manual process&amp;quot;)
                    $Qty = Read-Host (&amp;quot;How many VM(s) do you want to deploy&amp;quot;)
                    $SCF = Read-Host (&amp;quot;Please provide full address to save csv file&amp;quot;)
                    if ($Qty -ge 1) {
                        if ($Name -and $Template -and $VMHost -and $Datastore -and $SCF -notlike &amp;quot;&amp;quot;) {
                            $i = 1
                            $FileContent = &amp;quot;Name,Template,VMHost,Datastore,ResourcePool,vApp,CustomizationProfile,StartState&amp;quot;
                            1..$Qty | foreach {
                                $NewContent = &amp;quot;</code>n$Name$i,$Template,$VMHost,$Datastore,$ResourcePool,$vApp,$OSCP,$StartState&quot;
                                $FileContent = $FileContent + $NewContent
                                $i++
                            }
                            Header
                            Write-Host &quot;<code>nNew</code>&quot;.csv<code>&amp;quot; file would be create containing bellow information on $SCF</code>n&quot;
                            $FileContent
                            $WCFA = Read-Host (&quot;<code>nDo you want to save these information (Y/N)&amp;quot;)
                            Switch ($WCFA) {
                                &amp;quot;y&amp;quot; {
                                    $FileContent | Out-File $SCF
                                }
                                &amp;quot;n&amp;quot; {
                                    $RWPA = Read-Host (&amp;quot;</code>n[N] Create new <code>&amp;quot;.csv</code>&quot; file<code>n[M] Main menu</code>n[Other keys] Exit<code>nPlease select from above items [default = Exit]&amp;quot;)
                                    Switch ($RWPA) {
                                        &amp;quot;n&amp;quot; {
                                            AutomatedMode{}
                                        }
                                        &amp;quot;m&amp;quot; {
                                            MainBody{}
                                        }
                                        default {
                                            Header
                                            Write-Host (&amp;quot;</code>nAll process aborted by user!<code>n&amp;quot;) -BackgroundColor Yellow -ForegroundColor DarkRed
                                            Write-Host &amp;quot;</code>nThanks for using <code>&amp;quot;Deploy Template Script (v0.4)</code>&quot;<code>nSohrab Kasraeian Fard (@Kasraeian)</code>n&quot;
                                            Exit
                                        }
                                    }
                                }
                            }
                            $Answer = Read-Host (&quot;<code>n[E] Edit</code>&quot;.csv<code>&amp;quot; file and back to main menu</code>n[M] Main menu<code>n[Other keys] Exit</code>nPlease select from above items [default = Exit]&quot;)
                            Switch ($Answer) {
                                &quot;e&quot; {
                                    Invoke-Item $SCF
                                    MainBody{}
                                }
                                &quot;m&quot; {
                                    MainBody{}
                                }
                                default {
                                    Header
                                    Write-Host (&quot;<code>nAll process aborted by user!</code>n&quot;) -BackgroundColor Yellow -ForegroundColor DarkRed
                                    Write-Host &quot;<code>nThanks for using</code>&quot;Deploy Template Script (v0.4)<code>&amp;quot;</code>nSohrab Kasraeian Fard (@Kasraeian)<code>n&amp;quot;
                                    Exit
                                }
                            }
                        } else {
                            Write-Host (&amp;quot;</code>nAll required data is not provided<code>n&amp;quot;) -BackgroundColor Yellow -ForegroundColor DarkRed
                            $Answer = Read-Host (&amp;quot;</code>nDo you want to try again (Y/N) [default = Y]&quot;)
                            Switch ($Answer) {
                                &quot;n&quot; {
                                    Header
                                    Write-Host (&quot;<code>nAll process aborted by user!</code>n&quot;) -BackgroundColor Yellow -ForegroundColor DarkRed
                                    Write-Host &quot;<code>nThanks for using</code>&quot;Deploy Template Script (v0.4)<code>&amp;quot;</code>nSohrab Kasraeian Fard (@Kasraeian)<code>n&amp;quot;
                                    Exit
                                }
                                default {
                                    AutomatedMode{}
                                }
                            }
                        }
                    } else {
                        Write-Host (&amp;quot;You have provided wrong information, number of VM(s) to be deploy should more than 0&amp;quot;) -BackgroundColor Yellow -ForegroundColor DarkRed
                        $Answer = Read-Host (&amp;quot;</code>nDo you want to try again (Y/N) [default = Y]&quot;)
                        Switch ($Answer) {
                            &quot;n&quot; {
                                Header
                                Write-Host (&quot;<code>nAll process aborted by user!</code>n&quot;) -BackgroundColor Yellow -ForegroundColor DarkRed
                                Write-Host &quot;<code>nThanks for using</code>&quot;Deploy Template Script (v0.4)<code>&amp;quot;</code>nSohrab Kasraeian Fard (@Kasraeian)<code>n&amp;quot;
                                Exit
                            }
                            default {
                                AutomatedMode{}
                            }
                        }
                    }
                }
                default {
                    Header
                    Write-Host (&amp;quot;</code>nAll process aborted by user!<code>n&amp;quot;) -BackgroundColor Yellow -ForegroundColor DarkRed
                    Write-Host &amp;quot;</code>nThanks for using <code>&amp;quot;Deploy Template Script (v0.4)</code>&quot;<code>nSohrab Kasraeian Fard (@Kasraeian)</code>n&quot;
                    Exit
                }
            }
        }
    }
}

<h1>Interactive Mode functions and process</h1>

function InteractiveMode {
    # 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]
            $SvApp = $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;
                    $SvApp = &amp;quot;none&amp;quot;
                }
                default {
                    $SLocation = $AVAs.Name
                    $SvApp = $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;
                    $SvApp = &amp;quot;none&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[S] Create and start VM(s)`n[R] Restart whole process`n[M] Main menu`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{}
        } else {
            Write-Host &amp;quot;`nThanks for using `&amp;quot;Deploy Template Script (v0.4)`&amp;quot;`nSohrab Kasraeian Fard (@Kasraeian)`n&amp;quot;
            Exit
        }
    }
    &amp;quot;s&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{}
        } else {
            Write-Host &amp;quot;`nThanks for using `&amp;quot;Deploy Template Script (v0.4)`&amp;quot;`nSohrab Kasraeian Fard (@Kasraeian)`n&amp;quot;
            Exit
        }
    }
    &amp;quot;r&amp;quot; {
        InteractiveMode{}
    }
    &amp;quot;m&amp;quot; {
        MainBody{}
    }
    default {
        Header
        Write-Host (&amp;quot;`nAll process aborted by user!`n&amp;quot;) -BackgroundColor Yellow -ForegroundColor DarkRed
        Write-Host &amp;quot;Thanks for using `&amp;quot;Deploy Template Script (v0.4)`&amp;quot;`nSohrab Kasraeian Fard (@Kasraeian)`n&amp;quot;
        Exit
    }
}
</code></pre>

}

function MainBody {
    if ($CRMB -eq &quot;Yes&quot;) {
        Header
        $ModeSelection = Read-Host (&quot;<code>n[A] Automated Mode (csv)</code>n[I] Interactive Mode<code>n[Other keys] Exit</code>nPlease select from above list [default = E]&quot;)
        Switch ($ModeSelection) {
            &quot;i&quot; {
                Header
                InteractiveMode{}
            }
            &quot;a&quot; {
                Header
                AutomatedMode{}
            }
            default {
                Header
                Write-Host (&quot;<code>nAll process aborted by user!</code>n&quot;) -BackgroundColor Yellow -ForegroundColor DarkRed
                Write-Host &quot;<code>nThanks for using</code>&quot;Deploy Template Script (v0.4)<code>&amp;quot;</code>nSohrab Kasraeian Fard (@Kasraeian)`n&quot;
                Exit
            }
        }
    } else {
        Write-Host (&quot;Error on connecting to vCenter Server&quot;) -BackgroundColor Yellow -ForegroundColor DarkRed
    }
}

MainBody

<h3>EOF ###

It's your kindness to leave a reply/feedback

Kasraeian © 2016
%d bloggers like this: