$VMHostNames=@"
itdvmbisps10.nd.gov
itdvmbisps11.nd.gov
itdvmbisps12.nd.gov
itdvmbisps13.nd.gov
itdvmbisps14.nd.gov
itdvmbisps15.nd.gov
itdvmbisps16.nd.gov
itdvmbisps17.nd.gov
itdvmbisps18.nd.gov
"@

$VMHostNames = ConvertTo-Array -MultiLineString $VMHostNames

<#$VMHostNames | ForEach-Object{
    New-ITDVMwareHostScratchFolder -Site Bismarck $_.split('.')[0]
}
#>
foreach ($VMHostName in $VMHostNames){
    $GetVMHost = Get-VMhost $VMHostName
    $VMHostParent = $GetVMHost.Parent
    $HostCluster = Get-Cluster $VMHostParent
    $NewSpec = New-Object VMware.Vim.ClusterConfigSpec
    $NewSpec.DasConfig = New-Object VMware.Vim.ClusterDasConfigInfo
    $NewSpec.DasConfig.AdmissionControlPolicy = New-Object VMware.Vim.ClusterFailoverResourcesAdmissionControlPolicy
    $NewSpec.DasConfig.AdmissionControlPolicy.AutoComputePercentages = $true
    $HostCluster.ExtensionData.ReconfigureCluster($NewSpec,$true)
    $VIServer = $GetVMHost.Uid.Split('@')[1].Split(':')[0]
    $alarmMgr = Get-View AlarmManager -Server $VIServer
    $alarmEnabled = $GetVMHost.ExtensionData.AlarmActionsEnabled
    if ($alarmEnabled -eq $true){
        $alarmMgr.EnableAlarmActions($GetVMHost.ExtensionData.MoRef,$false)
    }
    $VMs = $GetVMHost | Get-VM
    #Disconnect CDROM and/or VMtools ISO
    foreach ($VM in $VMs){
        $GetVM = Get-VM $VM
        $CDDrive = $GetVM | Get-CDDrive
        if ($CDDrive | Where-Object {$_.IsoPath -Like "*vmware/isoimages*"}){
            $GetVM | Dismount-Tools
            #$CDDrive | Set-CDDrive -NoMedia -Confirm:$false
        }
        elseif ($CDDrive.HostDevice -Like "*drive*"){
            $CDDrive | Set-CDDrive -NoMedia -Confirm:$false
        }            
    }
    $GetVMHost = $null
}
#Move Powered Off VMs
foreach ($VMHostName in $VMHostNames){
    $VMHostDetails = Get-VMHost -Name $VMHostName
    $VMHostParent = $VMHostDetails.Parent.Name
    $VMHostMigrate = Get-Cluster $VMHostParent | Get-VMHost | Where-Object Name -NE $VMHostName | Select-Object -First 1
    $VMsPoweredOff = Get-VMHost $VMHostName | Get-VM | Where-Object PowerState -EQ "PoweredOff"
    Move-VM -VM $VMsPoweredOff -Destination $VMHostMigrate
}
#Enter Maintenance Mode
Set-VMHost -VMHost $VMHostNames -State "Maintenance" -RunAsync
#Monitor
Get-VMHost -Name $VMHostNames | Select-Object Name,ConnectionState,@{Name="VM.count";E={@($_ | Get-VM | Where-Object {$_.ExtensionData.Summary.Config.ManagedBy.Type -NE "placeholderVm"}).Count}} | sort-object Name

# shutdown
Get-VMHost -Name $VMHostnames | Stop-VMHost -Confirm:$false

#status
Get-VMHost -Name $VMHostnames 

# OneView, Update Server Profile from Template
ForEach($VMHostName in $VMHostNames){
    Get-OVServerProfile -Name $VMHostName.split('.')[0] | Update-OVServerProfile -Async -confirm:$false
}

# Power on
ForEach($VMHostName in $VMHostNames){
    Get-OVServer -ServerName $VMHostName | Start-OVServer -Async
}

# Verify on and connected
Get-VMHost $VMHostNames

# attach new host profile
Get-VMHost -Name $VMHostnames | Set-VMHost -Profile "MDN 6.5.0 Synergy General 2203"

# Apply/Remediate Host Profile (invoke)
Get-VMHost -Name $VMHostnames | Invoke-VMHostProfile -confirm:$false -RunAsync

# manual check for dvswitch data-user


#Exit Maintenance Mode and Enable Alarms
Set-VMHost -VMHost $VMHostNames -State "Connected" -RunAsync
Start-Sleep -Seconds 60
foreach ($VMHostName in $VMHostNames){
    $GetVMHost = Get-VMhost $VMHostName
    $VIServer = $GetVMHost.Uid.Split('@')[1].Split(':')[0]
    $alarmMgr = Get-View AlarmManager -Server $VIServer
    $alarmEnabled = $GetVMHost.ExtensionData.AlarmActionsEnabled
    if ($alarmEnabled -eq $false){
        $alarmMgr.EnableAlarmActions($GetVMHost.ExtensionData.MoRef,$true)
    }
    $GetVMHost = $null
}