Powershell脚本,用于通过Web应用程序中的子网站附加工作流

时间:2017-03-30 14:08:37

标签: powershell sharepoint

我创建了一个PowerShell脚本,它将工作流程附加到网站集和子站点。但我希望将其自动化到网络应用程序下的所有网站集和子网站:

function AddWorkflowToLibraries ($SiteCollection, $ctName, $WfName, $WfAssociationName)
{
//here I need to add something to trace the webapp and sitecollections. I need help
$site = Get-SPSite $SiteCollection
[Guid]$wfTemplateId = New-Object Guid

//Step through each web in site collection
$site | Get-SPWeb -limit all | ForEach-Object {
    $web = $_
    $_.Lists | ForEach-Object{
        if($_.AllowContentTypes -eq $true)
        {
            if($_.ContentTypes.Item("$ctName") -ne $null)
            {
                write-host "Enabling workflow on" $_.Title "in" $_.ParentWebUrl

                $ct = $_.ContentTypes[$ctName]
                $culture = New-Object System.Globalization.CultureInfo("en-US")
                $template = $site.RootWeb.WorkflowTemplates.GetTemplateByName($WfName, $culture)

                if($template -ne $null)
                {
                    $tasklist = "Tasks"
                    $historylist = "Workflow History"
                    //workflow history list added
                    if(!$web.Lists[$historylist])
                    {
                        $web.Lists.Add($historylist, "A system library used to store workflow history information that is created in this site.  It is created by the Publishing feature.", 
                        "WorkflowHistory", "00BFEA71-4EA5-48D4-A4AD-305CF7030140", 140, "100")
                        if (!$web.Features["00BFEA71-4EA5-48D4-A4AD-305CF7030140"]) {
                            Enable-SPFeature -Identity WorkflowHistoryList -Url $web.Url
                        }
                        $wfHistory = $web.Lists[$historylist]
                        $wfHistory.Hidden = $true
                        $wfHistory.Update()
                    }

                    if(!$web.Lists[$tasklist])
                    {
                   //task list linked if required by the workflow
                        $web.Lists.Add($tasklist, "This system library was created by the Publishing feature to store workflow tasks that are created in this site.", "WorkflowTasks", "00BFEA71-A83E-497E-9BA0-7A5C597D0107", 107, "100")
                    }
            //workflow association done with below command
                    $association = [Microsoft.SharePoint.Workflow.SPWorkflowAssociation]::CreateListContentTypeAssociation($template, $wfName, $web.Lists[$tasklist], $web.Lists[$historylist])
                  //workflow setting manual enabled
                    $association.AllowManual = $true
                   //workflow setting on onchange enabled
                    $association.AutoStartChange = $true

                    $_.ContentTypes[$ctname].AddWorkflowAssociation($association)
                    $_.ContentTypes[$ctname].Update()
                    #$ct.UpdateWorkflowAssociationsOnChildren($true,$true,$true,$false)
                }
                else
                {
                    Write-Error "Workflow Template not found"
                }
            }
        }
    }
}
}

该功能由以下命令

执行
AddWorkflowToLibraries 'WebApp URL' 'Content Type Name' 'Workflow Name' 'Workflow Association name' this command sends the parameter to the function

是否可以帮助您了解如何通过webapp附加工作流程。我尝试过使用$site = Get-SPSite –webapplication $SiteCollection,但收到错误:

  

错误无法调用具有空值表达式的方法。

0 个答案:

没有答案