PowerShell module to manage lock files using the current process and computer name. This type of locking is useful in the event there is a script that needs to be highly available running on multiple computers but only one instance can be active at a time.
# PowerShellGet
Install-Module LockFile
# PSResourceGet
Install-PSResource LockFile
If creating a lock file that needs to be read from multiple machines use a shared path.
Set-LockFile -Path \\scripts\myscript.lock
To get the configuration of the lock file.
Get-LockFile -Path \\scripts\myscript.lock
ComputerName ProcessId ProcessName
------------ --------- -----------
TestDC 4800 pwsh
Test-LockFile -Path \\scripts\myscript.lock
You can also pipe the outputs from Get-LockFile
.
Get-LockFile -Path \\scripts\myscript.lock | Test-LockFile
True
If the process that created the lock is on another computer create a session to the computer for the cmdlet to check the validity.
$lock = Get-LockFile -Path \\scripts\myscript.lock
$results = if ($env:ComputerName -ne $lock.ComputerName) {
$session = New-PSSession -ComputerName $lock.ComputerName
$lock | Test-LockFile -Session $session
} else {
$lock | Test-LockFile
}
$results
True