powershell 运行工作流程

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell 运行工作流程相关的知识,希望对你有一定的参考价值。

Add-PSSnapin Microsoft.SharePoint.PowerShell  -erroraction silentlycontinue

$listName = "WorkflowExamples"
$workflowName = "WorkflowEmail"
$url = "https://xxxx/dept/109/109.14/BO/SS/DavidJWilliams"

$startDate = Get-Date
Set-SPLogLevel -TraceSeverity Verbose -EventSeverity Verbose

$web = Get-SPWeb -Identity $url

$path = "f:\temp\WorkflowRun.txt"
If (Test-Path $path){
	Remove-Item $path
}

$manager = $web.Site.WorkFlowManager
 
# Name of the list
$list = $web.Lists[$listName]
 
# Name of the Workflow
$assoc = $list.WorkflowAssociations.GetAssociationByName($workflowName,"en-US")
 
$data = $assoc.AssociationData
$items = $list.Items
$assoc.AllowAsyncManualStart = $true
$assoc.AllowManual = $true

$item = $items[0]

$runningWorkflows = $item.Workflows | Where-Object {($_.InternalState -eq "Running, HasNewEvents")}

if($runningWorkflows -eq $null)
{
	write-host "No workflows running..starting the workflow...." -BackgroundColor Yellow -ForegroundColor Black
	$wf = $manager.StartWorkFlow($item,$assoc,$data,$true)
}

$runningWorkflows = $item.Workflows | Where-Object {($_.InternalState -eq "Running, HasNewEvents")}

$runCount = 0
while($runningWorkflows -ne $null)
{
	$runCount = $runCount + 1
	write-host "workflow is still running..$runCount..on item id: $($item.ID)" -BackgroundColor Green -ForegroundColor Black
	$testItem = $list.GetItemById($item.ID)
	$runningWorkflows = $testItem.Workflows | Where-Object {($_.InternalState -eq "Running, HasNewEvents")}

	Start-Sleep -s 10
	
	if($runCount -eq 6 -or $runCount -eq 12 -or $runCount -eq 18 -or $runCount -eq 24)
	{
		#[Microsoft.SharePoint.Workflow.SPWorkflowManager]::CancelWorkflow($runningWorkflows);
		$totalTime = $runCount * 10
		write-host "Workflow on item with ID $($item.id) has been running for $totalTime seconds" -BackgroundColor Blue -ForegroundColor White
		#$runningWorkflows = $null
	}
	
}#end while			
 
$manager.Dispose()
$web.Dispose()

Set-SPLogLevel -TraceSeverity High -EventSeverity Error
$endDate = Get-Date

Start-Sleep -s 15

$path = "F:\temp\temp.log"
If (Test-Path $path){
	Remove-Item $path
}

try
{
	Merge-SPLogFile -path $path -Overwrite -StartTime $startDate -EndTime $endDate
}
catch
{
    write-host "Error occured: $_"
}
#-Category "Information Policy Management"
notepad $path

以上是关于powershell 运行工作流程的主要内容,如果未能解决你的问题,请参考以下文章

Active Directory 命令在 PowerShell 下工作,但不适用于命令提示符

Powershell 3.0:保存工作脚本后,如果我关闭然后重新打开脚本,当我尝试运行它时会收到一条错误消息

Powershell“启动服务”cmdlet 不工作。也不向主机写入任何内容

运行 powershell -command 时找不到类型

在另一个 Powershell 脚本中运行一个 Powershell 脚本

Ansible 中的 Powershell 脚本