Instantly share code, notes, and snippets.
Last active
August 16, 2016 06:36
-
Star
0
(0)
You must be signed in to star a gist -
Fork
0
(0)
You must be signed in to fork a gist
-
Save theonlyway/710d93d9731e530bcd512ea01d446a2f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#requires -Version 2.0 | |
$logfiledate = Get-Date -Format 'yyyy-MM-dd %h-m-ss' | |
$logfile = "C:\Admin\Logs\Deployment Transcript $logfiledate.txt" | |
Start-Transcript -path $logfile | |
if ( (Get-PSSnapin -Name 'VMware.VimAutomation.Core' -ErrorAction SilentlyContinue) -eq $null ) | |
{ | |
Add-PSSnapin -Name 'VMware.VimAutomation.Core' | |
} | |
Write-Host Connecting to server -ForegroundColor 'magenta' | |
connect-viserver blah -ErrorAction Stop | |
#Generate random password | |
Add-Type -AssemblyName System.Web | |
$length = 12 | |
$numberOfNonAlphanumericCharacters = 4 | |
$password = [Web.Security.Membership]::GeneratePassword($length,$numberOfNonAlphanumericCharacters) | |
#Set VNC in the vmx | |
Function Set_VMX | |
{ | |
#Generating VNC port | |
[int]$vncport = Get-Content -Path vncport.txt | |
$vncport++ | |
$vncport | Out-File -FilePath VNCPort.txt | |
Get-VM -name $nameselection | | |
Get-AdvancedSetting -name 'RemoteDisplay.vnc.password' | | |
Set-AdvancedSetting -Value $password -confirm:$false | |
Get-VM -name $nameselection | | |
Get-AdvancedSetting -name 'RemoteDisplay.vnc.port' | | |
Set-AdvancedSetting -Value $vncport -confirm:$false | |
} | |
#Email configuration details | |
Function Email_deets | |
{ | |
$uuid = get-vm -name $nameselection | ForEach-Object -Process { | |
(Get-View $_.Id).config.uuid | |
} | |
$guid = get-vm -name $nameselection | | |
Get-View | | |
Select-Object -ExpandProperty MoRef | |
$bundleplan = $bundleselection.Plan | |
[int]$vncportout = Get-Content -Path vncport.txt | |
Send-MailMessage -SmtpServer blah -To 'blah' -From 'blah' -Subject "[iseek-track #$ticketselection] Configuration details for VM: $nameselection" -Attachments $logfile -Body "Administrator username: Administrator | |
Password: $password | |
Bundle: $bundleplan | |
Template: $osplan | |
OSCode: $oscode | |
BYOL expiry: $BYOLselection | |
Short backup: $shortbackup | |
Long backup: $longbackup | |
Datastore: $datastoreselection | |
Port group: $portgroupselection | |
I.P: $ipselection | |
Subnet: $subselection | |
Gateway: $gateselection | |
VNC Port: $vncportout | |
VNC Password: $password | |
UUID: $uuid | |
GUID: $guid | |
" | |
} | |
#Ticket # | |
Function Enter_Ticket | |
{ | |
Clear-Host | |
[int]$tans = Read-Host -Prompt 'Enter the ticket #' | |
$ticketselection = $tans | |
if (($ticketselection -eq $null) -or ($tans -eq 0)) | |
{ | |
Write-Host -ForegroundColor Red -Object "Couldn't determine the ticket #. Please retry..." | |
Start-Sleep -Seconds 1 | |
Enter_Ticket | |
} | |
else | |
{ | |
#Goto Name | |
Select_Name | |
} | |
} | |
#Name the VM | |
Function Select_Name | |
{ | |
[string]$nans = Read-Host -Prompt 'Enter the name you want to assign to the VM' | |
$nameselection = $nans | |
if (($nameselection -eq $null) -or ($nans -eq 0)) | |
{ | |
Write-Host -ForegroundColor Red -Object "Couldn't determine the name you want. Please retry..." | |
Start-Sleep -Seconds 1 | |
Select_Name | |
} | |
else | |
{ | |
#Go to Organisation | |
Select_OrgName | |
} | |
} | |
#Organisation name | |
Function Select_OrgName | |
{ | |
[string]$onans = Read-Host -Prompt 'Enter the Organisation name you want to assign to the VM' | |
$orgnameselection = $onans | |
if (($orgnameselection -eq $null) -or ($onans -eq 0)) | |
{ | |
Write-Host -ForegroundColor Red -Object "Couldn't determine the name you want. Please retry..." | |
Start-Sleep -Seconds 1 | |
Select_Name | |
} | |
else | |
{ | |
#Go to I.P | |
Select_IP | |
} | |
} | |
#I.P | |
Function Select_IP | |
{ | |
[string]$ipans = Read-Host -Prompt 'Enter the I.P you want to assign to the VM' | |
$ipselection = $ipans | |
if (($ipselection -eq $null) -or ($ipans -eq 0)) | |
{ | |
Write-Host -ForegroundColor Red -Object "Couldn't determine the I.P you want. Please retry..." | |
Start-Sleep -Seconds 1 | |
Select_Name | |
} | |
else | |
{ | |
#Go to subnet | |
Select_Subnet | |
} | |
} | |
#subnet | |
Function Select_Subnet | |
{ | |
[string]$subans = Read-Host -Prompt 'Enter the subnet you want to assign to the VM' | |
$subselection = $subans | |
if (($subselection -eq $null) -or ($subans -eq 0)) | |
{ | |
Write-Host -ForegroundColor Red -Object "Couldn't determine the subnet you want. Please retry..." | |
Start-Sleep -Seconds 1 | |
Select_Name | |
} | |
else | |
{ | |
#Go to gateway | |
Select_Gateway | |
} | |
} | |
#gateway | |
Function Select_Gateway | |
{ | |
[string]$gateans = Read-Host -Prompt 'Enter the gateway you want to assign to the VM' | |
$gateselection = $gateans | |
if (($gateselection -eq $null) -or ($gateans -eq 0)) | |
{ | |
Write-Host -ForegroundColor Red -Object "Couldn't determine the gateway you want. Please retry..." | |
Start-Sleep -Seconds 1 | |
Select_Name | |
} | |
else | |
{ | |
#Go to bundle | |
Select_Bundle | |
} | |
} | |
#Bundle menu | |
Function Select_Bundle | |
{ | |
$bundle = Import-Csv -Path bundles.csv | |
Write-Host Select your bundle | |
$bundlemenu = @{} | |
for ($i = 1;$i -le $bundle.count; $i++) | |
{ | |
Write-Host -Object "$i. $($bundle[$i-1])" -ForegroundColor 'green' | |
$bundlemenu.Add($i,($bundle[$i-1])) | |
} | |
[int]$bundleans = Read-Host -Prompt 'Enter bundle selection' | |
$bundleselection = $bundlemenu.Item($bundleans) | |
if (($bundleans -gt $bundle.count) -or ($bundleselection -eq $null) -or ($bundleans -eq 0)) | |
{ | |
Write-Host -ForegroundColor Red -Object "Couldn't determine which bundle you want. Please retry..." | |
Start-Sleep -Seconds 1 | |
Select_Bundle | |
} | |
else | |
{ | |
#Go to OS plan | |
Select_OSPlan | |
} | |
} | |
#Select OS Plan | |
Function Select_OSPlan | |
{ | |
Write-Host Please select the OS plan | |
[int]$osselection = 0 | |
while ( $osselection -lt 1 -or $osselection -gt 18 ) | |
{ | |
Write-Host -Object '1. Windows Server 2008 R2 Standard' -ForegroundColor 'green' | |
Write-Host -Object '2. Windows Server 2008 R2 Standard - BYOL' -ForegroundColor 'green' | |
Write-Host -Object '3. Windows Server 2012 Standard' -ForegroundColor 'green' | |
Write-Host -Object '4. Windows Server 2012 Standard - BYOL' -ForegroundColor 'green' | |
Write-Host -Object '5. Windows Server 2012 R2 Standard' -ForegroundColor 'green' | |
Write-Host -Object '6. Windows Server 2012 R2 Standard - BYOL' -ForegroundColor 'green' | |
Write-Host -Object '7. Windows Server 2008 R2 Standard + SQL WEB' -ForegroundColor 'green' | |
Write-Host -Object '8. Windows Server 2008 R2 Standard + SQL WEB - BYOL' -ForegroundColor 'green' | |
Write-Host -Object '9. Windows Server 2012 Standard + SQL WEB' -ForegroundColor 'green' | |
Write-Host -Object '10. Windows Server 2012 Standard + SQL WEB - BYOL' -ForegroundColor 'green' | |
Write-Host -Object '11. Windows Server 2012 R2 Standard + SQL WEB' -ForegroundColor 'green' | |
Write-Host -Object '12. Windows Server 2012 R2 Standard + SQL WEB - BYOL' -ForegroundColor 'green' | |
Write-Host -Object '13. Windows Server 2008 R2 Standard + SQL Standard' -ForegroundColor 'green' | |
Write-Host -Object '14. Windows Server 2008 R2 Standard + SQL Standard - BYOL' -ForegroundColor 'green' | |
Write-Host -Object '15. Windows Server 2012 Standard + SQL Standard' -ForegroundColor 'green' | |
Write-Host -Object '16. Windows Server 2012 Standard + SQL Standard - BYOL' -ForegroundColor 'green' | |
Write-Host -Object '17. Windows Server 2012 R2 Standard + SQL Standard' -ForegroundColor 'green' | |
Write-Host -Object '18. Windows Server 2012 R2 Standard + SQL Standard - BYOL' -ForegroundColor 'green' | |
[Int]$osselection = Read-Host -Prompt 'Please enter an option' | |
} | |
Switch( $osselection ){ | |
1 | |
{ | |
$templateselection = 'Server 2008 R2 Standard Template' | |
$osplan = 'Windows Server 2008 R2 Standard' | |
$oscustselection = 'Windows - Win2K8 Licensed' | |
$oscode = 'W2008R2-S' | |
} | |
2 | |
{ | |
$templateselection = 'Server 2008 R2 Standard Template' | |
$osplan = 'Windows Server 2008 R2 Standard - BYOL' | |
$oscustselection = 'Windows - Unlicensed' | |
$oscode = 'W2008R2-S-BYOL' | |
} | |
3 | |
{ | |
$templateselection = 'Server 2012 Standard Template' | |
$osplan = 'Windows Server 2012 Standard' | |
$oscustselection = 'Windows - 2012 Licensed' | |
$oscode = 'W2012-S' | |
} | |
4 | |
{ | |
$templateselection = 'Server 2012 Standard Template' | |
$osplan = 'Windows Server 2012 Standard - BYOL' | |
$oscustselection = 'Windows - Unlicensed' | |
$oscode = 'W2012-S-BYOL' | |
} | |
5 | |
{ | |
$templateselection = 'Server 2012 R2 Standard Template' | |
$osplan = 'Windows Server 2012 R2 Standard' | |
$oscustselection = 'Windows - 2012 R2 Licensed' | |
$oscode = 'W2012R2-S' | |
} | |
6 | |
{ | |
$templateselection = 'Server 2012 R2 Standard Template' | |
$osplan = 'Windows Server 2012 R2 Standard - BYOL' | |
$oscustselection = 'Windows - Unlicensed' | |
$oscode = 'W2012R2-S-BYOL' | |
} | |
7 | |
{ | |
$templateselection = 'Server 2008 R2 Standard Template' | |
$osplan = 'Windows Server 2008 R2 Standard + SQL WEB' | |
$oscustselection = 'Windows - Win2K8 Licensed' | |
$oscode = 'W2008R2-S-WEB' | |
$sql = 'SQL Web' | |
} | |
8 | |
{ | |
$templateselection = 'Server 2008 R2 Standard Template' | |
$osplan = 'Windows Server 2008 R2 Standard + SQL WEB - BYOL' | |
$oscustselection = 'Windows - Unlicensed' | |
$oscode = 'W2008R2-S-WEB-BYOL' | |
$sql = 'SQL Web' | |
} | |
9 | |
{ | |
$templateselection = 'Server 2012 Standard Template' | |
$osplan = 'Windows Server 2012 Standard + SQL WEB' | |
$oscustselection = 'Windows - Unlicensed' | |
$oscode = 'W2012-S-WEB' | |
$sql = 'SQL Web' | |
} | |
10 | |
{ | |
$templateselection = 'Server 2012 Standard Template' | |
$osplan = 'Windows Server 2012 Standard + SQL WEB - BYOL' | |
$oscustselection = 'Windows - Unlicensed' | |
$oscode = 'W2012-S-WEB-BYOL' | |
$sql = 'SQL Web' | |
} | |
11 | |
{ | |
$templateselection = 'Server 2012 R2 Standard Template' | |
$osplan = 'Windows Server 2012 R2 Standard + SQL WEB' | |
$oscustselection = 'Windows - 2012 R2 Licensed' | |
$oscode = 'W2012R2-S-WEB' | |
$sql = 'SQL Web' | |
} | |
12 | |
{ | |
$templateselection = 'Server 2012 R2 Standard Template' | |
$osplan = 'Windows Server 2012 R2 Standard + SQL - BYOL' | |
$oscustselection = 'Windows - Unlicensed' | |
$oscode = 'W2012R2-S-WEB-BYOL' | |
$sql = 'SQL Standard' | |
} | |
13 | |
{ | |
$templateselection = 'Server 2008 R2 Standard Template' | |
$osplan = 'Windows Server 2008 R2 Standard + SQL Standard' | |
$oscustselection = 'Windows - Win2K8 Licensed' | |
$oscode = 'W2008R2-S-STD' | |
$sql = 'SQL Standard' | |
} | |
14 | |
{ | |
$templateselection = 'Server 2008 R2 Standard Template' | |
$osplan = 'Windows Server 2008 R2 Standard + SQL Standard - BYOL' | |
$oscustselection = 'Windows - Unlicensed' | |
$oscode = 'W2008R2-S-STD-BYOL' | |
$sql = 'SQL Standard' | |
} | |
15 | |
{ | |
$templateselection = 'Server 2012 Standard Template' | |
$osplan = 'Windows Server 2012 Standard + SQL Standard' | |
$oscustselection = 'Windows - 2012 Licensed' | |
$oscode = 'W2012-S-STD' | |
$sql = 'SQL Standard' | |
} | |
16 | |
{ | |
$templateselection = 'Server 2012 Standard Template' | |
$osplan = 'Windows Server 2012 Standard + SQL Standard - BYOL' | |
$oscustselection = 'Windows - Unlicensed' | |
$oscode = 'W2012-S-STD-BYOL' | |
$sql = 'SQL Standard' | |
} | |
17 | |
{ | |
$templateselection = 'Server 2012 R2 Standard Template' | |
$osplan = 'Windows Server 2012 R2 Standard + SQL Standard' | |
$oscustselection = 'Windows - 2012 R2 Licensed' | |
$oscode = 'W2012R2-S-STD' | |
$sql = 'SQL Standard' | |
} | |
18 | |
{ | |
$templateselection = 'Server 2012 R2 Standard Template' | |
$osplan = 'Windows Server 2012 R2 Standard + SQL Standard - BYOL' | |
$oscustselection = 'Windows - Unlicensed' | |
$oscode = 'W2012R2-S-STD-BYOL' | |
$sql = 'SQL Standard' | |
} | |
} | |
#Go to BYOL | |
Enter_BYOLExpiry | |
} | |
#BYOL license expiry | |
Function Enter_BYOLExpiry | |
{ | |
if ($oscode -like '*BYOL*') | |
{ | |
[string]$BYOLans = Read-Host -Prompt 'Enter the expiry date for the BYOL license agreement' | |
$BYOLselection = $BYOLans | |
Select_Cluster | |
} | |
else | |
{ | |
#Go to cluster | |
$BYOLselection = 'Not needed' | |
Select_Cluster | |
} | |
} | |
#Cluster menu | |
Function Select_Cluster | |
{ | |
Write-Host Select your cluster | |
$esxcluster = get-cluster | |
$esxclustermenu = @{} | |
for ($i = 1;$i -le $esxcluster.count; $i++) | |
{ | |
Write-Host -Object "$i. $($esxcluster[$i-1])" -ForegroundColor 'green' | |
$esxclustermenu.Add($i,($esxcluster[$i-1])) | |
} | |
[int]$cans = Read-Host -Prompt 'Enter cluster selection' | |
$esxclusterselection = $esxclustermenu.Item($cans) | |
if (($cans -gt $esxcluster.count) -or ($esxclusterselection -eq $null) -or ($cans -eq 0)) | |
{ | |
Write-Host -ForegroundColor Red -Object "Couldn't determine which ESX Cluster you want. Please retry..." | |
Start-Sleep -Seconds 1 | |
Select_Cluster | |
} | |
else | |
{ | |
#Go to select host menu | |
Select_Host | |
} | |
} | |
#Host menu | |
Function Select_Host | |
{ | |
Write-Host Select your host | |
$esxhost = get-vmhost -location "$esxclusterselection" | |
$hostmenu = @{} | |
for ($i = 1;$i -le $esxhost.count; $i++) | |
{ | |
Write-Host -Object "$i. $($esxhost[$i-1]) | memory used GB: $($esxhost.MemoryUsageGB[$i-1]) " -ForegroundColor 'green' | |
$hostmenu.Add($i,($esxhost[$i-1])) | |
} | |
[int]$hans = Read-Host -Prompt 'Enter host selection' | |
$hostselection = $hostmenu.Item($hans) | |
if (($hans -gt $esxhost.count) -or ($hostselection -eq $null) -or ($hans -eq 0)) | |
{ | |
Write-Host -ForegroundColor Red -Object "Couldn't determine which ESX host you want. Please retry..." | |
Start-Sleep -Seconds 1 | |
Select_Host | |
} | |
else | |
{ | |
#Go to select Resource menu | |
Select_Resource | |
} | |
} | |
#Select resource pool | |
Function Select_Resource | |
{ | |
Write-Host Select your resource pool | |
$resourcepool = get-cluster $esxclusterselection | get-resourcepool | |
$resourcemenu = @{} | |
for ($i = 1;$i -le $resourcepool.count; $i++) | |
{ | |
Write-Host -Object "$i. $($resourcepool[$i-1])" -ForegroundColor 'green' | |
$resourcemenu.Add($i,($resourcepool[$i-1])) | |
} | |
[int]$resans = Read-Host -Prompt 'Enter resource pool selection' | |
$resourceselection = $resourcemenu.Item($resans) | |
if (($resans -gt $resourcepool.count) -or ($resourceselection -eq $null) -or ($resans -eq 0)) | |
{ | |
Write-Host -ForegroundColor Red -Object "Couldn't determine which resource pool you want. Please retry..." | |
Start-Sleep -Seconds 1 | |
Select_Host | |
} | |
else | |
{ | |
#Go to select Datastore menu | |
Select_Datastore | |
} | |
} | |
#Datastore menu | |
Function Select_Datastore | |
{ | |
Write-Host Select your datastore | |
$esxdatastore = get-datastore -vmhost $hostselection | |
$datastoremenu = @{} | |
for ($i = 1;$i -le $esxdatastore.count; $i++) | |
{ | |
Write-Host -Object "$i. $($esxdatastore[$i-1])" -ForegroundColor 'green' | |
$datastoremenu.Add($i,($esxdatastore[$i-1])) | |
} | |
[int]$dans = Read-Host -Prompt 'Enter datastore selection' | |
$datastoreselection = $datastoremenu.Item($dans) | |
if (($dans -gt $esxdatastore.count) -or ($datastoreselection -eq $null) -or ($dans -eq 0)) | |
{ | |
Write-Host -ForegroundColor Red -Object "Couldn't determine which datastore you want. Please retry..." | |
Start-Sleep -Seconds 1 | |
Select_Datastore | |
} | |
else | |
{ | |
#Go to select VD Switch menu | |
Select_VDSwitch | |
} | |
} | |
#VD Switch menu | |
Function Select_VDSwitch | |
{ | |
Write-Host Select your VD Switch | |
$esxswitch = get-vdswitch | |
$switchmenu = @{} | |
for ($i = 1;$i -le $esxswitch.count; $i++) | |
{ | |
Write-Host -Object "$i. $($esxswitch[$i-1])" -ForegroundColor 'green' | |
$switchmenu.Add($i,($esxswitch[$i-1])) | |
} | |
[int]$vdsans = Read-Host -Prompt 'Enter VD Switch selection' | |
$switchselection = $switchmenu.Item($vdsans) | |
if (($vdsans -gt $esxswitch.count) -or ($switchselection -eq $null) -or ($vdsans -eq 0)) | |
{ | |
Write-Host -ForegroundColor Red -Object "Couldn't determine which virtual switch you want. Please retry..." | |
Start-Sleep -Seconds 1 | |
Select_VDSwitch | |
} | |
else | |
{ | |
#Go to select Port group menu | |
Select_Portgroup | |
} | |
} | |
#Expand guest HDD | |
Function Expand_GuestHDD | |
{ | |
$time = 600 # seconds, use you actual time in here | |
foreach($i in (1..$time)) | |
{ | |
$percentage = $i / $time | |
$remaining = New-TimeSpan -Seconds ($time - $i) | |
$message = '{0:p0} complete, remaining time {1}' -f $percentage, $remaining | |
Write-Progress -Activity $message -Status "Sysprep is doing it's stuff..." -PercentComplete ($percentage * 100) | |
Start-Sleep -Seconds 1 | |
} | |
Write-Host Expanding guest primary partition -ForegroundColor 'red' | |
Get-HardDisk -vm $nameselection | Set-HardDisk -CapacityGB $bundleselection.Storage -ResizeGuestPartition -GuestUser Administrator -GuestPassword $password -confirm:$false | |
} | |
#Port group menu | |
Function Select_Portgroup | |
{ | |
Write-Host Select your port group | |
$esxportgroup = Get-VirtualPortGroup -VirtualSwitch $switchselection | |
$portgroupmenu = @{} | |
for ($i = 1;$i -le $esxportgroup.count; $i++) | |
{ | |
Write-Host -Object "$i. $($esxportgroup[$i-1])" -ForegroundColor 'green' | |
$portgroupmenu.Add($i,($esxportgroup[$i-1])) | |
} | |
[int]$pgsans = Read-Host -Prompt 'Enter port group selection' | |
$portgroupselection = $portgroupmenu.Item($pgsans) | |
if (($pgsans -gt $esxportgroup.count) -or ($portgroupselection -eq $null) -or ($pgsans -eq 0)) | |
{ | |
Write-Host -ForegroundColor Red -Object "Couldn't determine which port group you want. Please retry..." | |
Start-Sleep -Seconds 1 | |
Select_Portgroup | |
} | |
else | |
{ | |
#Go to select short backup | |
Select_ShortBackups | |
} | |
} | |
#Select SQL | |
Function Select_SQL | |
{ | |
if ($sql -eq 'SQL Standard') | |
{ | |
Write-Host Mounting SQL 2008 R2 Standard CD -ForegroundColor 'red' | |
Get-CDDrive -VM $nameselection | Set-CDDrive -ISOpath '[ldr-ds001-7500] Software/en_sql_server_2008_r2_standard_x86_x64_ia64_dvd_521546.iso' -Connected $true -startconnected $true -confirm:$false | |
Set_Annotations | |
} | |
elseif ($sql -eq 'SQL Web') | |
{ | |
Write-Host Mounting SQL 2008 R2 Web CD -ForegroundColor 'red' | |
Get-CDDrive -VM $nameselection | Set-CDDrive -ISOpath '[ldr-ds001-7500] Software/en_sql_server_2008_r2_web_x86_x64_ia64_dvd_522515.iso' -Connected $true -startconnected $true -confirm:$false | |
Set_Annotations | |
} | |
else | |
{ | |
Set_Annotations | |
} | |
} | |
#Short backups | |
Function Select_ShortBackups | |
{ | |
Write-Host Please the short backup plan | |
[int]$sbackupselection = 0 | |
while ( $sbackupselection -lt 1 -or $sbackupselection -gt 5 ) | |
{ | |
Write-Host -Object '1. EB-1W' -ForegroundColor 'green' | |
Write-Host -Object '2. EB-2W' -ForegroundColor 'green' | |
Write-Host -Object '3. EB-1M' -ForegroundColor 'green' | |
Write-Host -Object '4. EB-3M' -ForegroundColor 'green' | |
Write-Host -Object '5. EB-6M' -ForegroundColor 'green' | |
Write-Host -Object '5. EB-1Y' -ForegroundColor 'green' | |
[Int]$sbackupselection = Read-Host -Prompt 'Please enter an option' | |
} | |
Switch( $sbackupselection ){ | |
1 | |
{ | |
$vmfolder = 'vm' | |
$shortbackup = 'EB-1W' | |
} | |
2 | |
{ | |
$vmfolder = 'EB-2W' | |
$shortbackup = 'EB-2W' | |
} | |
3 | |
{ | |
$vmfolder = 'EB-1M' | |
$shortbackup = 'EB-1M' | |
} | |
4 | |
{ | |
$vmfolder = 'EB-3M' | |
$shortbackup = 'EB-3M' | |
} | |
5 | |
{ | |
$vmfolder = 'EB-6M' | |
$shortbackup = 'EB-6M' | |
} | |
6 | |
{ | |
$vmfolder = 'EB-1Y' | |
$shortbackup = 'EB-1Y' | |
} | |
} | |
#Goto Select_LongBackups | |
Select_LongBackups | |
} | |
#Long backups | |
Function Select_LongBackups | |
{ | |
if ($shortbackup -ne 'EB-1W') | |
{ | |
Confirm_Details | |
} | |
else | |
{ | |
Write-Host Please the long backup plan | |
[int]$lbackupselection = 0 | |
while ( $lbackupselection -lt 1 -or $lbackupselection -gt 5 ) | |
{ | |
Write-Host -Object '1. None' -ForegroundColor 'green' | |
Write-Host -Object '2. HFB-1W' -ForegroundColor 'green' | |
Write-Host -Object '3. HFB-2W' -ForegroundColor 'green' | |
Write-Host -Object '4. HFB-1M' -ForegroundColor 'green' | |
[Int]$lbackupselection = Read-Host -Prompt 'Please enter an option' | |
} | |
Switch( $lbackupselection ){ | |
1 | |
{ | |
'Write-host No long backup plans selected' | |
$longbackup = 'None' | |
} | |
2 | |
{ | |
$vmfolder = 'HFB-1W' | |
$longbackup = 'HFB-1W' | |
} | |
3 | |
{ | |
$vmfolder = 'HFB-2W' | |
$longbackup = 'HFB-2W' | |
} | |
4 | |
{ | |
$vmfolder = 'HFB-1M' | |
$longbackup = 'HFB-1M' | |
} | |
} | |
} | |
#Goto Confirm_Details | |
Confirm_Details | |
} | |
#> | |
#Confirm details | |
Function Confirm_Details | |
{ | |
$bundleplan = $bundleselection.Plan | |
Clear-Host | |
Write-Host -Object "Cluster: $esxclusterselection" | |
Write-Host -Object "Host: $hostselection" | |
Write-Host -Object "Resource pool: $resourceselection" | |
Write-Host -Object "Bundle: $bundleplan" | |
Write-Host -Object "OS Plan: $osplan" | |
Write-Host -Object "Short Backup: $shortbackup" | |
Write-Host -Object "Long Backup: $longbackup" | |
Write-Host -Object "BYOL expiry: $BYOLselection" | |
Write-Host -Object "Customisation image: $oscustselection" | |
Write-Host -Object "Datastore: $datastoreselection" | |
Write-Host -Object "Port group: $portgroupselection" | |
Write-Host -Object "I.P: $ipselection" | |
Write-Host -Object "Subnet: $subselection" | |
Write-Host -Object "Gateway: $gateselection" | |
Write-Host -Object "Confirm the above inputs are correct. If they aren't select NO and you will be sent back to the start of the script to start again" -ForegroundColor 'red' | |
[int]$confirmselection = 0 | |
while ( $confirmselection -lt 1 -or $confirmselection -gt 2 ) | |
{ | |
Write-Host -Object '1. Yes the above is correct' -ForegroundColor 'green' | |
Write-Host -Object '2. No something in the above is wrong' -ForegroundColor 'green' | |
[Int]$confirmselection = Read-Host -Prompt 'Please enter an option' | |
} | |
Switch( $confirmselection ){ | |
1 | |
{ | |
$date = Get-Date | |
Build_VM | |
} | |
2 | |
{ | |
Enter_Ticket | |
} | |
} | |
} | |
#Set annotations | |
Function Set_Annotations | |
{ | |
Write-Host Setting metadata VM -ForegroundColor 'red' | |
Set-Annotation -Entity $nameselection -customattribute guestPlan -Value $bundleselection.Plan | |
Set-Annotation -Entity $nameselection -customattribute backupsShort -Value $shortbackup | |
Set-Annotation -Entity $nameselection -customattribute backupsLong -Value $longbackup | |
Set-Annotation -Entity $nameselection -customattribute createdDate -Value $date | |
Set-Annotation -Entity $nameselection -customattribute osPlan -Value $oscode | |
Set-Annotation -Entity $nameselection -customattribute byolExpiration -Value $BYOLselection | |
Stop-Transcript | |
Email_deets | |
} | |
#Build VM | |
Function Build_VM | |
{ | |
Write-Host Removing any old non-persistant customization specs -ForegroundColor 'red' | |
#Get-OSCustomizationSpec -type nonpersistent | Remove-OSCustomizationSpec -confirm:$false | |
Write-Host Creating a clone of the original customisation spec -ForegroundColor 'red' | |
New-OSCustomizationSpec -Name $nameselection -OSCustomizationSpec $oscustselection -type NonPersistent | |
Write-Host Updating the cloned customisation spec with new VM specific settings -ForegroundColor 'red' | |
Set-OSCustomizationSpec -OSCustomizationSpec $nameselection -adminpassword $password -OrgName $orgnameselection -fullname $nameselection | |
Get-OSCustomizationSpec -name $nameselection | | |
Get-OSCustomizationNicMapping | | |
Set-OSCustomizationNicMapping -IPMode UseStaticIP -IpAddress $ipselection -subnetmask $subselection -DefaultGateway $gateselection -DNS 8.8.8.8 -Confirm:$false | |
Write-Host Starting to build the VM -ForegroundColor 'red' | |
new-vm -name $nameselection -vmhost $hostelection -location $vmfolder -resourcepool $resourceselection -template $templateselection -datastore $datastoreselection -OSCustomizationSpec $nameselection | |
Write-Host Configuring the VM CPU and memory based on the bundle selection -ForegroundColor 'red' | |
Set-VM -vm $nameselection -MemoryGB $bundleselection.Mem -numcpu $bundleselection.vCPU -confirm:$false | |
Write-Host Configuring network adapter -ForegroundColor 'red' | |
Get-VM -name $nameselection | | |
Get-NetworkAdapter | | |
Set-NetworkAdapter -networkname $portgroupselection -StartConnected:$true -confirm:$false | |
Write-Host Configuring HDD size -ForegroundColor 'red' | |
Get-VM -name $nameselection | | |
Get-HardDisk | | |
Set-HardDisk -CapacityGB $bundleselection.Storage -confirm:$false | |
Write-Host Configuring VNC -ForegroundColor 'red' | |
Set_VMX | |
Write-Host Starting the VM -ForegroundColor 'red' | |
Start-VM -VM $nameselection | |
Write-Host -ForegroundColor red -Object 'Please be aware image customisation (sysprep) at the vmware level my take up to 10 minutes to finish as it involves a few reboots. The script is currently paused for 10 minutes waiting for sysprep to finish configuration so the new password can be used to expand the guest OS primary drive' | |
Expand_GuestHDD | |
Select_SQL | |
#Open-VMConsoleWindow -vm $nameselection | |
} | |
#Exit | |
Function Select_Exit | |
{ | |
#Get-OSCustomizationSpec -type nonpersistent | Remove-OSCustomizationSpec -confirm:$false | |
Exit | |
} | |
Enter_Ticket | |
Select_Exit |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment