使用Powershell启动Delta同步时如何修复ADSync异常错误

时间:2019-05-24 17:03:50

标签: powershell office365

当我尝试通过pssession启动Delta Sync时,会引发错误。如果我一次只运行一条命令,它将运行得很好。

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

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

  

'Microsoft.DirectoryServices.MetadirectoryServices.UI.PropertySheetBase.UIUtils'   引发异常。

2 个答案:

答案 0 :(得分:0)

我通常通过帮助函数执行Start-ADSyncSyncCycle,该函数检测ADSyncScheduler是否被挂起,并且还检测是否已经在进行同步周期。不注意这些事情,您可能会收到错误。

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

希望有帮助

答案 1 :(得分:0)

最终为我解决的解决方案是调用命令,而不是输入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 
}