# enable upgrade at next reboot
$ToolsConfig = New-Object VMware.Vim.VirtualMachineConfigSpec
$ToolsConfig.tools = New-Object VMware.Vim.ToolsConfigInfo
$ToolsConfig.Tools.ToolsUpgradePolicy = 'upgradeAtPowerCycle'

# single VM, enable
$vm = Get-VM -Name itdscmt1.nd.gov
($vm | Get-View).ReconfigVM($ToolsConfig)

# get all WS
$AllVMs = Get-VM | Where-Object { $_.ExtensionData.summary.config.ManagedBy.Type -ne "placeholderVm" }
$AllVMsWindows = $AllVMs | Where-Object {$_.Guest.OSFullName -like "*Windows*"}

$AllVMsDTAP = $AllVMsWindows | Get-TagAssignment -Category DTAP
$AllVMsTest = $AllVMsDTAP | Where-Object {$_.Tag.Name -eq 'Test'}
$AllVMsProd = $AllVMsDTAP | Where-Object {$_.Tag.Name -eq 'Production'}

$AllVMsTest | Export-Csv "D:\OneDrive - State of North Dakota\AllTestVMs.csv"
$AllVMsProd | Export-Csv "D:\OneDrive - State of North Dakota\AllProdVMs.csv"

ForEach($VM in $AllVMsTest){
    ($VM | Get-View).ReconfigVM($ToolsConfig)
}  #### CHECK 2008 non-R2 x3

# get all Windows and ToolsVersion counts
$AllVMGuests = $AllVMs | Get-VMGuest
$AllVMGuests | select VMName, ToolsVersion

# disable upgrade at next reboot
$ToolsConfig = New-Object VMware.Vim.VirtualMachineConfigSpec
$ToolsConfig.tools = New-Object VMware.Vim.ToolsConfigInfo
$ToolsConfig.Tools.ToolsUpgradePolicy = 'manual'

$vm = Get-VM -Name itdscmt1.nd.gov
($vm | Get-View).ReconfigVM($ToolsConfig)


$AllVMs = Get-VM | Where-Object { $_.ExtensionData.summary.config.ManagedBy.Type -ne "placeholderVm" }



$AllVMs = Get-VM | Where-Object {$_.Name -like "*itd*" -and $_.Guest.OSFullName -notlike "*Windows*"}


# report
$AllVMsWindows | select Name,@{n='OS';e={$_.Guest.OSFullName}},@{n='ToolsVersion';e={($_ | Get-VMGuest).ToolsVersion}},@{n='ToolsPolicy';e={($_ | Get-View).config.tools.toolsupgradepolicy}} -ov ToolsReport | export-csv "D:\vmtools.csv"

# set prod
ForEach($VM in $AllVMsWindows){
    Write-Warning -Message ($VM.Name)
    ($VM | Get-View).ReconfigVM($ToolsConfig)
}