0

I have this script to run a workflow on a list of items. It starts the workflow, but it just goes straight to cancelled without actually running it. Am I missing something? If I run the workflow manually, it works.

I know it's not going into the workflow as I have some logging there that is not getting into the history log.

$ver = $host | select version
if ($ver.Version.Major -gt 1) {$host.Runspace.ThreadOptions = "ReuseThread"} 
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}

$sourceWebURL  = "http://intranet/finance"
$sourceSiteURL = "http://intranet"
$sourceListName = "Purchase Request Tracker";
$sourceWorkflow = "Purchase Request Tracker Workflow"

[Microsoft.SharePoint.SPSecurity]::RunWithElevatedPrivileges( { 
    #Site and List  
    $SPAssignment = Start-SPAssignment 
    $web = Get-SPWeb $sourceWebURL -AssignmentCollection $spAssignment 
    $site = Get-SPSite $sourceSiteURL

})


#List Name  
$list = $web.Lists[$sourceListName]; 

#Workflow Manager
$manager=$site.WorkFlowManager
$association=$list.WorkFlowAssociations.GetAssociationByName($sourceWorkflow, "en-US")
$association.AllowAsyncManualStart = $true
$association.AllowManual = $true

#Iterate through all Items in List and all Workflows on Items.  
#Filtered List
Write-Host "Restarting workflows.."
foreach ($item in $list.Items) { 
    if ($item["Status"] -like "*Pending") {
        Write-Host "$sourceWorkflow started on " $item.Name
        $data=$association.AssociationData
        $wf=$manager.StartWorkFlow($item,$association,$data,$true)
        #$item.Update()
    }
}

Write-Host Finished.
$web.Dispose() 
$site.Dispose()

Thanks!

0

You need to use impersonation step in powershell script, if I assume you are running the script with System Account.

You cannot trigger workflows using system account. You can check below poweshell script to impersonate web object using user ID

$user=$web.AllUsers.GetByID(1073741823)
$token = $user.UserToken;
$impWebObj= New-Object Microsoft.SharePoint.SPSite($web.Url, $token);
$imperWeb = $impWebObj.OpenWeb();

Reference: Powershell and Sharepoint: Impersonation

  • I did find my error and it is permission related. I have 2 accounts and I was running the script from the wrong account. But this is interesting, so ill take a look. Is there a way to run it as the sharepoint\system account? – Matt Mar 27 '18 at 6:37
  • Na..as per my understanding and knowledge you cannot run designer workflows using System Account.. – Ankitkumar Malde Mar 27 '18 at 6:55
  • seems to have worked for me – Matt Mar 27 '18 at 13:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.