function Invoke-WPFPopup { param ( [ValidateSet("Show", "Hide", "Toggle")] [string]$Action = "", [string[]]$Popups = @(), [ValidateScript({ $invalid = $_.GetEnumerator() | Where-Object { $_.Value -notin @("Show", "Hide", "Toggle") } if ($invalid) { throw "Found invalid Popup-Action pair(s): " + ($invalid | ForEach-Object { "$($_.Key) = $($_.Value)" } -join "; ") } $true })] [hashtable]$PopupActionTable = @{} ) if (-not $PopupActionTable.Count -and (-not $Action -or -not $Popups.Count)) { throw "Provide either 'PopupActionTable' or both 'Action' and 'Popups'." } if ($PopupActionTable.Count -and ($Action -or $Popups.Count)) { throw "Use 'PopupActionTable' on its own, or 'Action' with 'Popups'." } # Collect popups and actions $PopupsToProcess = if ($PopupActionTable.Count) { $PopupActionTable.GetEnumerator() | ForEach-Object { [PSCustomObject]@{ Name = "$($_.Key)Popup"; Action = $_.Value } } } else { $Popups | ForEach-Object { [PSCustomObject]@{ Name = "$_`Popup"; Action = $Action } } } $PopupsNotFound = @() # Apply actions foreach ($popupEntry in $PopupsToProcess) { $popupName = $popupEntry.Name if (-not $sync.$popupName) { $PopupsNotFound += $popupName continue } $sync.$popupName.IsOpen = switch ($popupEntry.Action) { "Show" { $true } "Hide" { $false } "Toggle" { -not $sync.$popupName.IsOpen } } } if ($PopupsNotFound.Count -gt 0) { throw "Could not find the following popups: $($PopupsNotFound -join ', ')" } }