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 "Name:<code>tDeploy Template Script By:</code>tSohrab Kasraeian Fard (@Kasraeian) Ver:`t0.1" ##################################################### <h1>Enabling multiple connection</h1> $PCC = get-PowerCLIConfiguration if ($test.DefaultVIServerMode -eq "single") { Set-PowerCLIConfiguration -DefaultVIServerMode Multiple } <h1>vCenter Server Connection</h1> Write-Host "" $Target = Read-Host ("Which vCenter Do you want") if ($global:DefaultVIServer.Name -eq $Target) { Write-Host "You already connected!" $CRMB = "Yes" } else { $VICSI = Get-VICredentialStoreItem | Sort-Object Host $AI = "No" foreach ($item in $VICSI) { if ($Target -eq $item.Host) { $AI = "Yes" } } if ($AI -eq "No") { $cred = Get-Credential $NCR = Connect-VIServer -Server $Target -Credential $cred } else { $NCR = Connect-VIServer -Server $Target } if ($NCR.IsConnected -eq $true) { $CRMB = "Yes" } else { $CRMB = "No" } } function MainBody { # Template Selection Write-Host "" $Templates = Get-Template | Select Name | Sort-Object Name $i = 1 $Templates | %{Write-Host $i":" $_.Name; $i++} $SIndex = Read-Host "Enter a number ( 1 -" $Templates.Count ")" $STemplate = $Templates[$SIndex - 1].Name Write-Host "You have selected" $STemplate <pre><code># Customization Profile Selection Write-Host &quot;&quot; $OSCPs = Get-OSCustomizationSpec | Select Name | Sort-Object $j = 1 $OSCPs | %{Write-Host $j&quot;:&quot; $_.Name; $j++} Write-Host $j&quot;: None&quot; $OSIndex = Read-Host &quot;Enter a number ( 1 -&quot; $OSCPs.Count &quot;)&quot; $SOSCP = $OSCPs[$OSIndex - 1].Name if ($OSIndex -eq $j) { Write-Host &quot;You didn't select any OS Customization Profile&quot; $SOSCP = &quot;None&quot; } else { Write-Host &quot;You have selected&quot; $SOSCP } # Host Selection Write-Host &quot;&quot; $VMHosts = Get-VMHost | Select Name | Sort-Object $x = 1 $VMHosts | %{Write-Host $x&quot;:&quot; $_.Name; $x++} $VMHIndex = Read-Host &quot;Enter a number ( 1 -&quot; $VMHosts.Count &quot;)&quot; $SVMHost = $VMHosts[$VMHIndex - 1].Name Write-Host &quot;You have selected&quot; $SVMHost # Datastore Selection write-Host &quot;&quot; $Datastores = Get-VMHost $SVMHost | Get-Datastore | Select Name,FreeSpaceGB,CapacityGB | Sort-Object CapacityGB $y = 1 $Datastores | %{Write-Host $y&quot;:&quot; $_.Name&quot;`t`tCapacity (GB):&quot; $_.CapacityGB&quot;`t`tFree Space (GB):&quot; $_.FreeSpaceGB; $y++} $DSIndex = Read-Host &quot;Enter a number ( 1 -&quot; $Datastores.count &quot;)&quot; $SDatastore = $Datastores[$DSIndex - 1].Name Write-Host &quot;You have selected&quot; $SDatastore # Name Selection Write-Host &quot;&quot; $SName = Read-Host &quot;Please enter VMs Naming Patern&quot; # Getting VM count Write-Host &quot;&quot; $NVMTD = Read-Host &quot;How many VMs do you want to deploy&quot; # Getting confirmation and deploying Write-Host &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&quot; $NVMDConf = Read-Host &quot;It's confirmed (Y/N)&quot; if ($NVMDConf -eq &quot;Y&quot;) { if ($SOSCP -eq &quot;None&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 &quot;`n----- Created VMs -----&quot; Get-VM | Where {$_.Name -like &quot;$SName*&quot;} | Select Name,NumCPU,MemoryMB | Format-Table -AutoSize } else { Write-Host &quot;All process aborted by user!&quot; } # Power On Option Write-Host &quot;&quot; $SPOO = Read-Host &quot;Do you want to power on all created VMs (Y/N)&quot; if ($SPOO -eq &quot;Y&quot;) { $SVMs = Start-VM -VM $NVMs Write-Host &quot;`n----- Powered On VMs -----&quot; Get-VM | Where {$_.Name -like &quot;$SName*&quot;} | Select Name,NumCPU,MemoryMB,PowerState | Format-Table -AutoSize } else { Write-Host &quot;You need to manually start your VMs.&quot; } </code></pre> } if ($CRMB -eq "Yes") { MainBody{} } else { Write-Host "Error on connecting to vCenter Server" }
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