1

I'm trying to validate the output of the Get-AzADGroup command from the Az.Resources PowerShell module. When provided with a group display name, I want to make sure that command outputs a group object that:

  1. Exists
  2. Is assigned (static) membership type
  3. Is not synced from on-prem AD (cloud only group).

The command Get-AzADGroup does not produce an error when using a non-existent group name as the DisplayName parameter.

I've tried using a switch statement to validate the output:

$AzADGroupName = "non_existent_group"
$AzADGroupObject = Get-AzADGroup -DisplayName $AzADGroupName
switch ($AzADGroupObject)
    {
        {$_.GroupType -eq "DynamicMembership"}  {Write-Error "Group is dynamic."}
        {$_.AdditionalProperties.onPremisesSecurityIdentifier}  {Write-Error "Group is synced from on-prem."}
        {!$_.Id} {Write-Error "Group does not exist"}
    }

The first two cases in the switch statement work fine, but if the group does not exist, the last case does not match, even though the $AzADGroupObject appears to be empty/null. I've tried various other methods to detect whether or not the Get-AzADGroup command has populated the variable with an actual object. Using $null as the value in the switch statement doesn't work. Using the OutVariable parameter on the Get-AzADGroup command to set the AzADGroupObject variable also makes no difference.

What am I missing?

newguise
  • 13
  • 4
  • It may be confusing but `$null` and `AutomationNull` (what's returned from your cmdlet) are different things, in this case, the `switch` (or any other type of looping mechanism) will not enumerate it – Santiago Squarzon Nov 30 '22 at 03:20
  • Hopefully the linked answer explains your question. The right way to do it would be to return early from your script or function if `Get-AzADGroup` did not return any results, i.e.: `if(-not $AzADGroupObject) { ... return early here ... }` – Santiago Squarzon Nov 30 '22 at 03:32
  • also there is this excellent documentation which explains both in great detail: https://learn.microsoft.com/en-us/powershell/scripting/learn/deep-dives/everything-about-null?view=powershell-7.3 – Santiago Squarzon Nov 30 '22 at 03:33
  • Thanks so much @SantiagoSquarzon, that makes sense. I took the null case out of the switch and used an if statement prior as you suggested. – newguise Nov 30 '22 at 05:24

0 Answers0