4

I just finished writing a script with PowerShell and I need it to run every time my system wakes up from sleep or Hibernation. I tried the gpedit.msc -> User Config -> Windows Settings -> Scripts -> Logon but this does not work when my system Wakes up from 'Sleep'... probably because 'Sleep' - > 'Wake -up' is not technically new logon (As a user I am already logged-on).

Either a Sleep-Wake event or Login (User Login) event should trigger my script.

Any ideas on how I can do this?

More Info on what my script does: It checks if internet/network is connected. If yes, it will attempt to run the Cisco VPN connection command line utility with required arguments and logs me into VPN. Every time I wake my system up from sleep and login I need the damn VPN to connect me automatically.

JasonMArcher
  • 14,195
  • 22
  • 56
  • 52
Kir
  • 385
  • 1
  • 7
  • 20

4 Answers4

5

I figured it out. I used the Task Scheduler as Richard indicated above.

I set the Trigger as.. On an Event -> System -> Kernel-Power -> 42

If anyone is interested... here is the script (I am a beginner with PowerShell scripts, so welcome your inputs)

$strComputerName = gc env:computername
$strVPN = 'c:\Program Files (x86)\Cisco Systems\VPN Client\vpnclient.exe'
$vpnArgs = 'connect', 'ProfileName', 'user', 'XXXXXXXX', 'pwd', 'XXXXXXXXXX'
[int]$count = 6
Write-Host "Checking for Internet Connectivity"
While (!([Activator]::CreateInstance([Type]::GetTypeFromCLSID([Guid]'{DCB00C01-570F-4A9B-8D69-199FDBA5723B}')).IsConnectedToInternet))
{
    Write-Host "Sleeping for 10 seconds before checking for internet again"
    Start-Sleep -s 10
    $count--
    if ( $count -eq 0) 
    {
        Write-Host "Breaking from loop"
        break
    }
}
if ( $count -ne 0) { Invoke-Command -ScriptBlock { & $strVPN $vpnArgs } }
else { Write-Host "No Internet Connectivity, so not attempting VPN connection" }
Kir
  • 385
  • 1
  • 7
  • 20
  • 1
    This worked for me but to get the Event ID you can check the Event Viewer > System and look for Kernel-Power events in the Source column and find the one that says it's for wake up. For me the Event ID was 107 (42 was the go to sleep event). I cleared my log, used sleep, and woke it up to get a very short list to look through. – Mycah Jul 22 '22 at 18:29
4

You can accomplish this by scheduling a task to run the script, triggering on event "Power-Troubleshooter" with Event ID 1. This is the event that's logged by the system when it resumes from sleep. When the system is entering sleep, the "Kernel-Power" event is called with Event ID 42 (unlike what another answer here suggests).

Hope that helps :)

Ratnesh Chandna
  • 395
  • 1
  • 2
  • 9
1

Task Scheduler would seem to be a better fit.

If a task trigger of "At Startup" doesn't work in this case (I suspect it won't) then identify an event log entry posted on wake from sleep and trigger on that.

Richard
  • 106,783
  • 21
  • 203
  • 265
0

On Windows 11 the Event Id we can use for this is 566

I set the Trigger as> On an Event -> System -> Kernel-Power -> 566

Here is my configuration Scheduled task triggers

In my case I run a powershell script that turns on my keyboard backlight on my laptop. The backlight now turns on after windows has resumed from sleep (open the lid on laptop) but before I have logged in.

You also might want to uncheck: conditions > Power > "start the task only if the computer is on AC power"

Community
  • 1
  • 1
Tony
  • 1,394
  • 5
  • 22
  • 48
  • I found 566 to work the best, but sometimes it gets triggered again right after the system resumes, resulting in my sleep script running when it shouldn't. – sgtcoder Feb 18 '22 at 17:45
  • For me the Event ID was 107, also on W11 (see my comment in the accepted answer) – Mycah Jul 22 '22 at 21:17