# Dump list of VMs from ServiceNow
Get-ITDServiceNowRecord -Table cmdb_ci_server -Filter "serial_numberSTARTSWITHvmware" -IncludeTotalCount | Export-Csv "C:\temp\CMDB_VMwareDump.csv"

# Disable DRS on all clusters
Get-Cluster LINUX*, MGMT*, ORACLE1, PS*, SQLa*, SQLe*, VAPP*, WAS*, WINDOWS* | Set-Cluster -DrsEnabled $false

# Dump location of all VMs to CSV
Get-VM | Where-Object { $_.ExtensionData.summary.config.ManagedBy.Type -ne "placeholderVm" } | select Name, PowerState, NumCpu, MemoryGB, VMHost, @{n = 'Datastores'; e = { ($_ | Get-Datastore).Name } } | Export-Csv "D:\State of North Dakota\-Tm-ITD-Virtualization - Documents\VMinfo.csv"

# VM array
$VMHosts = @(
    'itdvmbismgmt02.nd.gov',
    'itdvmbismgmt03.nd.gov',
    'itdvmmdnmgmt01.nd.gov',
    'itdvmbismgmt02.nd.gov'
)

# Disable lockdown on MGMT hosts
$VMHosts | ForEach-Object {
    (Get-VMHost -Name $_ | Get-View).ExitLockdownMode()
}

# connect to mgmt cluster
$VMHosts | ForEach-Object {
    Connect-VIServer -Server $_ -Credential (Get-ITDPassword -Title $_ -UserName root -Credential $PrvCred)
}

## verify lockdown mode is disabled manually

# get list of VMs  | Where-Object { $_.ExtensionData.Summary.Config.ManagedBy.Type -ne "placeholderVm" }
$VMs = Get-VM | Where-Object { $_.Name -notlike "vCLS*" -and $_.PowerState -eq "PoweredOn" -and $_.Name -notlike "itdvmutil*" -and $_.ExtensionData.Summary.Config.ManagedBy.Type -ne "placeholderVm"} | Sort-Object Name

# Cleanup snapshots on MGMT cluster
$VMs | Get-Snapshot | Where-Object Name -NotLike "*9.0.2*" | Remove-Snapshot -RunAsync

# Shutdown all appliance VMs on MGMT cluster
$VMs | Stop-VMGuest -Confirm:$false #-RunAsync

# Wait for VMs to power off
Get-VM -Name $VMs.Name | Sort-Object Name
#Get-Cluster MGMT1, MGMT2 | Get-VM | Where-Object { $_.Name -notlike "itdvmutil*" } | Sort-Object Name

# Create offline snapshots
$VMs | New-Snapshot -Name "9_Before 8.0 U3e Upgrade" -Description "9_Before 8.0 U3e Upgrade" -Confirm:$false -RunAsync

# Power on all VMs on MGMT cluster
$VMs | Start-VM -Confirm:$false

Break

## online mid-upgrade snapshots
$VMs | New-Snapshot -Name "1_vc_upgrade_complete" -Description "2_vc1_stage2_complete" -Confirm:$false -RunAsync -Memory

Get-VM -Name itdvmvc*,itdvmsrm*, itdvmvra* | Where-Object {$_.Name -notlike "*7.0*"} | Stop-VMGuest -Confirm:$false

# post upgrade snapshot
Get-VM -Name itdvmvc*,itdvmsrm*, itdvmvra* | New-Snapshot -Name "2_vc_upgrade_complete" -Description "2_vc1_stage2_complete" -Confirm:$false -RunAsync -Memory
Get-VM -Name itdvmvc*,itdvmsrm*, itdvmvra* | Start-VM
##### after upgrade

# Enable DRS on clusters that should have it
Get-Cluster LINUX*, MGMT*, ORACLE1, PS*, SQLa*, SQLe*, VAPP*, WAS*, WINDOWS* | Set-Cluster -DrsEnabled $true

# Enable lockdown on MGMT hosts
(Get-VMHost itdvmbismgmt01.nd.gov | Get-View).EnterLockdownMode()
(Get-VMHost itdvmbismgmt02.nd.gov | Get-View).EnterLockdownMode()
(Get-VMHost itdvmmdnmgmt01.nd.gov | Get-View).EnterLockdownMode()
(Get-VMHost itdvmmdnmgmt02.nd.gov | Get-View).EnterLockdownMode()

# reconfigure distributed switch to use both vnics
#### manual