Using PowerShell to Analyze Your PC’s Sleep & Wake Behavior

I have some older hardware running Windows 7 Media Center.  Normally we let the Media Center just go to sleep on its own but I’ve been concerned that it isn’t sleeping as much during there wee hours of the morning as I think it should be.  I’ve got Ceton quad tuner card in it that I like to give a chance to cool down.  The one problem with our slim form factor Media Center PC is that the cooling isn’t the best.

In order to determine how much the PC sleeps I wrote a small PowerShell script to analyze event log entries.  Here are the contents of the script Get-SleepInfo.ps1:

param($Newest = 50)

function Parse-EventLogEntry
{
    param(
        [Parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true)]
        [System.Diagnostics.EventLogEntry[]]
        $eventInfo
    )

    Process
    {
        foreach ($info in $eventInfo)
        {
            $enterSleep = [DateTime]::Parse($info.ReplacementStrings[0]);
            $exitSleep = [DateTime]::Parse($info.ReplacementStrings[1]);
            $duration = $exitSleep - $enterSleep
            $wakeSource = 'Unknown'
            if ($info.Message -match 'Wake Source:\s*(.*)$')
            {
                $wakeSource = $matches[1]
            }
            new-object psobject -Property @{Duration = $duration; Sleep = $enterSleep; 
                                            Wake = $exitSleep; WakeSource = $wakeSource}
        }
    }
}

Get-EventLog -LogName System -Source Microsoft-Windows-Power-Troubleshooter -Newest $Newest | 
     Sort TimeGenerated | Parse-EventLogEntry

 

After running this on my MediaCenter PC, I get the following data:

Duration         Sleep                Wake                 WakeSource
--------         -----                ----                 ----------
01:04:22.2225162 5/2/2013 2:47:08 PM  5/2/2013 3:51:31 PM  5Windows will exec...
00:44:42.9902954 5/3/2013 12:33:23 AM 5/3/2013 1:18:06 AM  5Windows will exec...
00:12:56.0674401 5/3/2013 2:18:08 AM  5/3/2013 2:31:05 AM  5Windows will exec...
06:59:43.2874463 5/3/2013 3:31:08 AM  5/3/2013 10:30:52 AM 0
05:54:48.3185846 5/4/2013 5:01:08 AM  5/4/2013 10:55:57 AM 0
00:30:09.8591193 5/5/2013 1:15:01 AM  5/5/2013 1:45:11 AM  5Windows will exec...
05:30:49.2332622 5/5/2013 5:01:04 AM  5/5/2013 10:31:54 AM 0
03:12:13.2579267 5/6/2013 5:02:20 AM  5/6/2013 8:14:33 AM  0
00:14:30.6312742 5/7/2013 1:25:14 AM  5/7/2013 1:39:45 AM  5Windows will exec...
00:11:23.6864251 5/7/2013 2:39:59 AM  5/7/2013 2:51:23 AM  5Windows will exec...
04:18:45.8838138 5/7/2013 4:34:44 AM  5/7/2013 8:53:30 AM  0
01:19:03.7671589 5/7/2013 12:46:44 PM 5/7/2013 2:05:48 PM  0
00:37:13.6740184 5/8/2013 12:59:29 AM 5/8/2013 1:36:43 AM  5Windows will exec...
00:01:11.1209186 5/8/2013 2:36:57 AM  5/8/2013 2:38:09 AM  5Windows will exec...
02:13:18.0939494 5/8/2013 3:38:12 AM  5/8/2013 5:51:31 AM  5Windows will exec...

 

The problematic wake source above (truncated in this output to fit the blog post width) is:

5Windows will execute ‘\Microsoft\Windows\Media Center\mcupdate_scheduled’ scheduled task that requested waking the computer.

Ah, good ol’ mcupdate_scheduled is configured to wake the computer if it is asleep.  This situation is quickly fixed with a little tweak to the scheduled task to a) not wake the computer and b) change the trigger to a time the computer is likely to be on.  With this tweak hopefully the MediaCenter will now sleep through the night.

Update: after tweaking the scheduled task “mcupdate_scheduled”, my Media Center PC slept for a good 12 hours before *I* woke it up:

02:13:18.0939494 5/8/2013 3:38:12 AM  5/8/2013 5:51:31 AM  5Windows will exec...
12:15:24.3601661 5/9/2013 1:24:42 AM  5/9/2013 1:40:07 PM  0
Advertisements
This entry was posted in PowerShell. Bookmark the permalink.

One Response to Using PowerShell to Analyze Your PC’s Sleep & Wake Behavior

  1. Gaurav Methi says:

    Nice one. Thanks.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s