1

I need some help output the result of this PowerShell scripts to a file. https://gallery.technet.microsoft.com/office/How-get-the-workflow-7d00eed9#content

I have a similar scripts I use to check execution status of SharePoint 2013 workflow on a list. but I think it will be more productive if can use this PowerShell scripts on TechNet. the scripts found on TechNet works as expected but I think it will be better if I can output result to a file our send the output to an email.

my end goal is to have a scripts that can check the execution status of all workflow within a site and get the status of all workflow that has been executed within the last 4hours and either output the result to a file or send to an email.

My old scripts

Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue

$web = get-spweb -Identity https://mysharepointsite
$list = $web.Lists["my list"]

$wfm = New-object Microsoft.SharePoint.WorkflowServices.WorkflowServicesManager($web)
$sub = $wfm.GetWorkflowSubscriptionService()
$wfs = $sub.EnumerateSubscriptionsByList($list.ID)
$wfis=$wfm.GetWorkflowInstanceService()
$array=@()
foreach ($item in $list.Items)
{
    $workflowInstances=$wfis.EnumerateInstancesForListItem($list.ID,$item.ID)

    foreach($wf in $workflowInstances) 
    {
        $wfName = $wfs | ?{$_.Id -eq $wf.WorkflowSubscriptionId} | select -ExpandProperty Name
        $wfID= $wf.ID
        $wfStatus = $wf.Status
        $wfListItem = $item.Name
        $WFLastUpDateTime=$wf.LastUpdated.ToLocalTime()
        $modifiedDateTime = Get-Date $WFLastUpDateTime


        if ($modifiedDateTime -gt (get-date).addhours(-4))
            {
        #write-host "Item Name:$wfListItem, Workflow Name: $wfName, Current Workflow Status: $wfStatus, Last Run: $modifiedDateTime" -f Yellow
$array+="Item Name:$wfListItem, Workflow Name: $wfName, Current Workflow Status: $wfStatus, Last Run: $modifiedDateTime"
}
 }
}

$array | Out-File "C:\workflowinfo.txt"
1

Using the Client Side Object Model (CSOM) as noted on the TechNet to get the workflow status would have been the best option for me but the output file is not displaying the way I wanted it.

I decided to changes my original scripts to iterate through all list/library within a site collections and output the workflow status.

The scripts below will check all the list/library with the site collection and output all workflow status that has been run within the last hour.

Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue
$web = Get-SPWeb -Identity https://mysharepointsite
$wfm = New-Object Microsoft.SharePoint.WorkflowServices.WorkflowServicesManager($web)
$sub = $wfm.GetWorkflowSubscriptionService()
foreach ($list in $web.Lists)
{    
    $wfs = $sub.EnumerateSubscriptionsByList($list.ID)
    $wfis = $wfm.GetWorkflowInstanceService()

    foreach ($item in $list.Items)
    {
        $workflowInstances = $wfis.EnumerateInstancesForListItem($list.ID, $item.ID)

        foreach ($wf in $workflowInstances) 
        {
            $wfName = $wfs | ?{$_.Id -eq $wf.WorkflowSubscriptionId} | select -ExpandProperty Name
            $wfID = $wf.ID
            $wfStatus = $wf.Status
            $wfListItem = $item.Name
            $WFLastUpDateTime = $wf.LastUpdated.ToLocalTime()
            $modifiedDateTime = Get-Date $WFLastUpDateTime

            if ($modifiedDateTime -gt (get-date).AddHours(-1))
            {
                #write-host "List name: $list, Workflow Title: $wfName, ListItem Name: $wfListItem,  WorkflowStatus: $wfStatus, last Workflow: $modifiedDateTime" -f yellow
                Write-Output "List name: $list, Workflow Title: $wfName, ListItem Name: $wfListItem,  WorkflowStatus: $wfStatus, last Workflow: $modifiedDateTime" | Add-Content "C:\output\workflowstatus.txt"
            }
        }
    }
}
$web.Dispose()

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.