Spuštění workflow na všech položkách/dokumentech pomocí PowerShellu

Vstupní parametry:

  • siteUrl – URL adresa na web kde se nachází seznam
  • listTitle – název seznamu
  • workflowName – název workflow (asociace)
  • workflowCulture – v jakém jazyce je název WF (default en-US)
  • restartIfRunning – pokud WF běží, zastaví se (default je NE, vynechá se zpracování)

 

Příklad:

.\StartListWorkflowOnAllItems.ps1 -siteUrl http://srvdev:81/ -listTitle „Documents“ -workflowName „Approval WF“ -restartIfRunning $true

 

StartListWorkflowOnAllItems.ps1 (2,62 kb)

 

<# 
   .Synopsis 
    Start workflow on all items in list or document library
   .Example
   .\StartListWorkflowOnAllItems.ps1 -siteUrl http://srvdev:81/ -listTitle "Documents" -workflowName "Approval WF" -restartIfRunning $true
   .Notes 
    NAME: StartListWorkflowOnAllItems.ps1
    AUTHOR: novotny@devit.cz
   .Link 
    http://www.devit.cz
#> 

param (
    [Parameter(Mandatory=$True)]
    [string]$siteUrl,
    [Parameter(Mandatory=$True)]
    [string]$listTitle,
    [Parameter(Mandatory=$True)]
    [string]$workflowName,
    [string]$workflowNameCulture = "en-US",
    [boolean]$restartIfRunning = $true
)

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") 

$web = Get-SPWeb -Identity $siteUrl

Write-Host "Opening site '$($web.Title)'"

$manager = $web.Site.WorkFlowManager

$list = $web.Lists[$listTitle]
if ($list -eq $null) {
    Write-Host "Unable to find list '$listTitle'" -ForegroundColor Red
    return
}
 
$assoc = $list.WorkflowAssociations.GetAssociationByName($workflowName, $workflowNameCulture)
if ($assoc -eq $null) {
    Write-Host "Unable to find WF association '$workflowName' for culture '$workflowNameCulture'" -ForegroundColor Red
    return
}
 
$data = $assoc.AssociationData
$items = $list.Items

Write-Host "Starting workflows in list '$($list.Title)'"
foreach($item in $items)
{
    $itemName = $item.Title
    if ($item.FileSystemObjectType -eq [Microsoft.SharePoint.SPFileSystemObjectType]::File) {
        $itemName = $item.File.Name
    }

    Write-Host "Starting on ID:$($item.ID) - '$itemName'"
    try {
        $runningWf = $null

        foreach($wf in $item.Workflows) {
            if ($wf.AssociationId -eq $assoc.Id -and ($workflow.InternalState -ne 'Completed' -and $workflow.InternalState -ne 'Cancelled')) {
                $runningWf = $wf
                break
            }
        }

        if ($restartIfRunning -eq $false -and $runningWf -ne $null) {
            Write-Host "   Already Running... Skipping" -ForegroundColor Green
            continue
        }

        if ($runningWf -ne $null) {
            Write-Host "   Stopping existing WF" -ForegroundColor Green
            [Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($runningWf)
        }
        
        $wf = $manager.StartWorkFlow($item, $assoc, $data, $true)
        Write-Host "   OK" -ForegroundColor Green
    }
    catch [System.Exception] {
        Write-Host "   ERROR:" $_ -ForegroundColor Red
    }
}
  
$manager.Dispose()
$web.Dispose()

Napsat komentář

Tato stránka používá Akismet k omezení spamu. Podívejte se, jak vaše data z komentářů zpracováváme..