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
<#
.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
    )
    # 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

    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
    }
}

Export-ModuleMember -Function *-*