Advertisement
Guest User

Untitled

a guest
Aug 8th, 2017
558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 27.28 KB | None | 0 0
  1. # Author: William Lam
  2. # Website: www.virtuallyghetto.com
  3. # Description: PowerCLI script to deploy a fully functional vSphere 6.0 lab consisting of 3
  4. # Nested ESXi hosts enable w/vSAN + VCSA 6.0. Expects a single physical ESXi host
  5. # as the endpoint and all three Nested ESXi VMs will be deployed to physical ESXi host
  6. # with the VCSA then being deployed to the inner-Nested ESXi VMs as a self-managed VC
  7. # Reference: http://www.virtuallyghetto.com/2016/11/vghetto-automated-vsphere-lab-deployment-for-vsphere-6-0u2-vsphere-6-5.html
  8. # Credit: Thanks to Alan Renouf as I borrowed some of his PCLI code snippets :)
  9. #
  10. # Changelog
  11. # 11/22/16
  12. # * Automatically handle Nested ESXi on vSAN
  13. # 01/20/17
  14. # * Resolved "Another task in progress" thanks to Jason M
  15. # 02/12/17
  16. # * Support for deploying to VC Target
  17. # * Support for enabling SSH on VCSA
  18. # * Added option to auto-create vApp Container for VMs
  19. # * Added pre-check for required files
  20. # 02/17/17
  21. # * Added missing dvFilter param to eth1 (missing in Nested ESXi OVA)
  22.  
  23. # Physical ESXi host or vCenter Server to deploy vSphere 6.0 lab
  24. $VIServer = "vcenter.primp-industries.com"
  25. $VIUsername = "administrator@vsphere.local"
  26. $VIPassword = "!!!MySuperDuperSecurePassword!!!"
  27.  
  28. # Specifies whether deployment is to an ESXi host or vCenter Server
  29. # Use either ESXI or VCENTER
  30. $DeploymentTarget = "VCENTER"
  31.  
  32. # Full Path to both the Nested ESXi 6.0 VA + extracted VCSA 6.0 ISO
  33. $NestedESXiApplianceOVA = "C:\Users\primp\Desktop\Nested_ESXi6.x_Appliance_Template_v5.ova"
  34. $VCSAInstallerPath = "C:\Users\primp\Desktop\VMware-VCSA-all-6.0.0-3634788"
  35.  
  36. # Nested ESXi VMs to deploy
  37. $NestedESXiHostnameToIPs = @{
  38. "vesxi60-1" = "172.30.0.85"
  39. "vesxi60-2" = "172.30.0.86"
  40. "vesxi60-3" = "172.30.0.87"
  41. }
  42.  
  43. #Nested ESXi Bootstrap Node
  44. $bootStrapNode = "172.30.0.85"
  45.  
  46. # Nested ESXi VM Resources
  47. $NestedESXivCPU = "2"
  48. $NestedESXivMEM = "32" #GB
  49. $NestedESXiCachingvDisk = "16" #GB
  50. $NestedESXiCapacityvDisk = "200" #GB
  51.  
  52. # VCSA Deployment Configuration
  53. $VCSADeploymentSize = "tiny"
  54. $VCSADisplayName = "vcenter60-1"
  55. $VCSAIPAddress = "172.30.0.50"
  56. $VCSAHostname = "vcenter60-1.primp-industries.com" #Change to IP if you don't have valid DNS
  57. $VCSAPrefix = "24"
  58. $VCSASSODomainName = "vghetto.local"
  59. $VCSASSOSiteName = "virtuallyGhetto"
  60. $VCSASSOPassword = "VMware1!"
  61. $VCSARootPassword = "VMware1!"
  62.  
  63. # General Deployment Configuration for both Nested ESXi VMs + VCSA
  64. $VMNetwork = "access333"
  65. $VMDatastore = "himalaya-local-SATA-dc3500-2"
  66. $VMNetmask = "255.255.255.0"
  67. $VMGateway = "172.30.0.1"
  68. $VMDNS = "172.30.0.100"
  69. $VMNTP = "pool.ntp.org"
  70. $VMPassword = "vmware123"
  71. $VMDomain = "primp-industries.com"
  72. $VMSyslog = "172.30.0.170"
  73. # Applicable to Nested ESXi only
  74. $VMSSH = "true"
  75. $VMVMFS = "false"
  76. # Applicable to VC Deployment Target only
  77. $VMCluster = "Primp-Cluster"
  78.  
  79. # Name of new vSphere Datacenter/Cluster when VCSA is deployed
  80. $NewVCDatacenterName = "Datacenter"
  81. $NewVCVSANClusterName = "VSAN-Cluster"
  82.  
  83. #### DO NOT EDIT BEYOND HERE ####
  84.  
  85. $verboseLogFile = "vsphere60-vghetto-lab-deployment.log"
  86. $vSphereVersion = "6.0u2"
  87. $deploymentType = "Self Managed"
  88. $random_string = -join ((65..90) + (97..122) | Get-Random -Count 8 | % {[char]$_})
  89. $VAppName = "vGhetto-Nested-vSphere-Lab-$vSphereVersion-$random_string"
  90.  
  91. $preCheck = 1
  92. $confirmDeployment = 1
  93. $deployNestedESXiVMs = 1
  94. $bootStrapFirstNestedESXiVM = 1
  95. $deployVCSA = 1
  96. $setupNewVC = 1
  97. $addESXiHostsToVC = 1
  98. $configureVSANDiskGroups = 1
  99. $clearVSANHealthCheckAlarm = 1
  100.  
  101. $StartTime = Get-Date
  102.  
  103. Function My-Logger {
  104. param(
  105. [Parameter(Mandatory=$true)]
  106. [String]$message
  107. )
  108.  
  109. $timeStamp = Get-Date -Format "MM-dd-yyyy_hh-mm-ss"
  110.  
  111. Write-Host -NoNewline -ForegroundColor White "[$timestamp]"
  112. Write-Host -ForegroundColor Green " $message"
  113. $logMessage = "[$timeStamp] $message"
  114. $logMessage | Out-File -Append -LiteralPath $verboseLogFile
  115. }
  116.  
  117. if($preCheck -eq 1) {
  118. if(!(Test-Path $NestedESXiApplianceOVA)) {
  119. Write-Host -ForegroundColor Red "`nUnable to find $NestedESXiApplianceOVA ...`nexiting"
  120. exit
  121. }
  122.  
  123. if(!(Test-Path $VCSAInstallerPath)) {
  124. Write-Host -ForegroundColor Red "`nUnable to find $VCSAInstallerPath ...`nexiting"
  125. exit
  126. }
  127. }
  128.  
  129. if($confirmDeployment -eq 1) {
  130. Write-Host -ForegroundColor Magenta "`nPlease confirm the following configuration will be deployed:`n"
  131.  
  132. Write-Host -ForegroundColor Yellow "---- vGhetto vSphere Automated Lab Deployment Configuration ---- "
  133. Write-Host -NoNewline -ForegroundColor Green "Deployment Type: "
  134. Write-Host -ForegroundColor White $deploymentType
  135. Write-Host -NoNewline -ForegroundColor Green "vSphere Version: "
  136. Write-Host -ForegroundColor White "vSphere $vSphereVersion"
  137. Write-Host -NoNewline -ForegroundColor Green "Nested ESXi Image Path: "
  138. Write-Host -ForegroundColor White $NestedESXiApplianceOVA
  139. Write-Host -NoNewline -ForegroundColor Green "VCSA Image Path: "
  140. Write-Host -ForegroundColor White $VCSAInstallerPath
  141.  
  142. if($DeploymentTarget -eq "ESXI") {
  143. Write-Host -ForegroundColor Yellow "`n---- Physical ESXi Configuration ----"
  144. Write-Host -NoNewline -ForegroundColor Green "ESXi Address: "
  145. } else {
  146. Write-Host -ForegroundColor Yellow "`n---- vCenter Server Configuration ----"
  147. Write-Host -NoNewline -ForegroundColor Green "vCenter Server Address: "
  148. }
  149.  
  150. Write-Host -ForegroundColor White $VIServer
  151. Write-Host -NoNewline -ForegroundColor Green "Username: "
  152. Write-Host -ForegroundColor White $VIUsername
  153. Write-Host -NoNewline -ForegroundColor Green "VM Network: "
  154. Write-Host -ForegroundColor White $VMNetwork
  155. Write-Host -NoNewline -ForegroundColor Green "VM Storage: "
  156. Write-Host -ForegroundColor White $VMDatastore
  157.  
  158. if($DeploymentTarget -eq "VCENTER") {
  159. Write-Host -NoNewline -ForegroundColor Green "VM Cluster: "
  160. Write-Host -ForegroundColor White $VMCluster
  161. Write-Host -NoNewline -ForegroundColor Green "VM vApp: "
  162. Write-Host -ForegroundColor White $VAppName
  163. }
  164.  
  165. Write-Host -ForegroundColor Yellow "`n---- vESXi Configuration ----"
  166. Write-Host -NoNewline -ForegroundColor Green "# of Nested ESXi VMs: "
  167. Write-Host -ForegroundColor White $NestedESXiHostnameToIPs.count
  168. Write-Host -NoNewline -ForegroundColor Green "vCPU: "
  169. Write-Host -ForegroundColor White $NestedESXivCPU
  170. Write-Host -NoNewline -ForegroundColor Green "vMEM: "
  171. Write-Host -ForegroundColor White "$NestedESXivMEM GB"
  172. Write-Host -NoNewline -ForegroundColor Green "Caching VMDK: "
  173. Write-Host -ForegroundColor White "$NestedESXiCachingvDisk GB"
  174. Write-Host -NoNewline -ForegroundColor Green "Capacity VMDK: "
  175. Write-Host -ForegroundColor White "$NestedESXiCapacityvDisk GB"
  176. Write-Host -NoNewline -ForegroundColor Green "IP Address(s): "
  177. Write-Host -ForegroundColor White $NestedESXiHostnameToIPs.Values
  178. Write-Host -NoNewline -ForegroundColor Green "Netmask "
  179. Write-Host -ForegroundColor White $VMNetmask
  180. Write-Host -NoNewline -ForegroundColor Green "Gateway: "
  181. Write-Host -ForegroundColor White $VMGateway
  182. Write-Host -NoNewline -ForegroundColor Green "DNS: "
  183. Write-Host -ForegroundColor White $VMDNS
  184. Write-Host -NoNewline -ForegroundColor Green "NTP: "
  185. Write-Host -ForegroundColor White $VMNTP
  186. Write-Host -NoNewline -ForegroundColor Green "Syslog: "
  187. Write-Host -ForegroundColor White $VMSyslog
  188. Write-Host -NoNewline -ForegroundColor Green "Enable SSH: "
  189. Write-Host -ForegroundColor White $VMSSH
  190. Write-Host -NoNewline -ForegroundColor Green "Create VMFS Volume: "
  191. Write-Host -ForegroundColor White $VMVMFS
  192. Write-Host -NoNewline -ForegroundColor Green "Root Password: "
  193. Write-Host -ForegroundColor White $VMPassword
  194. Write-Host -NoNewline -ForegroundColor Green "Bootstrap ESXi Node: "
  195. Write-Host -ForegroundColor White $bootStrapNode
  196.  
  197. Write-Host -ForegroundColor Yellow "`n---- VCSA Configuration ----"
  198. Write-Host -NoNewline -ForegroundColor Green "Deployment Size: "
  199. Write-Host -ForegroundColor White $VCSADeploymentSize
  200. Write-Host -NoNewline -ForegroundColor Green "SSO Domain: "
  201. Write-Host -ForegroundColor White $VCSASSODomainName
  202. Write-Host -NoNewline -ForegroundColor Green "SSO Site: "
  203. Write-Host -ForegroundColor White $VCSASSOSiteName
  204. Write-Host -NoNewline -ForegroundColor Green "SSO Password: "
  205. Write-Host -ForegroundColor White $VCSASSOPassword
  206. Write-Host -NoNewline -ForegroundColor Green "Root Password: "
  207. Write-Host -ForegroundColor White $VCSARootPassword
  208. Write-Host -NoNewline -ForegroundColor Green "Enable SSH: "
  209. Write-Host -ForegroundColor White $VCSASSHEnable
  210. Write-Host -NoNewline -ForegroundColor Green "Hostname: "
  211. Write-Host -ForegroundColor White $VCSAHostname
  212. Write-Host -NoNewline -ForegroundColor Green "IP Address: "
  213. Write-Host -ForegroundColor White $VCSAIPAddress
  214. Write-Host -NoNewline -ForegroundColor Green "Netmask "
  215. Write-Host -ForegroundColor White $VMNetmask
  216. Write-Host -NoNewline -ForegroundColor Green "Gateway: "
  217. Write-Host -ForegroundColor White $VMGateway
  218.  
  219. Write-Host -ForegroundColor Magenta "`nWould you like to proceed with this deployment?`n"
  220. $answer = Read-Host -Prompt "Do you accept (Y or N)"
  221. if($answer -ne "Y" -or $answer -ne "y") {
  222. exit
  223. }
  224. Clear-Host
  225. }
  226.  
  227. if($deployNestedESXiVMs -eq 1) {
  228. My-Logger "Connecting to $VIServer ..."
  229. $viConnection = Connect-VIServer $VIServer -User $VIUsername -Password $VIPassword -WarningAction SilentlyContinue
  230.  
  231. if($DeploymentTarget -eq "ESXI") {
  232. $datastore = Get-Datastore -Server $viConnection -Name $VMDatastore
  233. $vmhost = Get-VMHost -Server $viConnection
  234. $network = Get-VirtualPortGroup -Server $viConnection -Name $VMNetwork -VMHost $vmhost
  235.  
  236. if($datastore.Type -eq "vsan") {
  237. My-Logger "VSAN Datastore detected, enabling Fake SCSI Reservations ..."
  238. Get-AdvancedSetting -Entity $vmhost -Name "VSAN.FakeSCSIReservations" | Set-AdvancedSetting -Value 1 -Confirm:$false | Out-File -Append -LiteralPath $verboseLogFile
  239. }
  240. } else {
  241. $datastore = Get-Datastore -Server $viConnection -Name $VMDatastore | Select -First 1
  242. $network = Get-VirtualPortGroup -Server $viConnection -Name $VMNetwork | Select -First 1
  243. $cluster = Get-Cluster -Server $viConnection -Name $VMCluster
  244. $datacenter = $cluster | Get-Datacenter
  245. $vmhost = $cluster | Get-VMHost | Select -First 1
  246. }
  247.  
  248. if($DeploymentTarget -eq "ESXI") {
  249. $NestedESXiHostnameToIPs.GetEnumerator() | Sort-Object -Property Value | Foreach-Object {
  250. $VMName = $_.Key
  251. $VMIPAddress = $_.Value
  252.  
  253. My-Logger "Deploying Nested ESXi VM $VMName ..."
  254. $vm = Import-VApp -Server $viConnection -Source $NestedESXiApplianceOVA -Name $VMName -VMHost $vmhost -Datastore $datastore -DiskStorageFormat thin
  255.  
  256. My-Logger "Updating VM Network ..."
  257. foreach($networkAdapter in ($vm | Get-NetworkAdapter))
  258. {
  259. My-Logger "Configuring adapter $networkAdapter in $vm"
  260. $networkAdapter | Set-NetworkAdapter -Portgroup $network -confirm:$false | Out-File -Append -LiteralPath $verboseLogFile
  261. sleep 5
  262. }
  263.  
  264. My-Logger "Updating vCPU Count to $NestedESXivCPU & vMEM to $NestedESXivMEM GB ..."
  265. Set-VM -Server $viConnection -VM $vm -NumCpu $NestedESXivCPU -MemoryGB $NestedESXivMEM -Confirm:$false | Out-File -Append -LiteralPath $verboseLogFile
  266.  
  267. My-Logger "Updating vSAN Caching VMDK size to $NestedESXiCachingvDisk GB ..."
  268. Get-HardDisk -Server $viConnection -VM $vm -Name "Hard disk 2" | Set-HardDisk -CapacityGB $NestedESXiCachingvDisk -Confirm:$false | Out-File -Append -LiteralPath $verboseLogFile
  269.  
  270. My-Logger "Updating vSAN Capacity VMDK size to $NestedESXiCapacityvDisk GB ..."
  271. Get-HardDisk -Server $viConnection -VM $vm -Name "Hard disk 3" | Set-HardDisk -CapacityGB $NestedESXiCapacityvDisk -Confirm:$false | Out-File -Append -LiteralPath $verboseLogFile
  272.  
  273. $orignalExtraConfig = $vm.ExtensionData.Config.ExtraConfig
  274. $a = New-Object VMware.Vim.OptionValue
  275. $a.key = "guestinfo.hostname"
  276. $a.value = $VMName
  277. $b = New-Object VMware.Vim.OptionValue
  278. $b.key = "guestinfo.ipaddress"
  279. $b.value = $VMIPAddress
  280. $c = New-Object VMware.Vim.OptionValue
  281. $c.key = "guestinfo.netmask"
  282. $c.value = $VMNetmask
  283. $d = New-Object VMware.Vim.OptionValue
  284. $d.key = "guestinfo.gateway"
  285. $d.value = $VMGateway
  286. $e = New-Object VMware.Vim.OptionValue
  287. $e.key = "guestinfo.dns"
  288. $e.value = $VMDNS
  289. $f = New-Object VMware.Vim.OptionValue
  290. $f.key = "guestinfo.domain"
  291. $f.value = $VMDomain
  292. $g = New-Object VMware.Vim.OptionValue
  293. $g.key = "guestinfo.ntp"
  294. $g.value = $VMNTP
  295. $h = New-Object VMware.Vim.OptionValue
  296. $h.key = "guestinfo.syslog"
  297. $h.value = $VMSyslog
  298. $i = New-Object VMware.Vim.OptionValue
  299. $i.key = "guestinfo.password"
  300. $i.value = $VMPassword
  301. $j = New-Object VMware.Vim.OptionValue
  302. $j.key = "guestinfo.ssh"
  303. $j.value = $VMSSH
  304. $k = New-Object VMware.Vim.OptionValue
  305. $k.key = "guestinfo.createvmfs"
  306. $k.value = $VMVMFS
  307. $l = New-Object VMware.Vim.OptionValue
  308. $l.key = "ethernet1.filter4.name"
  309. $l.value = "dvfilter-maclearn"
  310. $m = New-Object VMware.Vim.OptionValue
  311. $m.key = "ethernet1.filter4.onFailure"
  312. $m.value = "failOpen"
  313. $orignalExtraConfig+=$a
  314. $orignalExtraConfig+=$b
  315. $orignalExtraConfig+=$c
  316. $orignalExtraConfig+=$d
  317. $orignalExtraConfig+=$e
  318. $orignalExtraConfig+=$f
  319. $orignalExtraConfig+=$g
  320. $orignalExtraConfig+=$h
  321. $orignalExtraConfig+=$i
  322. $orignalExtraConfig+=$j
  323. $orignalExtraConfig+=$k
  324. $orignalExtraConfig+=$l
  325. $orignalExtraConfig+=$m
  326.  
  327. $spec = New-Object VMware.Vim.VirtualMachineConfigSpec
  328. $spec.ExtraConfig = $orignalExtraConfig
  329.  
  330. My-Logger "Adding guestinfo customization properties to $vmname ..."
  331. $task = $vm.ExtensionData.ReconfigVM_Task($spec)
  332. $task1 = Get-Task -Id ("Task-$($task.value)")
  333. $task1 | Wait-Task | Out-File -Append -LiteralPath $verboseLogFile
  334.  
  335. My-Logger "Powering On $vmname ..."
  336. Start-VM -Server $viConnection -VM $vm -Confirm:$false | Out-File -Append -LiteralPath $verboseLogFile
  337. }
  338. } else {
  339. $NestedESXiHostnameToIPs.GetEnumerator() | Sort-Object -Property Value | Foreach-Object {
  340. $VMName = $_.Key
  341. $VMIPAddress = $_.Value
  342.  
  343. $ovfconfig = Get-OvfConfiguration $NestedESXiApplianceOVA
  344. $ovfconfig.NetworkMapping.VM_Network.value = $VMNetwork
  345.  
  346. $ovfconfig.common.guestinfo.hostname.value = $VMName
  347. $ovfconfig.common.guestinfo.ipaddress.value = $VMIPAddress
  348. $ovfconfig.common.guestinfo.netmask.value = $VMNetmask
  349. $ovfconfig.common.guestinfo.gateway.value = $VMGateway
  350. $ovfconfig.common.guestinfo.dns.value = $VMDNS
  351. $ovfconfig.common.guestinfo.domain.value = $VMDomain
  352. $ovfconfig.common.guestinfo.ntp.value = $VMNTP
  353. $ovfconfig.common.guestinfo.syslog.value = $VMSyslog
  354. $ovfconfig.common.guestinfo.password.value = $VMPassword
  355. if($VMSSH -eq "true") {
  356. $VMSSHVar = $true
  357. } else {
  358. $VMSSHVar = $false
  359. }
  360. $ovfconfig.common.guestinfo.ssh.value = $VMSSHVar
  361.  
  362. My-Logger "Deploying Nested ESXi VM $VMName ..."
  363. $vm = Import-VApp -Source $NestedESXiApplianceOVA -OvfConfiguration $ovfconfig -Name $VMName -Location $cluster -VMHost $vmhost -Datastore $datastore -DiskStorageFormat thin
  364.  
  365. # Add the dvfilter settings to the exisiting ethernet1 (not part of ova template)
  366. My-Logger "Correcting missing dvFilter settings for Ethernet[1] ..."
  367. $vm | New-AdvancedSetting -name "ethernet1.filter4.name" -value "dvfilter-maclearn" -confirm:$false | Out-File -Append -LiteralPath $verboseLogFile
  368. $vm | New-AdvancedSetting -Name "ethernet1.filter4.onFailure" -value "failOpen" -confirm:$false | Out-File -Append -LiteralPath $verboseLogFile
  369.  
  370. My-Logger "Updating vCPU Count to $NestedESXivCPU & vMEM to $NestedESXivMEM GB ..."
  371. Set-VM -Server $viConnection -VM $vm -NumCpu $NestedESXivCPU -MemoryGB $NestedESXivMEM -Confirm:$false | Out-File -Append -LiteralPath $verboseLogFile
  372.  
  373. My-Logger "Updating vSAN Caching VMDK size to $NestedESXiCachingvDisk GB ..."
  374. Get-HardDisk -Server $viConnection -VM $vm -Name "Hard disk 2" | Set-HardDisk -CapacityGB $NestedESXiCachingvDisk -Confirm:$false | Out-File -Append -LiteralPath $verboseLogFile
  375.  
  376. My-Logger "Updating vSAN Capacity VMDK size to $NestedESXiCapacityvDisk GB ..."
  377. Get-HardDisk -Server $viConnection -VM $vm -Name "Hard disk 3" | Set-HardDisk -CapacityGB $NestedESXiCapacityvDisk -Confirm:$false | Out-File -Append -LiteralPath $verboseLogFile
  378.  
  379. My-Logger "Powering On $vmname ..."
  380. $vm | Start-Vm -RunAsync | Out-Null
  381. }
  382. }
  383.  
  384. if($moveVMsIntovApp -eq 1 -and $DeploymentTarget -eq "VCENTER") {
  385. My-Logger "Creating vApp $VAppName ..."
  386. $VApp = New-VApp -Name $VAppName -Server $viConnection -Location $cluster
  387.  
  388. if($deployNestedESXiVMs -eq 1) {
  389. My-Logger "Moving Nested ESXi VMs into $VAppName vApp ..."
  390. $NestedESXiHostnameToIPs.GetEnumerator() | Sort-Object -Property Value | Foreach-Object {
  391. $vm = Get-VM -Name $_.Key -Server $viConnection
  392. Move-VM -VM $vm -Server $viConnection -Destination $VApp -Confirm:$false | Out-File -Append -LiteralPath $verboseLogFile
  393. }
  394. }
  395. }
  396.  
  397. My-Logger "Disconnecting from $VIServer ..."
  398. Disconnect-VIServer $viConnection -Confirm:$false
  399. }
  400.  
  401. if($bootStrapFirstNestedESXiVM -eq 1) {
  402. do {
  403. My-Logger "Waiting for $bootStrapNode to be ready on network ..."
  404. $ping = test-connection $bootStrapNode -Quiet
  405. sleep 60
  406. } until ($ping -contains "True")
  407.  
  408. My-Logger "Connecting to ESXi bootstrap node ..."
  409. $vEsxi = Connect-VIServer -Server $bootStrapNode -User root -Password $VMPassword -WarningAction SilentlyContinue
  410.  
  411. My-Logger "Updating the ESXi host VSAN Policy to allow Force Provisioning ..."
  412. $esxcli = Get-EsxCli -Server $vEsxi -V2
  413. $VSANPolicy = '(("hostFailuresToTolerate" i1) ("forceProvisioning" i1))'
  414. $VSANPolicyDefaults = $esxcli.vsan.policy.setdefault.CreateArgs()
  415. $VSANPolicyDefaults.policy = $VSANPolicy
  416. $VSANPolicyDefaults.policyclass = "vdisk"
  417. $esxcli.vsan.policy.setdefault.Invoke($VSANPolicyDefaults) | Out-File -Append -LiteralPath $verboseLogFile
  418. $VSANPolicyDefaults.policyclass = "vmnamespace"
  419. $esxcli.vsan.policy.setdefault.Invoke($VSANPolicyDefaults) | Out-File -Append -LiteralPath $verboseLogFile
  420.  
  421. My-Logger "Creating a new VSAN Cluster"
  422. $esxcli.vsan.cluster.new.Invoke() | Out-File -Append -LiteralPath $verboseLogFile
  423.  
  424. $luns = Get-ScsiLun -Server $vEsxi | select CanonicalName, CapacityGB
  425.  
  426. My-Logger "Querying ESXi host disks to create VSAN Diskgroups ..."
  427. foreach ($lun in $luns) {
  428. if(([int]($lun.CapacityGB)).toString() -eq "$NestedESXiCachingvDisk") {
  429. $vsanCacheDisk = $lun.CanonicalName
  430. }
  431. if(([int]($lun.CapacityGB)).toString() -eq "$NestedESXiCapacityvDisk") {
  432. $vsanCapacityDisk = $lun.CanonicalName
  433. }
  434. }
  435.  
  436. My-Logger "Tagging Capacity Disk ..."
  437. $capacitytag = $esxcli.vsan.storage.tag.add.CreateArgs()
  438. $capacitytag.disk = $vsanCapacityDisk
  439. $capacitytag.tag = "capacityFlash"
  440. $esxcli.vsan.storage.tag.add.Invoke($capacitytag) | Out-File -Append -LiteralPath $verboseLogFile
  441.  
  442. My-Logger "Creating VSAN Diskgroup ..."
  443. $addvsanstorage = $esxcli.vsan.storage.add.CreateArgs()
  444. $addvsanstorage.ssd = $vsanCacheDisk
  445. $addvsanstorage.disks = $vsanCapacityDisk
  446. $esxcli.vsan.storage.add.Invoke($addvsanstorage) | Out-File -Append -LiteralPath $verboseLogFile
  447.  
  448. My-Logger "Disconnecting from $esxi ..."
  449. Disconnect-VIServer $vEsxi -Confirm:$false
  450. }
  451.  
  452. if($deployVCSA -eq 1) {
  453. My-Logger "Connecting to first ESXi bootstrap node ..."
  454. $vEsxi = Connect-VIServer -Server $bootStrapNode -User root -Password $VMPassword -WarningAction SilentlyContinue
  455.  
  456. # Deploy using the VCSA CLI Installer
  457. $config = (Get-Content -Raw "$($VCSAInstallerPath)\vcsa-cli-installer\templates\install\embedded_vCSA_on_ESXi.json") | convertfrom-json
  458. $config.'target.vcsa'.esx.hostname = $bootStrapNode
  459. $config.'target.vcsa'.esx.username = "root"
  460. $config.'target.vcsa'.esx.password = $VMPassword
  461. $config.'target.vcsa'.esx.datastore = "vsanDatastore"
  462. $config.'target.vcsa'.appliance.'deployment.network' = "VM Network"
  463. $config.'target.vcsa'.appliance.'thin.disk.mode' = $true
  464. $config.'target.vcsa'.appliance.'deployment.option' = $VCSADeploymentSize
  465. $config.'target.vcsa'.appliance.name = $VCSADisplayName
  466. $config.'target.vcsa'.network.'ip.family' = "ipv4"
  467. $config.'target.vcsa'.network.mode = "static"
  468. $config.'target.vcsa'.network.ip = $VCSAIPAddress
  469. $config.'target.vcsa'.network.'dns.servers'[0] = $VMDNS
  470. $config.'target.vcsa'.network.'dns.servers'[1] = $null
  471. $config.'target.vcsa'.network.prefix = $VCSAPrefix
  472. $config.'target.vcsa'.network.gateway = $VMGateway
  473. $config.'target.vcsa'.network.hostname = $VCSAHostname
  474. if($VCSASSHEnable -eq "true") {
  475. $VCSASSHEnableVar = $true
  476. } else {
  477. $VCSASSHEnableVar = $false
  478. }
  479. $config.'target.vcsa'.os.'ssh.enable' = $VCSASSHEnableVar
  480. $config.'target.vcsa'.os.password = $VCSARootPassword
  481. $config.'target.vcsa'.sso.password = $VCSASSOPassword
  482. $config.'target.vcsa'.sso.'domain-name' = $VCSASSODomainName
  483. $config.'target.vcsa'.sso.'site-name' = $VCSASSOSiteName
  484.  
  485. My-Logger "Creating VCSA JSON Configuration file for deployment ..."
  486. $config | ConvertTo-Json | Set-Content -Path "$($ENV:Temp)\jsontemplate.json"
  487.  
  488. My-Logger "Deploying the VCSA ..."
  489. $output = Invoke-Expression "$($VCSAInstallerPath)\vcsa-cli-installer\win32\vcsa-deploy.exe install --no-esx-ssl-verify --accept-eula $($ENV:Temp)\jsontemplate.json" -ErrorVariable vcsaDeployOutput 2>&1
  490. $vcsaDeployOutput | Out-File -Append -LiteralPath $verboseLogFile
  491.  
  492. My-Logger "Disconnecting from $bootStrapNode ..."
  493. Disconnect-VIServer $vEsxi -Confirm:$false
  494. }
  495.  
  496. if($setupNewVC -eq 1) {
  497. My-Logger "Connecting to the new VCSA ..."
  498. $vc = Connect-VIServer $VCSAIPAddress -User "administrator@$VCSASSODomainName" -Password $VCSASSOPassword -WarningAction SilentlyContinue
  499.  
  500. My-Logger "Creating Datacenter $NewVCDatacenterName ..."
  501. New-Datacenter -Server $vc -Name $NewVCDatacenterName -Location (Get-Folder -Type Datacenter -Server $vc) | Out-File -Append -LiteralPath $verboseLogFile
  502.  
  503. My-Logger "Creating VSAN Cluster $NewVCVSANClusterName ..."
  504. New-Cluster -Server $vc -Name $NewVCVSANClusterName -Location (Get-Datacenter -Name $NewVCDatacenterName -Server $vc) -DrsEnabled -VsanEnabled -VsanDiskClaimMode 'Manual' | Out-File -Append -LiteralPath $verboseLogFile
  505.  
  506. if($addESXiHostsToVC -eq 1) {
  507. $NestedESXiHostnameToIPs.GetEnumerator() | sort -Property Value | Foreach-Object {
  508. $VMName = $_.Key
  509. $VMIPAddress = $_.Value
  510.  
  511. My-Logger "Adding ESXi host $VMIPAddress to Cluster ..."
  512. Add-VMHost -Server $vc -Location (Get-Cluster -Name $NewVCVSANClusterName) -User "root" -Password $VMPassword -Name $VMIPAddress -Force | Out-File -Append -LiteralPath $verboseLogFile
  513. }
  514.  
  515. }
  516.  
  517. if($configureVSANDiskGroups -eq 1) {
  518. # New vSAN cmdlets only works on 6.0u3+
  519. $VmhostToCheckVersion = (Get-Cluster -Server $vc | Get-VMHost)[0]
  520. $MajorVersion = $VmhostToCheckVersion.Version
  521. $UpdateVersion = (Get-AdvancedSetting -Entity $VmhostToCheckVersion -Name Misc.HostAgentUpdateLevel).value
  522.  
  523. if( ($MajorVersion -eq "6.0.0" -and $UpdateVersion -eq "3") -or $MajorVersion -eq "6.5.0") {
  524. My-Logger "Enabling VSAN Space Efficiency/De-Dupe & disabling VSAN Health Check ..."
  525. Get-VsanClusterConfiguration -Server $vc -Cluster $NewVCVSANClusterName | Set-VsanClusterConfiguration -SpaceEfficiencyEnabled $true -HealthCheckIntervalMinutes 0 | Out-File -Append -LiteralPath $verboseLogFile
  526. }
  527.  
  528. foreach ($vmhost in Get-Cluster -Server $vc | Get-VMHost) {
  529. if((Get-VsanDiskGroup -VMHost $vmhost) -eq $null) {
  530. $luns = $vmhost | Get-ScsiLun | select CanonicalName, CapacityGB
  531.  
  532. My-Logger "Querying ESXi host disks to create VSAN Diskgroups ..."
  533. foreach ($lun in $luns) {
  534. if(([int]($lun.CapacityGB)).toString() -eq "$NestedESXiCachingvDisk") {
  535. $vsanCacheDisk = $lun.CanonicalName
  536. }
  537. if(([int]($lun.CapacityGB)).toString() -eq "$NestedESXiCapacityvDisk") {
  538. $vsanCapacityDisk = $lun.CanonicalName
  539. }
  540. }
  541. My-Logger "Creating VSAN DiskGroup for $vmhost ..."
  542. New-VsanDiskGroup -Server $vc -VMHost $vmhost -SsdCanonicalName $vsanCacheDisk -DataDiskCanonicalName $vsanCapacityDisk | Out-File -Append -LiteralPath $verboseLogFile
  543. }
  544. }
  545. }
  546.  
  547. if($clearVSANHealthCheckAlarm -eq 1) {
  548. My-Logger "Clearing default VSAN Health Check Alarms, not applicable in Nested ESXi env ..."
  549. $alarmMgr = Get-View AlarmManager -Server $vc
  550. Get-Cluster -Server $vc | where {$_.ExtensionData.TriggeredAlarmState} | %{
  551. $cluster = $_
  552. $Cluster.ExtensionData.TriggeredAlarmState | %{
  553. $alarmMgr.AcknowledgeAlarm($_.Alarm,$cluster.ExtensionData.MoRef)
  554. }
  555. }
  556.  
  557. My-Logger "Updating VSAN Default VM Storage Policy back to its defaults ..."
  558. $VSANPolicy = Get-SpbmStoragePolicy "Virtual SAN Default Storage Policy"
  559. $Ruleset = New-SpbmRuleSet -Name "Rule-set 1" -AllOfRules @((New-SpbmRule -Capability VSAN.forceProvisioning $false), (New-SpbmRule -Capability VSAN.hostFailuresToTolerate 1))
  560. $VSANPolicy | Set-SpbmStoragePolicy -RuleSet $Ruleset | Out-File -Append -LiteralPath $verboseLogFile
  561. }
  562.  
  563. My-Logger "Disconnecting from new VCSA ..."
  564. Disconnect-VIServer $vc -Confirm:$false
  565. }
  566. $EndTime = Get-Date
  567. $duration = [math]::Round((New-TimeSpan -Start $StartTime -End $EndTime).TotalMinutes,2)
  568.  
  569. My-Logger "vSphere $vSphereVersion Lab Deployment Complete!"
  570. My-Logger "StartTime: $StartTime"
  571. My-Logger " EndTime: $EndTime"
  572. My-Logger " Duration: $duration minutes"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement