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 Exit-PSSession Write-Host "Disonnected from the Remote Exchange Server" # Run AD Sync to remove Legacy User ADSync-Delta 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 ADSync-Delta Start-Sleep -Seconds 120 }