ps/Modules/Alkami.PowerShell.Common/Public/Close-SMBApplicationLocks.ps1

72 lines
3.1 KiB
PowerShell
Raw Permalink Normal View History

2023-05-30 22:51:22 -07:00
function Close-SMBApplicationLocks {
<#
.SYNOPSIS
Closes Active SMB Sessions for Default or User Supplied Paths
.DESCRIPTION
This function is used to prevent interruption to deployments by closing any SMB locks
in application paths. Defaults to closing sessions in folders matching regex
"ORBLogs|ORB|CHOCOLATEY|WinTest"
.PARAMETER Paths
[string[]] A string array of paths or path segments to match sessions against.
.EXAMPLE
Close-SMBApplicationLocks
[Close-SMBApplicationLocks] : Closing FileId 3588176742489 on SMB Session 2717036577625 for user CORP\cmcdonald in path C:\ORBLogs
[Close-SMBApplicationLocks] : Closing FileId 3588176764644 on SMB Session 2717036577625 for user CORP\cmcdonald in path C:\ORBLogs
.EXAMPLE
Close-SMBApplicationLocks -Paths @("TEMP")
[Close-SMBApplicationLocks] : Closing FileId 3588176742489 on SMB Session 2717103686265 for user CORP\dsage in path C:\Temp
[Close-SMBApplicationLocks] : Closing FileId 3588176742765 on SMB Session 2717103686265 for user CORP\dsage in path C:\temp\deploy
[Close-SMBApplicationLocks] : Closing FileId 3588176742324 on SMB Session 2717103686265 for user CORP\dsage in path C:\temp\deploy
[Close-SMBApplicationLocks] : Closing FileId 3588176746854 on SMB Session 2717103686265 for user CORP\dsage in path C:\Temp
[Close-SMBApplicationLocks] : Closing FileId 3588176787652 on SMB Session 2717103686265 for user CORP\dsage in path C:\Temp
[Close-SMBApplicationLocks] : Closing FileId 3588176712345 on SMB Session 2717103686265 for user CORP\dsage in path C:\temp\deploy
#>
[CmdletBinding()]
param(
[Alias("SharePaths")]
[Parameter(Mandatory=$false)]
[string[]]$Paths = @("ORBLogs","ORB","CHOCOLATEY","WinTest")
)
$logLead = Get-LogLeadName
$pathsExpression = $Paths -join "|"
Write-Host ("$logLead : Looking for SMB Sessions Matching Path: {0}" -f $pathsExpression)
$smbSessions = @(Get-SmbOpenFile | Where-Object {$_.Path -match $pathsExpression})
if ((Test-IsCollectionNullOrEmpty -Collection $smbSessions)) {
Write-Host "$logLead : No Matching SMB Sessions Found"
return
}
Write-Host "$logLead : Found $($smbSessions.Count) Matching SMB Sessions"
$uniqueFileIds = ($smbSessions).FileId | Sort-Object -Unique
foreach ($fileId in $uniqueFileIds) {
$session = @($smbSessions | Where-Object { $_.FileId -eq $fileId })[0]
$sessionId = $session.SessionId
$username = $session.ClientUserName
$path = $session.Path
Write-Host "$logLead : Closing FileId $fileId on SMB Session $sessionId for user $username in path $path"
try {
## Yes this is FOUR ways to suppress output. Microsoft has proven remarkably resilient at showing an error here.
## This is a CIM function that we can't force to behave like PowerShell so ... good times
(Close-SmbOpenFile -FileId $fileId -Force -ErrorAction Ignore *>&1) | Out-Null
} catch {
$errorMessage = $_.Exception.Message
Write-Warning "$logLead : An Error Occurred While Trying to Close Session $sessionId : $errorMessage"
}
}
}