Search code examples
arrayspowershellif-statementcountscalar

IF contains more than one string do "this" else "this"


Trying to make a script that request more info (group Id) if there are SCOM groups with identical names:

function myFunction {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$true)]
        [string[]]$ObjectName
    )

    foreach ($o in $ObjectName) {
        $p = Get-SCOMGroup -DisplayName "$o" | select DisplayName
        <#
        if ($p contains more than one string) {
            "Request group Id"
        } else {
            "do this"
        }
        #>            
    }
}

Need help with the functionality in the comment block.


Solution

  • Wrap the value in an array subexpression @() and count how many entries it has:

    if(@($p).Count -gt 1){"Request group Id"}