Created
November 23, 2013 20:03
-
-
Save michaellwest/7619225 to your computer and use it in GitHub Desktop.
Function to retrieve locked items in the Sitecore CMS.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Get-LockedChildItem { | |
<# | |
.SYNOPSIS | |
Gets the locked item at the specified location. | |
.PARAMETER Path | |
Specifies a path to search for locked items. The default location is the current directory (.). | |
.PARAMETER LockedBy | |
Specifies the the owner account locked on the item. | |
.PARAMETER Unlock | |
Indicates the locked items should be unlocked. | |
.PARAMETER Recurse | |
Specifies all the child items at the specified path should be included in the search. | |
.EXAMPLE | |
PS master:\> Get-LockedChildItem | |
Returns all locked child items at the current path. | |
.EXAMPLE | |
PS master:\> Get-LockedChildItem -LockedBy 'sitecore\admin' | |
Returns all locked child items at the current path for the specified user. | |
.EXAMPLE | |
PS master:\> Get-LockedChildItem -LockedBy $me -Unlock -Recurse | |
Returns all locked child items at the current path and below for the specified user. The items are also unlocked. | |
#> | |
[CmdletBinding()] | |
param( | |
[ValidateNotNullOrEmpty()] | |
[string]$Path = ".", | |
[string]$LockedBy, | |
[timespan]$IdleTime=[timespan]::Zero, | |
[switch]$Unlock, | |
[switch]$Recurse | |
) | |
filter Locked { | |
$skip = $false | |
if($_.Locking -and $_.Locking.IsLocked()) { | |
$lockField = [Sitecore.Data.Fields.LockField]$_.Fields[[Sitecore.FieldIDs]::Lock] | |
if($lockField) { | |
$owner = $lockField.Owner | |
if($LockedBy) { | |
if($owner -ne $LockedBy) { | |
Write-Verbose "Skipping locked item $($_.Paths.Path) with different owner." | |
$skip = $true | |
} | |
} | |
if($IdleTime -gt [timespan]::Zero) { | |
$matched = [Sitecore.Web.Authentication.DomainAccessGuard]::Sessions | Where-Object { $_.UserName -eq $owner } | |
if($matched -and $matched.LastRequest.Add($IdleTime) -gt [datetime]::Now) { | |
$user = [Sitecore.Security.Accounts.User]::FromName($owner, $false) | |
if($user -and $user.IsAdministrator) { | |
Write-Verbose "Skipping locked item $($_.Paths.Path) owned by administrator." | |
$skip = $true | |
} | |
} | |
} | |
if(!$skip) { | |
if($Unlock) { | |
Write-Log "Unlocking item $($_.Paths.Path) which exceeded the specified idle time - locked by $($owner)" | |
$_.Editing.BeginEdit() | Out-Null | |
$_.Locking.Unlock() | Out-Null | |
$_.Editing.EndEdit() | Out-Null | |
} | |
$_ | |
} | |
} | |
} | |
} | |
Get-ChildItem -Path $Path -Recurse:$Recurse | Locked -Verbose:$Verbose | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment