使用 powershell 启动 Delta sync 时如何修复 ADSync 异常错误

How to fix ADSync exception error when starting Delta sync with powershell

当我尝试通过 pssession 启动 Delta Sync 时,出现错误。如果我 运行 一次一行地执行命令,它 运行 完全没问题。

Enter-PSSession server_name
Get-Module ADSync | Import-Module
Start-ADSyncSyncCycle -PolicyType Delta 
Exit-PSSession

我希望它启动同步,但只是收到错误消息:

'Microsoft.DirectoryServices.MetadirectoryServices.UI.PropertySheetBase.UIUtils' threw an exception.

我通常通过检测 ADSyncScheduler 是否挂起以及检测是否可能已经有同步周期的辅助函数执行 Start-ADSyncSyncCycle。如果不注意这些事情,您可能会收到错误。

function Start-SyncCycle {
    [CmdletBinding()]
    param(
        [Parameter(Mandatory = $false, Position = 0)]
        [ValidateSet('Delta','Initial')]
        [string]$PolicyType = 'Delta'
    )
    # test if the ADSyncScheduler is not suspended
    $scheduler = Get-ADSyncScheduler
    if ($scheduler.SchedulerSuspended) {
        # SchedulerSuspended is set during an upgrade to temporarily block the scheduler from running. 
        # When this property is $true, running Start-ADSyncSyncCycle wil result in error:
        # System.InvalidOperationException: Scheduler is already suspended via global parameters.

        Set-ADSyncScheduler -SchedulerSuspended $false
    }
    # test if a scheduled synchronization is already running
    if ($scheduler.SyncCycleInProgress) {  # or use: if (Get-ADSyncConnectorRunStatus) { ... }
        Write-Warning "A sync is already in progress. Please try again later."
    }
    else {
        Write-Host "Initializing Azure AD $PolicyType Sync..." -ForegroundColor Yellow
        try {
            Start-ADSyncSyncCycle -PolicyType $PolicyType -ErrorAction Stop | Out-Null

            Write-Host "Waiting for Sync to start.."
            # give the Sync Connector some time to start up (10 seconds should be enough)
            Start-Sleep -Seconds 10

            Write-Host "Waiting for Sync to finish.."
            While(Get-ADSyncConnectorRunStatus) {
                Write-Host "." -NoNewline
                Start-Sleep -Seconds 5
            }
            Write-Host
            Write-Host "Azure AD $PolicyType Sync has finished." -ForegroundColor Green

        }
        catch {
            Write-Error $_.Exception.Message
        }
    }
}

你这样使用函数:

$cred = Get-Credential -UserName "your.username@yourdomain.com" -Message "Please enter credentials for yourdomain.com"
Invoke-Command -ComputerName $AzureConnectServer -ScriptBlock ${function:Start-SyncCycle} -Credential $cred

希望对您有所帮助

最终为我解决的解决方案是调用命令而不是输入 pssession。我不确定为什么会这样,但确实如此。

$cred = Get-Credential
Invoke-Command -ComputerName server_name -Credential $cred -ScriptBlock{
    Import-Module -Name "C:\Program Files\Microsoft Azure AD Sync\Bin\ADSync"
    Start-ADSyncSyncCycle -PolicyType Delta 
}