|
| 1 | +function Write-FormatSelectionCondition |
| 2 | +{ |
| 3 | + <# |
| 4 | + .SYNOPSIS |
| 5 | + Writes a selection condition. |
| 6 | + .DESCRIPTION |
| 7 | + Writes a formatting selection condition. |
| 8 | + .NOTES |
| 9 | + PowerShell formatting can be conditional. |
| 10 | +
|
| 11 | + A condition is expressed an `<SelectionCondition>` element, which must be inside of an `<EntrySelectedBy>` element.. |
| 12 | +
|
| 13 | + Multiple selection conditions are permitted in a single `<EntrySelectedBy>` element. |
| 14 | + |
| 15 | + They will be evaluated using `-or` (if any of the conditions is true, the formatting will be applied). |
| 16 | + #> |
| 17 | + param( |
| 18 | + # The type name of the selection set. |
| 19 | + [Parameter(ValueFromPipelineByPropertyName)] |
| 20 | + [string] |
| 21 | + $TypeName, |
| 22 | + |
| 23 | + # The name of the selection set. |
| 24 | + [Parameter(ValueFromPipelineByPropertyName)] |
| 25 | + [string] |
| 26 | + $SelectionSet, |
| 27 | + |
| 28 | + # The type names in the selection set. |
| 29 | + [Alias('SelectionCondition')] |
| 30 | + [ScriptBlock] |
| 31 | + $ScriptBlock |
| 32 | + ) |
| 33 | + |
| 34 | + begin { |
| 35 | + # First, create a queue to hold the selection conditions |
| 36 | + $selectionConditionQueue = [Collections.Queue]::new() |
| 37 | + } |
| 38 | + |
| 39 | + process { |
| 40 | + # Each selection condition is a separate XML element |
| 41 | + $selectionConditionXml = [xml](@( |
| 42 | + "<SelectionCondition>" |
| 43 | + # Consisting of a typename |
| 44 | + if ($TypeName) { |
| 45 | + if (-not ("<TypeName>$TypeName</TypeName>" -as [xml])) { |
| 46 | + "<TypeName>$([Security.SecurityElement]::Escape($TypeName))</TypeName>" |
| 47 | + } else { |
| 48 | + "<TypeName>$TypeName</TypeName>" |
| 49 | + } |
| 50 | + } |
| 51 | + # or a selection set name |
| 52 | + elseif ($SelectionSet) { |
| 53 | + if (-not ("<SelectionSetName>$SelectionSet</SelectionSetName>" -as [xml])) { |
| 54 | + "<SelectionSetName>$([Security.SecurityElement]::Escape($SelectionSet))</SelectionSetName>" |
| 55 | + } else { |
| 56 | + "<SelectionSetName>$SelectionSet</SelectionSetName>" |
| 57 | + } |
| 58 | + } |
| 59 | + # and a script block. |
| 60 | + # If no `-ScriptBlock` is provided, |
| 61 | + if (-not $ScriptBlock) { |
| 62 | + # it is assumed to be `$true`. |
| 63 | + "<ScriptBlock>`$true</ScriptBlock>" |
| 64 | + } else { |
| 65 | + "<ScriptBlock>$([Security.SecurityElement]::Escape($ScriptBlock))</ScriptBlock>" |
| 66 | + } |
| 67 | + "</SelectionCondition>" |
| 68 | + ) -join '') |
| 69 | + if (-not $selectionConditionXml) { return } |
| 70 | + # Save the XML to a string (for readablility), |
| 71 | + $xOut=[IO.StringWriter]::new() |
| 72 | + $selectionConditionXml.Save($xOut) |
| 73 | + # and add the string to the queue (remove the XML declaration). |
| 74 | + $selectionConditionQueue.Enqueue("$xOut" -replace '\<\?xml.+?\?>[\s\r\n]+') |
| 75 | + $xOut.Dispose() |
| 76 | + } |
| 77 | + |
| 78 | + end { |
| 79 | + # Finally, create an `<EntrySelectedBy>` element |
| 80 | + $EntrySelectedByXml = @( |
| 81 | + "<EntrySelectedBy>" |
| 82 | + # containing all of the selection conditions |
| 83 | + $selectionConditionQueue.ToArray() |
| 84 | + "</EntrySelectedBy>" |
| 85 | + ) -join '' -as [xml] |
| 86 | + # Save the XML to a string (for readablility), |
| 87 | + $xOut=[IO.StringWriter]::new() |
| 88 | + $EntrySelectedByXml.Save($xOut) |
| 89 | + # and return the string (remove the XML declaration). |
| 90 | + "$xOut" -replace '\<\?xml.+?\?>[\s\r\n]+' |
| 91 | + $xOut.Dispose() |
| 92 | + } |
| 93 | +} |
| 94 | + |
0 commit comments