PoshCode Archive  Artifact [c533457be3]

Artifact c533457be3fd761e344783c8085998043d5b7e64b18e757f1ff1de367a79e881:

  • File PS-file-locking.ps1 — part of check-in [0761cfb816] at 2018-06-10 12:58:38 on branch trunk — The code uses a file locking mechanism to ensure that the (user: Michael R size: 2060)

# encoding: utf-8
# api: powershell
# title: PS file locking
# description: The code uses a file locking mechanism to ensure that the
# version: 0.1
# type: script
# author: Michael R
# license: CC0
# x-poshcode-id: 1566
# x-archived: 2012-12-29T04:31:09
# x-published: 2012-01-11T12:36:00
#
# script beeing started is running exclusively. Mostly used where
# a powershell script is started parallel within a few seconds
#
#################################################
#Scriptname:     checklock.ps1
#Author:         Michael RΓΌefli (www.miru.ch)
#Date:           21.11.2009
#Description:    The code uses a file locking mechanism to ensure that the
#                script beeing started is running exclusively. Mostly used where
#                a powershell script is started parallel within a few seconds
#
#Usage:          Paste your code in the MAIN section and set the $lockfile paramater to match your needs

############################################

$lockfile = “d:\lock.lck”
$lockstatus = 0
While ($lockstatus -ne 1)
{
	If (Test-Path $lockfile)
	{
		echo “Lock file found!”
		$pidlist = Get-content $lockfile
		If (!$pidlist)
		{
			$PID | Out-File $lockfile
			$lockstatus = 1
		}
		$currentproclist = Get-Process | ? { $_.id -match $pidlist }
		If ($currentproclist)
		{
			echo “lockfile in use by other process!”
			$rndwait = New-Object system.Random
			$rndwait=	$rndwait.next(500,1000)
			echo “Sleeping for $rndwait Milliseconds”
			Start-Sleep -Milliseconds $rndwait
		}
		Else
		{
			Remove-Item $lockfile -Force
			$PID | Out-File $lockfile
			$lockstatus = 1
		}
	}
	Else
	{
		$PID | Out-File $lockfile
		$lockstatus = 1
	}
}

#———————————————————————————————–
## Main Script Part
## Here you can paste your code, in fact what the script has to do
echo “Hi, it seems that no other script is doing the same as me now.. so I can do my job exclusively”

## End of Main Part
#———————————————————————————————–
#remove the lockfile
Remove-Item $lockfile -Force