How can I guarantee that only one instance of a script is running at a time?

Suppose I run a powershell script and run in a loop. I open the second ps console and run the second script. In this script, I want to determine if the first script is working or not. What are the ways to achieve this?

+4
source share
2 answers

If you open 2 separate consoles, you need to add some logic to your script to make some changes to the file system, registry, or even the title bar of the PowerShell session where it is running.Then you can use some logic on your second console to find this information .

Another way that I usually use is WMI:

PS>get-wmiobject win32_process|where {$_.name -eq "powershell.exe"}|select-exp commandline 

Example:

 CommandLine : powershell.exe -file "./loop.ps1" 

This means that you need to call powershell.exe to run the script.

+4
source

When I am only interested in processes on my local computer, I would use

 get-process *powershell* 

I want to get PowerShell ISE too, so a wildcard.

0
source

Source: https://habr.com/ru/post/1333301/


All Articles