Advertisement
Guest User

Untitled

a guest
Aug 16th, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.35 KB | None | 0 0
  1. $logfiledate = get-date -format "yyyy-MM-dd %h-m-ss"
  2. $logfile = "C:\Admin\Logs\Deployment Transcript $logfiledate.txt"
  3. start-transcript -path $logfile
  4. if ( (Get-PSSnapin -Name "VMware.VimAutomation.Core" -ErrorAction SilentlyContinue) -eq $null )
  5. {
  6. Add-PsSnapin "VMware.VimAutomation.Core"
  7. }
  8. Write-host Connecting to server -foregroundcolor "magenta"
  9. connect-viserver blah -ErrorAction Stop
  10.  
  11. #Generate random password
  12. Add-Type -Assembly System.Web
  13. $length = 12
  14. $numberOfNonAlphanumericCharacters = 4
  15. $password = [Web.Security.Membership]::GeneratePassword($length,$numberOfNonAlphanumericCharacters)
  16.  
  17. #Set VNC in the vmx
  18. Function Set_VMX
  19. {
  20. #Generating VNC port
  21. [int]$vncport = get-content vncport.txt
  22. $vncport++
  23. $vncport | Out-file VNCPort.txt
  24. Get-VM -name $nameselection | Get-AdvancedSetting -name "RemoteDisplay.vnc.password" | Set-AdvancedSetting -Value $password -confirm:$false
  25. Get-VM -name $nameselection | Get-AdvancedSetting -name "RemoteDisplay.vnc.port" | Set-AdvancedSetting -Value $vncport -confirm:$false
  26. }
  27.  
  28. #Email configuration details
  29. Function Email_deets
  30. {
  31. $uuid = get-vm -name $nameselection | %{(Get-View $_.Id).config.uuid}
  32. $guid = get-vm -name $nameselection | Get-View | select -ExpandProperty MoRef
  33. $bundleplan = $bundleselection.Plan
  34. [int]$vncportout = get-content vncport.txt
  35. Send-MailMessage -smtpserver blah -to "blah" -from "blah" -subject "[iseek-track #$ticketselection] Configuration details for VM: $nameselection" -attachment $logfile -body "Administrator username: Administrator
  36. Password: $password
  37. Bundle: $bundleplan
  38. Template: $osplan
  39. OSCode: $oscode
  40. BYOL expiry: $BYOLselection
  41. Short backup: $shortbackup
  42. Long backup: $longbackup
  43. Datastore: $datastoreselection
  44. Port group: $portgroupselection
  45. I.P: $ipselection
  46. Subnet: $subselection
  47. Gateway: $gateselection
  48. VNC Port: $vncportout
  49. VNC Password: $password
  50. UUID: $uuid
  51. GUID: $guid
  52. "
  53. }
  54.  
  55. #Ticket #
  56. Function Enter_Ticket
  57. {
  58. Clear
  59. [int]$tans = Read-Host 'Enter the ticket #'
  60. $ticketselection = $tans
  61.  
  62. if (($ticketselection -eq $null) -or ($tans -eq 0))
  63. {
  64. Write-Host -ForegroundColor Red "Couldn't determine the ticket #. Please retry..."
  65. Start-Sleep -s 1
  66. Enter_Ticket
  67. }
  68.  
  69. else
  70. {
  71. #Goto Name
  72. Select_Name
  73. }
  74. }
  75.  
  76. #Name the VM
  77. Function Select_Name
  78. {
  79. [string]$nans = Read-Host 'Enter the name you want to assign to the VM'
  80. $nameselection = $nans
  81.  
  82. if (($nameselection -eq $null) -or ($nans -eq 0))
  83. {
  84. Write-Host -ForegroundColor Red "Couldn't determine the name you want. Please retry..."
  85. Start-Sleep -s 1
  86. Select_Name
  87. }
  88.  
  89. else
  90. {
  91. #Go to Organisation
  92. Select_OrgName
  93. }
  94. }
  95.  
  96. #Organisation name
  97. Function Select_OrgName
  98. {
  99. [string]$onans = Read-Host 'Enter the Organisation name you want to assign to the VM'
  100. $orgnameselection = $onans
  101.  
  102. if (($orgnameselection -eq $null) -or ($onans -eq 0))
  103. {
  104. Write-Host -ForegroundColor Red "Couldn't determine the name you want. Please retry..."
  105. Start-Sleep -s 1
  106. Select_Name
  107. }
  108.  
  109. else
  110. {
  111. #Go to I.P
  112. Select_IP
  113. }
  114. }
  115.  
  116. #I.P
  117. Function Select_IP
  118. {
  119. [string]$ipans = Read-Host 'Enter the I.P you want to assign to the VM'
  120. $ipselection = $ipans
  121.  
  122. if (($ipselection -eq $null) -or ($ipans -eq 0))
  123. {
  124. Write-Host -ForegroundColor Red "Couldn't determine the I.P you want. Please retry..."
  125. Start-Sleep -s 1
  126. Select_Name
  127. }
  128.  
  129. else
  130. {
  131. #Go to subnet
  132. Select_Subnet
  133. }
  134. }
  135.  
  136. #subnet
  137. Function Select_Subnet
  138. {
  139. [string]$subans = Read-Host 'Enter the subnet you want to assign to the VM'
  140. $subselection = $subans
  141.  
  142. if (($subselection -eq $null) -or ($subans -eq 0))
  143. {
  144. Write-Host -ForegroundColor Red "Couldn't determine the subnet you want. Please retry..."
  145. Start-Sleep -s 1
  146. Select_Name
  147. }
  148.  
  149. else
  150. {
  151. #Go to gateway
  152. Select_Gateway
  153. }
  154. }
  155.  
  156. #gateway
  157. Function Select_Gateway
  158. {
  159. [string]$gateans = Read-Host 'Enter the gateway you want to assign to the VM'
  160. $gateselection = $gateans
  161.  
  162. if (($gateselection -eq $null) -or ($gateans -eq 0))
  163. {
  164. Write-Host -ForegroundColor Red "Couldn't determine the gateway you want. Please retry..."
  165. Start-Sleep -s 1
  166. Select_Name
  167. }
  168.  
  169. else
  170. {
  171. #Go to bundle
  172. Select_Bundle
  173. }
  174. }
  175.  
  176. #Bundle menu
  177. Function Select_Bundle
  178. {
  179. $bundle = import-csv bundles.csv
  180. Write-host Select your bundle
  181. $bundlemenu = @{}
  182. for ($i=1;$i -le $bundle.count; $i++) {
  183. Write-Host "$i. $($bundle[$i-1])" -foregroundcolor "green"
  184. $bundlemenu.Add($i,($bundle[$i-1]))
  185. }
  186. [int]$bundleans = Read-Host 'Enter bundle selection'
  187. $bundleselection = $bundlemenu.Item($bundleans)
  188. if (($bundleans -gt $bundle.count) -or ($bundleselection -eq $null) -or ($bundleans -eq 0))
  189. {
  190. Write-Host -ForegroundColor Red "Couldn't determine which bundle you want. Please retry..."
  191. Start-Sleep -s 1
  192. Select_Bundle
  193. }
  194.  
  195. else
  196. {
  197. #Go to OS plan
  198. Select_OSPlan
  199. }
  200. }
  201.  
  202. #Select OS Plan
  203. Function Select_OSPlan
  204. {
  205. Write-host Please select the OS plan
  206. [int]$osselection = 0
  207. while ( $osselection -lt 1 -or $osselection -gt 18 ){
  208. Write-host "1. Windows Server 2008 R2 Standard" -foregroundcolor "green"
  209. Write-host "2. Windows Server 2008 R2 Standard - BYOL" -foregroundcolor "green"
  210. Write-host "3. Windows Server 2012 Standard" -foregroundcolor "green"
  211. Write-host "4. Windows Server 2012 Standard - BYOL" -foregroundcolor "green"
  212. Write-host "5. Windows Server 2012 R2 Standard" -foregroundcolor "green"
  213. Write-host "6. Windows Server 2012 R2 Standard - BYOL" -foregroundcolor "green"
  214. Write-host "7. Windows Server 2008 R2 Standard + SQL WEB" -foregroundcolor "green"
  215. Write-host "8. Windows Server 2008 R2 Standard + SQL WEB - BYOL" -foregroundcolor "green"
  216. Write-host "9. Windows Server 2012 Standard + SQL WEB" -foregroundcolor "green"
  217. Write-host "10. Windows Server 2012 Standard + SQL WEB - BYOL" -foregroundcolor "green"
  218. Write-host "11. Windows Server 2012 R2 Standard + SQL WEB" -foregroundcolor "green"
  219. Write-host "12. Windows Server 2012 R2 Standard + SQL WEB - BYOL" -foregroundcolor "green"
  220. Write-host "13. Windows Server 2008 R2 Standard + SQL Standard" -foregroundcolor "green"
  221. Write-host "14. Windows Server 2008 R2 Standard + SQL Standard - BYOL" -foregroundcolor "green"
  222. Write-host "15. Windows Server 2012 Standard + SQL Standard" -foregroundcolor "green"
  223. Write-host "16. Windows Server 2012 Standard + SQL Standard - BYOL" -foregroundcolor "green"
  224. Write-host "17. Windows Server 2012 R2 Standard + SQL Standard" -foregroundcolor "green"
  225. Write-host "18. Windows Server 2012 R2 Standard + SQL Standard - BYOL" -foregroundcolor "green"
  226.  
  227. [Int]$osselection = read-host "Please enter an option" }
  228. Switch( $osselection ){
  229. 1{ $templateselection = "Server 2008 R2 Standard Template"
  230. $osplan = "Windows Server 2008 R2 Standard"
  231. $oscustselection = "Windows - Win2K8 Licensed"
  232. $oscode = "W2008R2-S"
  233. }
  234. 2{ $templateselection = "Server 2008 R2 Standard Template"
  235. $osplan = "Windows Server 2008 R2 Standard - BYOL"
  236. $oscustselection = "Windows - Unlicensed"
  237. $oscode = "W2008R2-S-BYOL"
  238. }
  239. 3{ $templateselection = "Server 2012 Standard Template"
  240. $osplan = "Windows Server 2012 Standard"
  241. $oscustselection = "Windows - 2012 Licensed"
  242. $oscode = "W2012-S"
  243. }
  244. 4{ $templateselection = "Server 2012 Standard Template"
  245. $osplan = "Windows Server 2012 Standard - BYOL"
  246. $oscustselection = "Windows - Unlicensed"
  247. $oscode = "W2012-S-BYOL"
  248. }
  249. 5{ $templateselection = "Server 2012 R2 Standard Template"
  250. $osplan = "Windows Server 2012 R2 Standard"
  251. $oscustselection = "Windows - 2012 R2 Licensed"
  252. $oscode = "W2012R2-S"
  253. }
  254. 6{ $templateselection = "Server 2012 R2 Standard Template"
  255. $osplan = "Windows Server 2012 R2 Standard - BYOL"
  256. $oscustselection = "Windows - Unlicensed"
  257. $oscode = "W2012R2-S-BYOL"
  258. }
  259. 7{ $templateselection = "Server 2008 R2 Standard Template"
  260. $osplan = "Windows Server 2008 R2 Standard + SQL WEB"
  261. $oscustselection = "Windows - Win2K8 Licensed"
  262. $oscode = "W2008R2-S-WEB"
  263. $sql = "SQL Web"
  264. }
  265. 8{ $templateselection = "Server 2008 R2 Standard Template"
  266. $osplan = "Windows Server 2008 R2 Standard + SQL WEB - BYOL"
  267. $oscustselection = "Windows - Unlicensed"
  268. $oscode = "W2008R2-S-WEB-BYOL"
  269. $sql = "SQL Web"
  270. }
  271. 9{ $templateselection = "Server 2012 Standard Template"
  272. $osplan = "Windows Server 2012 Standard + SQL WEB"
  273. $oscustselection = "Windows - Unlicensed"
  274. $oscode = "W2012-S-WEB"
  275. $sql = "SQL Web"
  276. }
  277. 10{ $templateselection = "Server 2012 Standard Template"
  278. $osplan = "Windows Server 2012 Standard + SQL WEB - BYOL"
  279. $oscustselection = "Windows - Unlicensed"
  280. $oscode = "W2012-S-WEB-BYOL"
  281. $sql = "SQL Web"
  282. }
  283. 11{ $templateselection = "Server 2012 R2 Standard Template"
  284. $osplan = "Windows Server 2012 R2 Standard + SQL WEB"
  285. $oscustselection = "Windows - 2012 R2 Licensed"
  286. $oscode = "W2012R2-S-WEB"
  287. $sql = "SQL Web"
  288. }
  289. 12{ $templateselection = "Server 2012 R2 Standard Template"
  290. $osplan = "Windows Server 2012 R2 Standard + SQL - BYOL"
  291. $oscustselection = "Windows - Unlicensed"
  292. $oscode = "W2012R2-S-WEB-BYOL"
  293. $sql = "SQL Standard"
  294. }
  295. 13{ $templateselection = "Server 2008 R2 Standard Template"
  296. $osplan = "Windows Server 2008 R2 Standard + SQL Standard"
  297. $oscustselection = "Windows - Win2K8 Licensed"
  298. $oscode = "W2008R2-S-STD"
  299. $sql = "SQL Standard"
  300. }
  301. 14{ $templateselection = "Server 2008 R2 Standard Template"
  302. $osplan = "Windows Server 2008 R2 Standard + SQL Standard - BYOL"
  303. $oscustselection = "Windows - Unlicensed"
  304. $oscode = "W2008R2-S-STD-BYOL"
  305. $sql = "SQL Standard"
  306. }
  307. 15{ $templateselection = "Server 2012 Standard Template"
  308. $osplan = "Windows Server 2012 Standard + SQL Standard"
  309. $oscustselection = "Windows - 2012 Licensed"
  310. $oscode = "W2012-S-STD"
  311. $sql = "SQL Standard"
  312. }
  313. 16{ $templateselection = "Server 2012 Standard Template"
  314. $osplan = "Windows Server 2012 Standard + SQL Standard - BYOL"
  315. $oscustselection = "Windows - Unlicensed"
  316. $oscode = "W2012-S-STD-BYOL"
  317. $sql = "SQL Standard"
  318. }
  319. 17{ $templateselection = "Server 2012 R2 Standard Template"
  320. $osplan = "Windows Server 2012 R2 Standard + SQL Standard"
  321. $oscustselection = "Windows - 2012 R2 Licensed"
  322. $oscode = "W2012R2-S-STD"
  323. $sql = "SQL Standard"
  324. }
  325. 18{ $templateselection = "Server 2012 R2 Standard Template"
  326. $osplan = "Windows Server 2012 R2 Standard + SQL Standard - BYOL"
  327. $oscustselection = "Windows - Unlicensed"
  328. $oscode = "W2012R2-S-STD-BYOL"
  329. $sql = "SQL Standard"
  330. }
  331. }
  332. #Go to BYOL
  333. Enter_BYOLExpiry
  334. }
  335.  
  336. #BYOL license expiry
  337. Function Enter_BYOLExpiry
  338. {
  339. if ($oscode -like "*BYOL*")
  340. {
  341. [string]$BYOLans = Read-Host 'Enter the expiry date for the BYOL license agreement'
  342. $BYOLselection = $BYOLans
  343. Select_Cluster
  344. }
  345. else
  346. {
  347. #Go to cluster
  348. $BYOLselection = "Not needed"
  349. Select_Cluster
  350. }
  351. }
  352.  
  353. #Cluster menu
  354. Function Select_Cluster
  355. {
  356. Write-host Select your cluster
  357. $esxcluster = get-cluster
  358. $esxclustermenu = @{}
  359. for ($i=1;$i -le $esxcluster.count; $i++) {
  360. Write-Host "$i. $($esxcluster[$i-1])" -foregroundcolor "green"
  361. $esxclustermenu.Add($i,($esxcluster[$i-1]))
  362. }
  363.  
  364. [int]$cans = Read-Host 'Enter cluster selection'
  365. $esxclusterselection = $esxclustermenu.Item($cans)
  366.  
  367. if (($cans -gt $esxcluster.count) -or ($esxclusterselection -eq $null) -or ($cans -eq 0))
  368. {
  369. Write-Host -ForegroundColor Red "Couldn't determine which ESX Cluster you want. Please retry..."
  370. Start-Sleep -s 1
  371. Select_Cluster
  372. }
  373.  
  374. else
  375. {
  376. #Go to select host menu
  377. Select_Host
  378. }
  379. }
  380.  
  381. #Host menu
  382. Function Select_Host
  383. {
  384. Write-host Select your host
  385. $esxhost = get-vmhost -location "$esxclusterselection"
  386. $hostmenu = @{}
  387. for ($i=1;$i -le $esxhost.count; $i++) {
  388. Write-Host "$i. $($esxhost[$i-1]) | memory used GB: $($esxhost.MemoryUsageGB[$i-1]) " -foregroundcolor "green"
  389. $hostmenu.Add($i,($esxhost[$i-1]))
  390. }
  391.  
  392. [int]$hans = Read-Host 'Enter host selection'
  393. $hostselection = $hostmenu.Item($hans)
  394.  
  395. if (($hans -gt $esxhost.count) -or ($hostselection -eq $null) -or ($hans -eq 0))
  396. {
  397. Write-Host -ForegroundColor Red "Couldn't determine which ESX host you want. Please retry..."
  398. Start-Sleep -s 1
  399. Select_Host
  400. }
  401.  
  402. else
  403. {
  404. #Go to select Resource menu
  405. Select_Resource
  406. }
  407. }
  408.  
  409. #Select resource pool
  410. Function Select_Resource
  411. {
  412. Write-host Select your resource pool
  413. $resourcepool = get-cluster $esxclusterselection | get-resourcepool
  414. $resourcemenu = @{}
  415. for ($i=1;$i -le $resourcepool.count; $i++) {
  416. Write-Host "$i. $($resourcepool[$i-1])" -foregroundcolor "green"
  417. $resourcemenu.Add($i,($resourcepool[$i-1]))
  418. }
  419.  
  420. [int]$resans = Read-Host 'Enter resource pool selection'
  421. $resourceselection = $resourcemenu.Item($resans)
  422.  
  423. if (($resans -gt $resourcepool.count) -or ($resourceselection -eq $null) -or ($resans -eq 0))
  424. {
  425. Write-Host -ForegroundColor Red "Couldn't determine which resource pool you want. Please retry..."
  426. Start-Sleep -s 1
  427. Select_Host
  428. }
  429.  
  430. else
  431. {
  432. #Go to select Datastore menu
  433. Select_Datastore
  434. }
  435. }
  436.  
  437. #Datastore menu
  438. Function Select_Datastore
  439. {
  440. Write-host Select your datastore
  441. $esxdatastore = get-datastore -vmhost $hostselection
  442. $datastoremenu = @{}
  443. for ($i=1;$i -le $esxdatastore.count; $i++) {
  444. Write-Host "$i. $($esxdatastore[$i-1])" -foregroundcolor "green"
  445. $datastoremenu.Add($i,($esxdatastore[$i-1]))
  446. }
  447.  
  448. [int]$dans = Read-Host 'Enter datastore selection'
  449. $datastoreselection = $datastoremenu.Item($dans)
  450.  
  451. if (($dans -gt $esxdatastore.count) -or ($datastoreselection -eq $null) -or ($dans -eq 0))
  452. {
  453. Write-Host -ForegroundColor Red "Couldn't determine which datastore you want. Please retry..."
  454. Start-Sleep -s 1
  455. Select_Datastore
  456. }
  457.  
  458. else
  459. {
  460. #Go to select VD Switch menu
  461. Select_VDSwitch
  462. }
  463. }
  464.  
  465. #VD Switch menu
  466. Function Select_VDSwitch
  467. {
  468. Write-host Select your VD Switch
  469. $esxswitch = get-vdswitch
  470. $switchmenu = @{}
  471. for ($i=1;$i -le $esxswitch.count; $i++) {
  472. Write-Host "$i. $($esxswitch[$i-1])" -foregroundcolor "green"
  473. $switchmenu.Add($i,($esxswitch[$i-1]))
  474. }
  475.  
  476. [int]$vdsans = Read-Host 'Enter VD Switch selection'
  477. $switchselection = $switchmenu.Item($vdsans)
  478.  
  479. if (($vdsans -gt $esxswitch.count) -or ($switchselection -eq $null) -or ($vdsans -eq 0))
  480. {
  481. Write-Host -ForegroundColor Red "Couldn't determine which virtual switch you want. Please retry..."
  482. Start-Sleep -s 1
  483. Select_VDSwitch
  484. }
  485.  
  486. else
  487. {
  488. #Go to select Port group menu
  489. Select_Portgroup
  490. }
  491. }
  492.  
  493. #Expand guest HDD
  494. Function Expand_GuestHDD
  495. {
  496. $time = 600 # seconds, use you actual time in here
  497. foreach($i in (1..$time)) {
  498. $percentage = $i / $time
  499. $remaining = New-TimeSpan -Seconds ($time - $i)
  500. $message = "{0:p0} complete, remaining time {1}" -f $percentage, $remaining
  501. Write-Progress -Activity $message -status "Sysprep is doing it's stuff..." -PercentComplete ($percentage * 100)
  502. Start-Sleep 1
  503. }
  504. Write-host Expanding guest primary partition -foregroundcolor "red"
  505. Get-HardDisk -vm $nameselection | Set-HardDisk -CapacityGB $bundleselection.Storage -ResizeGuestPartition -GuestUser Administrator -GuestPassword $password -confirm:$false
  506. }
  507.  
  508. #Port group menu
  509. Function Select_Portgroup
  510. {
  511. Write-host Select your port group
  512. $esxportgroup = Get-VirtualPortGroup -VirtualSwitch $switchselection
  513. $portgroupmenu = @{}
  514. for ($i=1;$i -le $esxportgroup.count; $i++) {
  515. Write-Host "$i. $($esxportgroup[$i-1])" -foregroundcolor "green"
  516. $portgroupmenu.Add($i,($esxportgroup[$i-1]))
  517. }
  518.  
  519. [int]$pgsans = Read-Host 'Enter port group selection'
  520. $portgroupselection = $portgroupmenu.Item($pgsans)
  521.  
  522. if (($pgsans -gt $esxportgroup.count) -or ($portgroupselection -eq $null) -or ($pgsans -eq 0))
  523. {
  524. Write-Host -ForegroundColor Red "Couldn't determine which port group you want. Please retry..."
  525. Start-Sleep -s 1
  526. Select_Portgroup
  527. }
  528.  
  529. else
  530. {
  531. #Go to select short backup
  532. Select_ShortBackups
  533. }
  534. }
  535.  
  536. #Select SQL
  537. Function Select_SQL
  538. {
  539. if ($sql -eq "SQL Standard")
  540. {
  541. Write-host Mounting SQL 2008 R2 Standard CD -foregroundcolor "red"
  542. 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
  543. Set_Annotations
  544. }
  545.  
  546. elseif ($sql -eq "SQL Web")
  547. {
  548. Write-host Mounting SQL 2008 R2 Web CD -foregroundcolor "red"
  549. 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
  550. Set_Annotations
  551. }
  552. else
  553. {
  554. Set_Annotations
  555. }
  556. }
  557.  
  558. #Short backups
  559. Function Select_ShortBackups
  560. {
  561. Write-host Please the short backup plan
  562. [int]$sbackupselection = 0
  563. while ( $sbackupselection -lt 1 -or $sbackupselection -gt 5 ){
  564. Write-host "1. EB-1W" -foregroundcolor "green"
  565. Write-host "2. EB-2W" -foregroundcolor "green"
  566. Write-host "3. EB-1M" -foregroundcolor "green"
  567. Write-host "4. EB-3M" -foregroundcolor "green"
  568. Write-host "5. EB-6M" -foregroundcolor "green"
  569. Write-host "5. EB-1Y" -foregroundcolor "green"
  570. [Int]$sbackupselection = read-host "Please enter an option" }
  571. Switch( $sbackupselection ){
  572. 1{ $vmfolder = "vm"
  573. $shortbackup = "EB-1W"
  574. }
  575. 2{ $vmfolder = "EB-2W"
  576. $shortbackup = "EB-2W"
  577. }
  578. 3{ $vmfolder = "EB-1M"
  579. $shortbackup = "EB-1M"
  580. }
  581. 4{ $vmfolder = "EB-3M"
  582. $shortbackup = "EB-3M"
  583. }
  584. 5{ $vmfolder = "EB-6M"
  585. $shortbackup = "EB-6M"
  586. }
  587. 6{ $vmfolder = "EB-1Y"
  588. $shortbackup = "EB-1Y"
  589. }
  590. }
  591. #Goto Select_LongBackups
  592. Select_LongBackups
  593. }
  594.  
  595. #Long backups
  596. Function Select_LongBackups
  597. {
  598. if ($shortbackup -ne "EB-1W") {
  599. Confirm_Details
  600. }
  601. else {
  602. Write-host Please the long backup plan
  603. [int]$lbackupselection = 0
  604. while ( $lbackupselection -lt 1 -or $lbackupselection -gt 5 ){
  605. Write-host "1. None" -foregroundcolor "green"
  606. Write-host "2. HFB-1W" -foregroundcolor "green"
  607. Write-host "3. HFB-2W" -foregroundcolor "green"
  608. Write-host "4. HFB-1M" -foregroundcolor "green"
  609. [Int]$lbackupselection = read-host "Please enter an option" }
  610. Switch( $lbackupselection ){
  611. 1{ "Write-host No long backup plans selected"
  612. $longbackup = "None"
  613. }
  614. 2{ $vmfolder = "HFB-1W"
  615. $longbackup = "HFB-1W"
  616. }
  617. 3{ $vmfolder = "HFB-2W"
  618. $longbackup = "HFB-2W"
  619. }
  620. 4{ $vmfolder = "HFB-1M"
  621. $longbackup = "HFB-1M"
  622. }
  623. }
  624. }
  625. #Goto Confirm_Details
  626. Confirm_Details
  627. }
  628. #>
  629.  
  630. #Confirm details
  631. Function Confirm_Details
  632. {
  633. $bundleplan = $bundleselection.Plan
  634. clear
  635. Write-host "Cluster: $esxclusterselection"
  636. Write-host "Host: $hostselection"
  637. Write-host "Resource pool: $resourceselection"
  638. Write-host "Bundle: $bundleplan"
  639. Write-host "OS Plan: $osplan"
  640. Write-host "Short Backup: $shortbackup"
  641. Write-host "Long Backup: $longbackup"
  642. Write-host "BYOL expiry: $BYOLselection"
  643. Write-host "Customisation image: $oscustselection"
  644. Write-host "Datastore: $datastoreselection"
  645. Write-host "Port group: $portgroupselection"
  646. Write-host "I.P: $ipselection"
  647. Write-host "Subnet: $subselection"
  648. Write-host "Gateway: $gateselection"
  649. Write-host "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"
  650. [int]$confirmselection = 0
  651. while ( $confirmselection -lt 1 -or $confirmselection -gt 2 ){
  652. Write-host "1. Yes the above is correct" -foregroundcolor "green"
  653. Write-host "2. No something in the above is wrong" -foregroundcolor "green"
  654. [Int]$confirmselection = read-host "Please enter an option" }
  655. Switch( $confirmselection ){
  656. 1{ $date = get-date
  657. Build_VM
  658. }
  659. 2{ Enter_Ticket }
  660. }
  661. }
  662.  
  663. #Set annotations
  664. Function Set_Annotations
  665. {
  666. Write-host Setting metadata VM -foregroundcolor "red"
  667. Set-Annotation -Entity $nameselection -customattribute guestPlan -Value $bundleselection.Plan
  668. Set-Annotation -Entity $nameselection -customattribute backupsShort -Value $shortbackup
  669. Set-Annotation -Entity $nameselection -customattribute backupsLong -Value $longbackup
  670. Set-Annotation -Entity $nameselection -customattribute createdDate -Value $date
  671. Set-Annotation -Entity $nameselection -customattribute osPlan -Value $oscode
  672. Set-Annotation -Entity $nameselection -customattribute byolExpiration -Value $BYOLselection
  673. Stop-Transcript
  674. Email_Deets
  675. }
  676.  
  677. #Build VM
  678. Function Build_VM
  679. {
  680. Write-host Removing any old non-persistant customization specs -foregroundcolor "red"
  681. #Get-OSCustomizationSpec -type nonpersistent | Remove-OSCustomizationSpec -confirm:$false
  682. Write-host Creating a clone of the original customisation spec -foregroundcolor "red"
  683. New-OSCustomizationSpec -Name $nameselection -OSCustomizationSpec $oscustselection -type NonPersistent
  684. Write-host Updating the cloned customisation spec with new VM specific settings -foregroundcolor "red"
  685. Set-OSCustomizationSpec -OSCustomizationSpec $nameselection -adminpassword $password -OrgName $orgnameselection -fullname $nameselection
  686. Get-OSCustomizationSpec -name $nameselection | Get-OSCustomizationNicMapping | Set-OSCustomizationNicMapping -IPMode UseStaticIP -IpAddress $ipselection -subnetmask $subselection -DefaultGateway $gateselection -DNS 8.8.8.8 -Confirm:$false
  687. Write-host Starting to build the VM -foregroundcolor "red"
  688. new-vm -name $nameselection -vmhost $hostelection -location $vmfolder -resourcepool $resourceselection -template $templateselection -datastore $datastoreselection -OSCustomizationSpec $nameselection
  689. Write-host Configuring the VM CPU and memory based on the bundle selection -foregroundcolor "red"
  690. Set-VM -vm $nameselection -MemoryGB $bundleselection.Mem -numcpu $bundleselection.vCPU -confirm:$false
  691. Write-host Configuring network adapter -foregroundcolor "red"
  692. Get-VM -name $nameselection | Get-NetworkAdapter | Set-NetworkAdapter -networkname $portgroupselection -StartConnected:$true -confirm:$false
  693. Write-host Configuring HDD size -foregroundcolor "red"
  694. Get-VM -name $nameselection | Get-HardDisk | Set-HardDisk -CapacityGB $bundleselection.Storage -confirm:$false
  695. Write-host Configuring VNC -foregroundcolor "red"
  696. Set_VMX
  697. Write-host Starting the VM -foregroundcolor "red"
  698. Start-VM -VM $nameselection
  699. Write-host -foregroundcolor red "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"
  700. Expand_GuestHDD
  701. Select_SQL
  702. #Open-VMConsoleWindow -vm $nameselection
  703. }
  704.  
  705. #Exit
  706. Function Select_Exit
  707. {
  708. #Get-OSCustomizationSpec -type nonpersistent | Remove-OSCustomizationSpec -confirm:$false
  709. Exit
  710. }
  711.  
  712. Enter_Ticket
  713. Select_Exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement