How can I check if other PowerShell scripts are running before hibernating?

0
0
Asked By TechyTurtle99 On

I'm trying to run multiple backup scripts for my external hard drives, with attended and unattended versions. The attended ones handle tasks normally, while the unattended ones set the power profile to high performance to prevent my PC from sleeping and hibernate it once the job is done. However, if I run several scripts overnight, I don't want the hibernation to kick in prematurely when some scripts are still working.

I'm looking for a way to check if any other PowerShell instances are still active before initiating hibernation. I want to add some code to my unattended script to handle this. Can anyone provide a code example showing how I can check for running PowerShell scripts so that I can delay the hibernation until they're all finished? As I'm new to PowerShell, I would appreciate a literal code example, not just instructions on what to do.

3 Answers

Answered By SchedulerSavant33 On

Instead of manual backups, why not schedule them? It could streamline your process and eliminate the risk of overlapping runs.

Answered By SleepyScripter11 On

You can periodically check for running PowerShell processes with this loop:
`Do {Start-Sleep -Seconds 15} Until ((Get-Process -Name PowerShell -ErrorAction Ignore) -eq $null)` This will keep checking until no PowerShell processes are found. If you want to be more specific, you can enhance it using the command line checks.

Answered By BackupGuru77 On

Consider using a mutex for better synchronization. When your backup script starts, it can request a mutex lock, and release it once it's done. Your hibernation script can then check for this lock and only hibernate when it's free.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.