SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a web service that is kicked off from a workflow. The web service runs a powershell script that kicks off the User Profile Synchronization job.

The powershell script works perfectly if I run the script on the server. When I have the webservice kick off the script I get errors:

System.Management.Automation.MethodInvocationException: Exception calling "StartSynchronization" with "1" argument(s): "Cannot complete this action.

Please try again." ---> Microsoft.SharePoint.SPException: Cannot complete this action.

Please try again. ---> System.Runtime.InteropServices.COMException: Cannot complete this action.

Has anyone done anything like this before? Any idea why this doesn't work?

Here is my WebMethod in the web service:

public void RunUserProfileSync()
    RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();

    Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);

    RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);

    Pipeline pipeline = runspace.CreatePipeline();

    // Execute PowerShell script

    public string sychronizationScript()
        System.IO.StreamReader powerShellScript= new System.IO.StreamReader("C:\\SynchronizationScript.ps1");
        var pSScript = powerShellScript.ReadToEnd();
        return pSScript;

Here is the powershell script that I'm running:

#------- Get-SPServiceContext

Add-Type -Path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI\Microsoft.Office.Server.UserProfiles.dll'
Add-PSSnapin "Microsoft.Sharepoint.PowerShell"

function Get-SPServiceContext([Microsoft.SharePoint.Administration.SPServiceApplication]$profileApp)
    if($profileApp -eq $null)
        #----- Get first User Profile Service Application
        $profileApp = @(Get-SPServiceApplication | ? { $_.TypeName -eq "User Profile Service Application" })[0]
return [Microsoft.SharePoint.SPServiceContext]::GetContext(

$serviceContext= Get-SPServiceContext
$configManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileConfigManager($serviceContext)
if($configManager.IsSynchronizationRunning() -eq $false)
Write-Host "Started Synchronizing"
Write-Host "Already Synchronizing"

any help would be appreciated!

share|improve this question

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.