Hi @Jeff Kartechner ,
Thanks for sharing your script and the error details!
From what you described, I think the main reason your script is failing on Windows Server 2022 is that it tries to look up Azure AD groups using their display name, but sometimes the display name is blank.
When PowerShell sees an empty string for the -DisplayName parameter in the Get-AzADGroup command, it throws an error saying it can’t accept an empty string. This can happen if a group was renamed, deleted, or if there’s a data issue in Azure AD. It’s not a PowerShell version problem. Both your servers are running the same version but rather a difference in the data returned by Azure or the modules.
I also think the problem happens in two places in your script, both times where you have a line like this:
$GroupObj = Get-AzADGroup -DisplayName $role.DisplayName
If $role.DisplayName is empty, the script crashes.
To make your script more reliable, I’d recommend adding a quick check before calling Get-AzADGroup. If the display name is empty, you can look up the group by its object ID instead, which is always unique and present. Here’s how I would change those lines:
$GroupObj = if ([string]::IsNullOrWhiteSpace($role.DisplayName)) {
Get-AzADGroup -ObjectId $role.ObjectId
} else {
Get-AzADGroup -DisplayName $role.DisplayName
}
Just replace both instances of the original line with this new version.
Using the group’s ObjectId is a reliable approach because every Azure AD group always has one, even if the display name is missing or blank. The Get-AzADGroup command fully supports looking up groups by ObjectId, as shown in Microsoft’s documentation for Get-AzADGroup.
If you run into the same error elsewhere in your script (for example, if you look up groups by display name in other places), you can use the same pattern there too.
Hope this helps! If you still have any questions, please feel free to comment below. I'll be happy to help out!