SingleInstance.psm1
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 |
<#
.SYNOPSIS PowerShell commandlet that enforces a single instance of the running script. .LINK https://github.com/alekdavis/SingleInstance #> #------------------------[ RUN-TIME REQUIREMENTS ]------------------------- #Requires -Version 4.0 #---------------------------[ MODULE VARIABLES ]--------------------------- # Mutex to prevent multiple instances from running concurrently. [System.Threading.Mutex]$Mutex = $null #---------------------------[ PUBLIC FUNCTIONS ]--------------------------- <# .SYNOPSIS Checks if the specified instance of the script is running. .DESCRIPTION Use this function to check if the instance of the script is running. The script instance is identified by an arbitrary ID specified by the caller. This cmdlet uses a mutex to detect if the script is running (the mutex must be released before the process exits via the Exit-SingleInstance). .PARAMETER ScriptID Id of the script that will be used to identify the script instance. It can be some unique value (such as GUID) that a calling script would know or another identifier, such as script path ($PSCommandPath). .EXAMPLE if (!(Enter-SingleInstance $PSCommandPath) throw "The script is already running." Checks if the script with the given path is already running and if not, sets a mutex to prevent other calls from entering. .LINK https://github.com/alekdavis/SingleInstance #> function Enter-SingleInstance { [CmdletBinding()] param ( [string] $ScriptId = $null ) # Allow module to inherit '-Verbose' flag. if (($PSCmdlet) -and (-not $PSBoundParameters.ContainsKey('Verbose'))) { $VerbosePreference = $PSCmdlet.GetVariableValue('VerbosePreference') } # Allow module to inherit '-Debug' flag. if (($PSCmdlet) -and (-not $PSBoundParameters.ContainsKey('Debug'))) { $DebugPreference = $PSCmdlet.GetVariableValue('DebugPreference') } $success = $false if (!$ScriptId) { $ScriptId = Get-ScriptPath } if (!$ScriptId) { throw "Cannot detect calling script path from module '$PSCommandPath'." } Write-Verbose "Creating single-instance mutex '$ScriptId'." $Script:Mutex = New-Object System.Threading.Mutex($true, $ScriptId, [ref] $success) if ($success) { return $true } return $false } <# .SYNOPSIS Releases single-instance mutex initialized in the Enter-SingleInstance function. .DESCRIPTION Use this function to release the single-instance mutex allocated by the Enter-SingleInstance function. If no mutex was allocated, e.g. if Enter-SingleInstance returned false, this function will do nothing. .EXAMPLE Exit-SingleInstance Releases the single-instance mutex. .LINK https://github.com/alekdavis/SingleInstance #> function Exit-SingleInstance { [CmdletBinding()] param ( ) # Allow module to inherit '-Verbose' flag. if (($PSCmdlet) -and (-not $PSBoundParameters.ContainsKey('Verbose'))) { $VerbosePreference = $PSCmdlet.GetVariableValue('VerbosePreference') } # Allow module to inherit '-Debug' flag. if (($PSCmdlet) -and (-not $PSBoundParameters.ContainsKey('Debug'))) { $DebugPreference = $PSCmdlet.GetVariableValue('DebugPreference') } if ($Script:Mutex) { Write-Verbose "Releasing single-instance mutex." $Mutex.ReleaseMutex() $Mutex.Dispose() $Mutex = $null } } <# .SYNOPSIS Returns the full path of the first PowerShell script in the call stack. .DESCRIPTION Use this function to get the full path of the calling PowerShell script from a PowerShell module. .PARAMETER Extension The extension by which the calling script must be identified. The default value is '.ps1'. .EXAMPLE $scriptPath = GetScriptPath Gets the full path of the calling .ps1 script. .LINK https://github.com/alekdavis/SingleInstance #> function Get-ScriptPath { [CmdletBinding()] param ( [string] $Extension = '.ps1' ) # Allow module to inherit '-Verbose' flag. if (($PSCmdlet) -and (-not $PSBoundParameters.ContainsKey('Verbose'))) { $VerbosePreference = $PSCmdlet.GetVariableValue('VerbosePreference') } # Allow module to inherit '-Debug' flag. if (($PSCmdlet) -and (-not $PSBoundParameters.ContainsKey('Debug'))) { $DebugPreference = $PSCmdlet.GetVariableValue('DebugPreference') } $callstack = Get-PSCallStack $i = 0 $max = 100 while ($true) { if (!$callstack[$i]) { Write-Verbose "Cannot detect callstack frame '$i' in 'Get-ScriptPath'." return $null } $path = $callstack[$i].ScriptName if ($path) { Write-Verbose "Callstack frame '$i': '$path'." $ext = [IO.Path]::GetExtension($path) if (($ext) -and $ext -eq $Extension) { return $path } } $i++ if ($i -gt $max) { Write-Verbose "Exceeded the maximum of '$max' callstack frames in 'Get-ScriptPath'." return $null } } return $null } Export-ModuleMember -Function *-* |