Maybe there is a better way to accomplish this, so I'm open to suggestion.
We are in a hybrid setup between onprem and M365. When we create a new user, they are automatically added to a Security Group in our onprem AD. Membership in that group then gets synced to a Mail Enabled Security Group in Exchange Online. We rely on a scheduled PowerShell script to sync the group membership.
The script is helpful because does both these functions:
- If a user is added to the onprem group, they are added to the cloud group
- If a user is removed from the onprem group, they are removed from the cloud group
The PowerShell script stopped working due to a cmdlet being depreciated - "CreateExchangeOnlinePSSession". So I upgraded our Adaxes to the latest version and, per the Adaxes KB, switched the script to use the "ConnectExchangeOnline" instead.
But now we're getting an error related to the Import-PSSession $session part of the script:
#Connect to Exchange Online
$session = $Context.CloudServices.ConnectExchangeOnline()
Import-PSSession $session -AllowClobber -DisableNameChecking -CommandName "Get-DistributionGroupMember", "Add-DistributionGroupMember", "Remove-DistributionGroupMember"
The error is:
Cannot validate argument on parameter 'Session'. The argument is null.
Any thoughts on why it might be saying the 'Session' parameter is null? It should be getting populated by the ConnectExchangeOnline command in the previous step, right?
If needed, I can provide the full script.