Change DistributionGroup Settings Remote Domain

I am running a script that connects to a remote exchange computer in another domain and sets an attribute on a Distribution Group. When I run the lines of code manually in an Exchange PowerShell window on my machine, it runs as expected. Then when I run it in a script that calls the same remote PowerShell, it errors for what seems to be authentication.

Script that I am running:

# Create Variables
Set-Variable -Name Sam
Set-Variable -Name DisplayName
Set-Variable -Name TADServer 
Set-Variable -Name SADServer 
Set-Variable -Name SourceCred
Set-Variable -Name TargetCred
Set-Variable -Name EmailAddresses
Set-Variable -Name Attribute7 
Set-Variable -Name Session

# Clear Variables
Clear-Variable Sam
Clear-Variable DisplayName
Clear-Variable TADServer 
Clear-Variable SADServer 
Clear-Variable SourceCred
Clear-Variable TargetCred
Clear-Variable EmailAddresses
Clear-Variable Attribute7 
Clear-Variable Session

#Set Inital Variable Value
$TADServer = "Target DC FQDN"
$SADServer = "Source DC FQDN"

#Input Creditials for Source Active Directory
Write-Host = "Requesting Credentials for Source Active Directory."
$SourceCred = Get-Credential

#Input Creditials for Target Active Directory
#Write-Host = "Requesting Credentials for Target Active Directory."
#$TargetCred = Get-Credential

Import-Csv 'Path to CSV File' | ForEach-Object {

    # Connect Remote Exchange PowerShell Session
  Write-Host "Connecting to Remote Exchange Server"
  Enter-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://FQDN/powershell -Authentication Kerberos -Credential $SourceCred
    Write-Host "Connected to the Remote Exchange Server"
  Start-Sleep 10

    # Set Variables for Remote PowerShell Session
  $SADServer = "Source DC FQDN"
  $Attribute7 = "NoSyncO365"
    $DisplayName = $_."DisplayName"
    Write-Host "Importing Distribution Group: " $DisplayName
  $Sam = $_."SamAccountName"
  $EmailAddresses = $_."EmailAddresses"
    # Set Source Distribution Group extensionAttribute7 to Not Sync with O365
  Set-DistributionGroup $Sam -DomainController $SADServer -customAttribute7 $null
    Set-DistributionGroup $Sam -DomainController $SADServer -customAttribute7 $Attribute7
    Write-Host "Disonnected from the Remote Exchange Server"
  # Run AD Sync to remove Legacy User
  Start-Sleep -Seconds 120

    # Import Data to Target AD Distribution Group
  Enable-DistributionGroup -Identity $DisplayName
    Set-DistributionGroup $Sam -DomainController $TADServer -EmailAddresses $EmailAddresses
    Set-DistributionGroup $Sam -DomainController $TADServer -customAttribute7 $null
    Start-Sleep -Seconds 120


Can you post the actual error?

The error that I get is below:

An Active Directory error 0x52 occurred when trying to check the suitability of server ‘Source AD FQDN’. Error: 'Active directory response: A

local error occurred.’
+ CategoryInfo : NotSpecified: (:slight_smile: [Set-DistributionGroup], SuitabilityDirectoryException
+ FullyQualifiedErrorId : [Server=CAS1601,RequestId=ded099e1-575e-45c8-946d-70d3f363c463,TimeStamp=4/2/2020 5:02:42 PM] [FailureCategory=Cmdlet-Suitabi
lityDirectoryException] C37AB760,Microsoft.Exchange.Management.RecipientTasks.SetDistributionGroup
+ PSComputerName : ‘Target Exchange FQDN’

<p style=“background: white; vertical-align: baseline; margin: 0in 0in .25in 0in;”>When you crosspost the same question at the same time to different forums you should at least post links to the other forums along with your question to avoid people willing to you help making their work twice or more.</p>


Please check these links.


Can you try specifying the server to authenticate against? The issue seems to be with the user account or domain/dc.

Please let me know if it helps.