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]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 | ##################################################### function Header { CLS Write-Host "Name:`tDeploy Template Script (v0.3)" } ##################################################### <h1>Adding PSSnapin</h1> $pssnap = Get-PSSnapin | Sort-Object Name $APS = "No" $PSIndex = 1 1.. $pssnap .Count | foreach { if ( $pssnap [ $PSIndex ].Name -like "vmware.vimautomation.core") { $APS = "Yes" } $PSIndex ++ } if ( $APS -eq "No") { Add-PSSnapin VMware.VimAutomation.Core } <h1>Enabling multiple connection</h1> $PCC = get-PowerCLIConfiguration if ( $test .DefaultVIServerMode -eq "single") { Set-PowerCLIConfiguration -DefaultVIServerMode Multiple } <h1>vCenter Server Connection</h1> Header Write-Host "" $Target = Read-Host ("Which vCenter Do you want connect to") if ( $global:DefaultVIServer .Name -eq $Target ) { $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 Header Write-Host "" $Templates = Get-Template | Select Name | Sort-Object Name if ( $Templates .count -ge 2) { $i = 1 $Templates | %{ Write-Host $i ":" $<em>.Name; $i ++} $SIndex = Read-Host "Enter a number ( 1 -" $Templates .Count ")" $STemplate = $Templates [ $SIndex - 1].Name } elseif ( $Templates .Name -notlike "") { $i = 1 $Templates | %{ Write-Host $i ":" $</em>.Name; $i ++} $TSMC = Read-Host ("You only have one template, do you want to use it (Y/N) [ default = Y]") Switch ( $TSMC ) { "n" { Write-Host ("<code>nAll process aborted by user!</code>n") -BackgroundColor Yellow -ForegroundColor DarkRed exit } default { $STemplate = $Templates .Name } } } else { Write-Host "No template found, please check vCenter server and try again.`n" -BackgroundColor Yellow -ForegroundColor DarkRed exit } <pre><code> # Customization Profile Selection Header Write-Host &quot;&quot; $OSCPs = Get-OSCustomizationSpec | Select Name | Sort-Object if ( $OSCPs .count -ge 2) { $j = 1 $OSCPs | %{ Write-Host $j &quot;:&quot; $_ .Name; $j ++} Write-Host $j &quot;: None&quot; $OSCPRC = $OSCPs .count + 1 $OSIndex = Read-Host &quot;Enter a number ( 1 -&quot; $OSCPRC &quot;)&quot; $SOSCP = $OSCPs [ $OSIndex - 1].Name if ( $OSIndex -eq $j ) { $SOSCP = &quot;None&quot; } } elseif ( $OSCPs .Name -notlike &quot;&quot;) { $j = 1 $OSCPs | %{ Write-Host $j &quot;:&quot; $_ .Name; $j ++} Write-Host $j &quot;: None&quot; $OSIndex = Read-Host &quot;Enter a number ( 1 - 2 )&quot; Switch ( $OSIndex ) { 1 { $SOSCP = $OSCPs .Name } 2 { $SOSCP = &quot;None&quot; } } } else { Write-Host (&quot;Currently, you don 't have any customization profile available.&quot;) $CPSMC = Read-Host (&quot;[C] continue the process`n[A] Abort and came back latter`nPlease select from above list [default = C]&quot;) Switch ($CPSMC) { &quot;a&quot; { Write-Host (&quot;`nAll process aborted by user!`n&quot;) -BackgroundColor Yellow -ForegroundColor DarkRed exit } default { $SOSCP = &quot;None&quot; } } } # Host Selection Header Write-Host &quot;&quot; $VMHosts = Get-VMHost | Select Name | Sort-Object if ($VMHosts.count -ge 2) { $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 } elseif ($VMHosts.Name -notlike &quot;&quot;) { $x = 1 $VMHosts | %{Write-Host $x&quot;:&quot; $_.Name; $x++} $HSMC = Read-Host (&quot;You only have one host, do you want to use it (Y/N)&quot;) if ($HSMC -like &quot;y&quot;) { $SVMHost = $VMHosts.Name } elseif ($HSMC -like &quot;n&quot;) { Write-Host (&quot;`nAll process aborted by user!&quot;) -BackgroundColor Yellow -ForegroundColor DarkRed Read-Host (&quot;`nPress any key to exit&quot;) -BackgroundColor Yellow -ForegroundColor DarkRed exit } } else { Write-Host &quot;No host found, please check vCenter server and try again.&quot; Read-Host (&quot;`nPress any key to exit&quot;) -BackgroundColor Yellow -ForegroundColor DarkRed exit } # Datastore Selection Header Write-Host &quot;&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&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 } elseif ($Datastores.Name -notlike &quot;&quot;) { $y = 1 $Datastores | %{Write-Host $y&quot;:&quot; $_.Name&quot;`t`tCapacity (GB):&quot; $_.CapacityGB&quot;`t`tFree Space (GB):&quot; $_.FreeSpaceGB; $y++} $DSSMC = Read-Host (&quot;You only have one host, do you want to use it (Y/N) [default = Y]&quot;) Switch ($DSSMC) { &quot;n&quot; { Write-Host (&quot;`nAll process aborted by user!&quot;) -BackgroundColor Yellow -ForegroundColor DarkRed exit } default { $SDatastore = $Datastores.Name } } } else { Write-Host &quot;No datastore found, please check vCenter server and try again.&quot; -BackgroundColor Yellow -ForegroundColor DarkRed exit } # Location Selection Header Write-Host &quot;&quot; $LSA = Read-Host &quot;[R] Resource Pool`n[V] vApp`n[Other Keys] No Config (simple)`nPlease select location for placing new VM(s)&quot; $SLocation = &quot;Root&quot; Switch ($LSA) { &quot;r&quot; { Write-Host &quot;&quot; $ARPs = Get-ResourcePool | Sort-Object Name if ($ARPs.count -ge 2) { $z = 1 $ARPs | %{Write-Host $z&quot;:&quot; $_.Name; $z++} $RPIndex = Read-Host &quot;Enter a number ( 1 -&quot; $ARPs.count &quot;)&quot; $SRP = $ARPs[$RPIndex - 1].Name $SLocation = $ARPs[$RPIndex - 1] } elseif ($ARPs.Name -notlike &quot;&quot;) { $RPSMC = Read-Host (&quot;You only have one resource pool ($ARPs.Name), do you want to use it (Y/N) [default = Y]&quot;) Switch ($RPSMC) { &quot;n&quot; { $SLocation = &quot;Root&quot; } default { $SLocation = $ARPs.Name } } } else { Write-Host (&quot;Currently, you don' t have any resource pool available.&quot;) $RPSMC = Read-Host (&quot; [C] continue the process`n[A] Abort and came back latter`nPlease select from above list [ default = C]&quot;) Switch ( $RPSMC ) { &quot;a&quot; { Write-Host (&quot;`nAll process aborted by user!`n&quot;) -BackgroundColor Yellow -ForegroundColor DarkRed exit } default { $SLocation = &quot;Root&quot; } } } } &quot;v&quot; { Write-Host &quot;&quot; $AVAs = Get-VApp | Sort-Object Name if ( $AVAs .count -ge 2) { $k = 1 $AVAs | %{ Write-Host $k &quot;:&quot; $_ .Name; $k ++} $VAIndex = Read-Host &quot;Enter a number ( 1 -&quot; $AVAs .count &quot;)&quot; $SVA = $AVAs [ $VAIndex - 1].Name $SLocation = $AVAs [ $VAIndex - 1] } elseif ( $AVAs .Name -notlike &quot;&quot;) { $VASMC = Read-Host (&quot;You only have one resource pool ( $AVAs .Name), do you want to use it (Y/N) [ default = Y]&quot;) Switch ( $VASMC ) { &quot;n&quot; { $SLocation = &quot;Root&quot; } default { $SLocation = $AVAs .Name } } } else { Write-Host (&quot;Currently, you don't have any vApp available.&quot;) $VASMC = Read-Host (&quot; [C] continue the process`n[A] Abort and came back latter`nPlease select from above list [ default = C]&quot;) Switch ( $VASMC ) { &quot;a&quot; { Write-Host (&quot;`nAll process aborted by user!`n&quot;) -BackgroundColor Yellow -ForegroundColor DarkRed exit } default { $SLocation = &quot;Root&quot; } } } } default { $SLocation = &quot;Root&quot; } } # Name Selection Header Write-Host &quot;&quot; $SName = Read-Host &quot;Please enter VMs Naming Patern&quot; # Getting VM count $NVMTD = Read-Host &quot;How many VMs do you want to deploy&quot; # Getting confirmation and deploying Header Write-Host &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 " <pre><code> # Action Menu $NVMDConf = Read-Host &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]&quot; switch ( $NVMDConf ) { &quot;c&quot; { Switch ( $SOSCP ) { &quot;None&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 ) { &quot;Root&quot; { break } default { $NoOut = Get-VM | Where { $_ .Name -like &quot; $SName *&quot;} | Move-VM -Destination $SLocation } } Write-Host &quot;`n----- Created VMs -----&quot; -NoNewline Get-VM | Where { $_ .Name -like &quot; $SName *&quot;} | Select Name,PowerState,NumCPU,MemoryMB | Format-Table -AutoSize Write-Host &quot;You need to manually start your VMs.`n&quot; -BackgroundColor Yellow -ForegroundColor Blue $TOS = Read-Host (&quot;Would you like to create other series (Y/N)&quot;) if ( $TOS -like &quot;y&quot;) { MainBody{} } } &quot;r&quot; { Switch ( $SOSCP ) { &quot;None&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 ) { &quot;Root&quot; { break } default { $NoOut = Get-VM | Where { $_ .Name -like &quot; $SName *&quot;} | Move-VM -Destination $SLocation } } Write-Host &quot;`n----- Created VMs -----&quot; Get-VM | Where { $_ .Name -like &quot; $SName *&quot;} | Select Name,PowerState,NumCPU,MemoryMB | Format-Table -AutoSize $TOS = Read-Host (&quot;Would you like to create other series (Y/N)&quot;) if ( $TOS -like &quot;y&quot;) { MainBody{} } } &quot;b&quot; { MainBody{} } default { Write-Host (&quot;`nAll process aborted by user!`n&quot;) -BackgroundColor Yellow -ForegroundColor DarkRed break } } </code></pre> } if ( $CRMB -eq "Yes") { MainBody{} Write-Host "<code>nThanks for using</code>"Deploy Template Script (v0.3)<code>&quot;</code>nSohrab Kasraeian Fard ( @Kasraeian )`n" } else { Write-Host ("Error on connecting to vCenter Server") -BackgroundColor Yellow -ForegroundColor DarkRed } |
Works perfectly.
Thank you, this helped me a lot
Glad to hear it works fine this time
Your welcome and thanks for your comment