Is there a more elegant way to write the following if statement in PowerShell
[ValidateNotNull()]
[ValidateSet('Service', 'Role', 'RoleService', 'Feature', 'Group', 'File', 'Package')]
[Parameter(Mandatory = $true, Position = 1)]
[string[]]
$ProcessingModes
if ($ProcessingModes -contains 'Role' -or $ProcessingModes -contains 'RoleService' -or $ProcessingModes -contains 'Feature')
{
}
You can do array intersection quite easily with this:
$keyModes = 'Role', 'RoleService', 'Feature'
if ($keyModes | ? { $ProcessingModes -contains $_ }) { "found at least one" }
Your question is basically, is there an operator in PowerShell which determines whether the intersection between two arrays is non-empty. The answer is no, there is not. Additionally, after reviewing the question Powershell, kind of set intersection built-in?, it looks like the best approach is to use HashSet objects instead of arrays, which do expose IntersectWith and UnionWith methods.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With