I am wondering the best approach to create a single Powershell script that does the following:
$AADGroup = "ExampleGroup1"
Get-AzureADGroup -SearchString $AADGroup | Get-AzureADGroupMember | Select Mail | ConvertTo-Json
The output of the above looks like this:
[
{
"Mail": "[email protected]"
},
{
"Mail": "[email protected]"
}
]
$UserName = @('[email protected]')
There is this approach, and I suppose also another approach possibly where you could add the new user as part of the query command before using the ConvertTo-Json? Not sure which one could work best.
Thanks.
You can use a single script block ({ ... }
) invoked via the call operator (&
) in order to output both the original users' addresses and the to-be-added ones, both as single-property objects with a .Mail
property, and then convert the combined results to JSON:
& {
param([string[]] $User)
# Output the existing users (email addresses) as objects with a .Mail property.
Get-AzureADGroup -SearchString $AADGroup | Get-AzureADGroupMember | Select-Object Mail
# Convert the array of users (email addresses) to objects with a .Mail property.
$User | ForEach-Object { [pscustomobject] @{ Mail = $_ } }
} -User @('[email protected]', '[email protected]') |
ConvertTo-Json
Working only with objects and then converting to JSON is preferable to partially converting to JSON first, and then trying to update the JSON (something that PowerShell provides no direct support for).