# report current status
$AllVMs = Get-VM  | Where-Object { $_.ExtensionData.Summary.Config.ManagedBy.Type -ne "placeholderVm" }
$WS2022VMs = $AllVMs | Where-Object { $_.ExtensionData.Guest.GuestFullName -like "*2022*" } | Sort-Object Name
$WS2022VMs | select Name, @{n = 'EfiSecureBootEnabled'; e = { $_.ExtensionData.Config.BootOptions.EfiSecureBootEnabled } }



$VMNames = @"
itdexchtest1.testnd.gov
itdexchtest2.testnd.gov
itdexch1.nd.gov
itdexch2.nd.gov
"@

$VMNames = ConvertTo-Array -MultiLineString $VMNames

# power off VMs
ForEach ($VMName in $VMNames) {
    Get-VM -Name $VMName | Where-Object { $_.ExtensionData.Summary.Config.ManagedBy.Type -ne "placeholderVm" } | Stop-VMGuest -Confirm:$false
}

# wait for all to be powered off
Get-VM -Name $VMNames

# disable secure boot on all of them
ForEach ($VMname in $VMNames) {
    $vm = Get-VM -Name $VMName | Where-Object { $_.ExtensionData.Summary.Config.ManagedBy.Type -ne "placeholderVm" }
    $spec = New-Object VMware.Vim.VirtualMachineConfigSpec
    $spec.Firmware = [VMware.Vim.GuestOsDescriptorFirmwareType]::efi
    $boot = New-Object VMware.Vim.VirtualMachineBootOptions
    $boot.EfiSecureBootEnabled = $false  # false to disable obviously
    $spec.BootOptions = $boot
    $vm.ExtensionData.ReconfigVM($spec)
}

# validate
Get-VM -Name $VMNames | Where-Object { $_.ExtensionData.Summary.Config.ManagedBy.Type -ne "placeholderVm" }| select Name, @{n = 'EfiSecureBootEnabled'; e = { $_.ExtensionData.Config.BootOptions.EfiSecureBootEnabled } }

# power on
ForEach($VMName in $VMNames){
    Get-VM -Name $VMName | Where-Object { $_.ExtensionData.Summary.Config.ManagedBy.Type -ne "placeholderVm" } | Start-VM
}