Recently, Microsoft deprecated use of the remote PS sessions using version 1. We have since converted all of our scripts to version 2, but our nightly staff changes that used to process without issue are now failing on several tasks with the error :
Your app has been throttled by ADD dude to too many requests.
Is there a way I need to modify the following code to allow all processes to complete properly as they did when using version 1?
Thanks
$groupNames = @("SG_EXAMPLE")
try
{
# Get the object ID in Microsoft 365
$objectId = [Guid]$Context.TargetObject.Get("adm-O365ObjectId")
}
catch
{
return # The user doesn't have a Microsoft 365 account
}
try
{
# Connect to Exchange Online
Connect-ExchangeOnline -Credential $Context.GetOffice365Credential()
foreach ($groupName in $groupNames)
{
# Add user to group
try
{
Add-DistributionGroupMember $groupName -Member $objectId.ToString() -BypassSecurityGroupManagerCheck -ErrorAction Stop
}
catch
{
$Context.LogMessage("An error occurred while adding the user to group $groupName. Error: " + $_.Exception.Message, "Warning")
}
}
}
finally
{
# Close the remote session and release resources
Disconnect-ExchangeOnline -Confirm:$false
}