Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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:

[WebMethod]
public void RunUserProfileSync()
{
    RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();

    Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
    runspace.Open();

    RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);

    Pipeline pipeline = runspace.CreatePipeline();
    pipeline.Commands.AddScript(sychronizationScript());

    // Execute PowerShell script
    pipeline.Invoke();
}

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

Here is the powershell script that I'm running:

#------- Get-SPServiceContext

[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
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(
        $profileApp.ServiceApplicationProxyGroup, 
        [Microsoft.SharePoint.SPSiteSubscriptionIdentifier]::Default) 
}

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

any help would be appreciated!

share|improve this question
add comment

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.