-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathWF2010-Inactive-Report.ps1
More file actions
132 lines (110 loc) · 4.16 KB
/
WF2010-Inactive-Report.ps1
File metadata and controls
132 lines (110 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Config
$appid = "ff5db855-5db2-4537-b057-e13f8623aaa7"
$appsecret = "CLIENT-SECRET-HERE"
# One year threshold for active workflow
$threshold = -365
$thresholdDate = (Get-Date).AddDays($threshold)
# Main
function Main {
# Prepare CSV data collection
$global:coll = @()
$csv = Import-CSV "WF2010-Site-Collections.csv"
foreach ($row in $csv) {
InspectSite $row.URL
}
# Write CSV
$global:coll | Export-CSV "WF2010-Report.csv" -NoTypeInformation -Force
}
# Walk site collection and all child webs
function InspectSite ($webUrl) {
# Connect Tenant
Write-Host $webUrl -Fore "Green"
Connect-PNPOnline -Url $webUrl -ClientId $appid -ClientSecret $appsecret
$ctx = Get-PNPContext
# Loop Lists
$lists = Get-PNPList
foreach ($l in $lists) {
# Collect WF Associations
$WorkflowAssociations = $l.WorkflowAssociations
$ctx.load($WorkflowAssociations)
$ctx.ExecuteQuery()
# Loop WF 2010 Associations
foreach ($wa in $l.WorkflowAssociations) {
# Display
$waid = $wa.Id
# Search WF History (dynamic)
$listHistory = Get-PNPList $wa.HistoryListTitle
# Enable index columns
@("Occurred", "WorkflowAssociation") | % {
$fieldName = $_
$field = Get-PnPField -List $listHistory -Identity $fieldName
if ($field.Indexed -ne 1) {
$field.Indexed = 1
$field.Update()
$ctx.ExecuteQuery()
}
}
# Check WF History
# from https://www.sharepointdiary.com/2019/03/fix-get-pnplistitem-attempted-operation-prohibited-because-it-exceeds-list-view-threshold-enforced-by-administrator.html
$caml = '<View><Query><OrderBy><FieldRef Name="Occurred" Ascending="FALSE" /></OrderBy><Where><Eq><FieldRef Name="WorkflowAssociation" /><Value Type="Text">{' + $waid + '}</Value></Eq></Where></Query><RowLimit>1</RowLimit></View>'
# https://github.com/pnp/PnP-PowerShell/issues/879
$camlQuery = New-Object "Microsoft.SharePoint.Client.CamlQuery"
$camlQuery.ViewXml = $caml
$result = $listHistory.GetItems($camlQuery)
$ctx.Load($result)
try {
$ctx.ExecuteQuery()
}
catch {}
# Format Occurred Date
$lastRunTime = $null
if ($result.Count) {
$lastRunTime = $result[0].FieldValues["Occurred"]
}
# Display result
if ($result) {
if ($lastRunTime -gt $thresholdDate) {
# ACTIVE
$global:coll += [PSCustomObject]@{
Status = 'ACTIVE';
WebURL = $webUrl;
List = $l.Title;
Workflow = $wa.Name;
Enabled = $wa.Enabled;
WFAID = $waid;
LastRunTime = $lastRunTime
};
}
else {
# INACTIVE
$global:coll += [PSCustomObject]@{
Status = 'INACTIVE';
WebURL = $webUrl;
List = $l.Title;
Workflow = $wa.Name;
Enabled = $wa.Enabled;
WFAID = $waid;
LastRunTime = $lastRunTime
};
}
}
else {
# UNDETERMINED-QUERY-FAILED
$global:coll += [PSCustomObject]@{
Status = 'UNDETERMINED-QUERY-FAILED';
WebURL = $webUrl;
List = $l.Title;
Workflow = $wa.Name;
Enabled = $wa.Enabled;
WFAID = $waid;
LastRunTime = $lastRunTime
};
}
# ---
}
}
}
# Main
Start-Transcript
Main
Stop-Transcript