- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You could use a mutual exclusion to prevent both programs to run at the same time. You can use the following code and wrap it around your programs:
Try { $MutexName = "Global\Mutex" # Start mutual exclusion $Mutex = New-Object -TypeName System.Threading.Mutex -ArgumentList $false, $MutexName $Mutex.WaitOne() # # Your program comes here # # Stop mutual exclusion $Mutex.ReleaseMutex() $Mutex = $null } Catch { # Stop mutual exclusion $Mutex.ReleaseMutex() $Mutex = $null }
The first program that runs will get the mutual exclusion and will continue. The second program will wait untill the first program relases the mutual exclusion. This prevents both programs to run at the same time. That will assure that if P2 runs it fails and stop execution.
The Try - Catch construction ensures that if an error is generated in your code, the mutex is still released before the program stops.
Regards, Robert
Message was edited by: RvdNieuwendijk Added the Try - Catch explanation.
Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition