-
Notifications
You must be signed in to change notification settings - Fork 0
/
Discard All checked out items.ps1
97 lines (79 loc) · 6.06 KB
/
Discard All checked out items.ps1
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
# Script that reports checked out files in a site
# Configuration variables
$URL = "http://XXXXXXXXXXXXX" # URL of the site
### DO NOT TOUCH THE CODE BELOW ###
# Required if not run in SharePoint Management Shell
if($ver.Version.Major -gt 1)
{
$Host.Runspace.ThreadOptions = "ReuseThread"
}
If((Get-PSSnapin -Name Microsoft.Sharepoint.Powershell -ErrorAction SilentlyContinue) -eq $null)
{
Add-PSSnapin Microsoft.Sharepoint.Powershell
}
$WebApp = Get-SPWeb $URL
If(!($WebApp)) { Write-Output "Website not found. Verify URL" -ForegroundColor Red; Break;}
Else { Write-Output "URL is valid: $($WebApp)"}
function UndoCheckedOutItems($WebApp)
{
Write-Output "Scanning Site: $($WebApp.Url)"
# Checking files in all Document Library items
foreach ($list in ($WebApp.Lists | ? {$_ -is [Microsoft.SharePoint.SPDocumentLibrary]})) {
Write-Output "Scanning List: $($list.RootFolder.ServerRelativeUrl)"
foreach ($item in $list.CheckedOutFiles) {
if (!$item.Url.EndsWith(".aspx")) { continue }
$writeTable = @{
"URL" = $WebApp.Site.MakeFullUrl("$($WebApp.ServerRelativeUrl.TrimEnd('/'))/$($item.Url)");
"Checked Out By" = $item.CheckedOutBy;
"Author" = $item.File.CheckedOutByUser.Name;
"Checked Out Since" = $item.CheckedOutDate.ToString();
"File Size (KB)" = $item.File.Length/1000;
"Email" = $item.File.CheckedOutByUser.Email;
}
$file = $item.File;
$file.ReleaseLock($file.LockId);
try
{
$file.UndoCheckOut();
}
catch
{
$file.CheckIn("Force check in for removal"); #If it's not possible to discard checkout
}
$file.Update();
New-Object PSObject -Property $writeTable
}
# Checking files in all list items
foreach ($item in $list.Items) {
if ($item.File.CheckOutStatus -ne "None") {
if (($list.CheckedOutFiles | where {$_.ListItemId -eq $item.ID}) -ne $null) { continue }
$writeTable = @{
"URL" = $WebApp.Site.MakeFullUrl("$($WebApp.ServerRelativeUrl.TrimEnd('/'))/$($item.Url)");
"Checked Out By" = $item.File.CheckedOutByUser.LoginName;
"Author" = $item.File.CheckedOutByUser.Name;
"Checked Out Since" = $item.File.CheckedOutDate.ToString();
"File Size (KB)" = $item.File.Length/1000;
"Email" = $item.File.CheckedOutByUser.Email;
}
$file = $item.File;
$file.ReleaseLock($file.LockId);
try
{
$file.UndoCheckOut();
}
catch
{
$file.CheckIn("Force check in for removal"); #If it's not possible to discard checkout
}
$file.Update();
New-Object PSObject -Property $writeTable
}
}
}
foreach($subWeb in $WebApp.Webs)
{
UndoCheckedOutItems($subWeb)
}
$WebApp.Dispose()
}
UndoCheckedOutItems($WebApp)