Code Monkey home page Code Monkey logo

singleinstance's Introduction

SingleInstance.psm1

This PowerShell module allows you to prevent multiple instances of the same PowerShell script from running at the same time.

Cmdlet

The SingleInstance module exports the following functions:

  • Enter-SingleInstance
  • Exit-SingleInstance
  • Get-ScriptPath

Enter-SingleInstance

Use this function to check if any other instances of the script are running by passing the script ID, such as script's path ($PSCommandPath), a GUID, or some other value that only that script would know. If the function detects that no other instances are running, it will create a mutex (named after the script ID), and return true to indicate that this is the first instance. The mutex will be held until the script calls the Exit-SingleInstance function, but before that any other script that calls Enter-SingleInstance will get back false and know that there is another instance of the script running. Keep in mind that if the first instance does not release the mutex (e.g. if the script crashes without cleanup), the mutex will be held until the system is rebooted.

Syntax

Enter-SingleInstance `
  [-ScriptId <string>] `
  [<CommonParameters>]

Arguments

-ScriptId

The value uniquely identifying the calling script. If you omit this parameter the module will try to dermine the path to the calling script (.ps1 file) and use this path as the script ID.

-<CommonParameters>

Common PowerShell parameters (cmdlet is not using these explicitly).

Exit-SingleInstance

Use this function to at the end of the processing to release the single-instance mutex.

Syntax

Exit-SingleInstance

Arguments

-<CommonParameters>

Common PowerShell parameters (cmdlet is not using these explicitly).

Get-ScriptPath

This is a helper function that you may find useful if you want to determine the path of the calling script from a module. The first script in the call stack identified by the extension will be used to determine the script path.

Syntax

Get-ScriptPath `
  [-Extension <string>] `
  [<CommonParameters>]

Arguments

-Extension

Extension by which the calling script will be identified. By default, the '.ps1' extension will be used.

-<CommonParameters>

Common PowerShell parameters (cmdlet is not using these explicitly).

Usage

You can download a copy of the module from the Github repository or install it from the PowerShell Gallery (see Examples).

Examples

Example 1

function LoadModule {
    param(
        [string]
        $ModuleName
    )

    if (!(Get-Module -Name $ModuleName)) {

        if (!(Get-Module -Listavailable -Name $ModuleName)) {
            Install-Module -Name $ModuleName -Force -Scope CurrentUser -ErrorAction Stop
        }

        Import-Module $ModuleName -ErrorAction Stop -Force
    }
}

$modules = @("SingleInstance")
foreach ($module in $modules) {
    try {
        LoadModule -ModuleName $module
    }
    catch {
        throw (New-Object System.Exception "Cannot load module $module.", $_.Exception)
    }
}

Downloads the SingleInstance module from the PowerShell Gallery into the PowerShell modules folder for the current user and imports it into the running script.

Example 2

$modulePath = Join-Path (Split-Path -Path $PSCommandPath -Parent) 'SingleInstance.psm1'
Import-Module $modulePath -ErrorAction Stop -Force

Imports the SingleInstance module from the same directory as the running script.

Example 3

if (!(Enter-SingleInstance)) {
    throw "The script is already running."
}
else {
    try {
        # Do what you need to do.
    }
    finally {
        # Make sure you exit single instance on both success and failure.
        Exit-SingleInstance
    }
}

Enforces single-instance script execution.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.