Workaround for Unexpected Error in PowerCfg -SleepStudy
PowerCfg is a nifty little tool for analyzing and configuring all kinds of things related to battery usage and power plans. In Windows 8.1 PowerCfg learned how to analyze the effectiveness of connected standby, a new low-power mode introduced in Windows 8 that enables applications to stay connected while the device (mostly) sleeps. All the more frustrating when that new command suddenly stops working.
How It Should Be
Before running an analysis of connected standby make sure your device supports it with the command powercfg -a (most laptops do not, tablets had better or you should get something else). Then try to generate the connected standby report on an elevated command prompt with powercfg -sleepstudy. If that works, congratulations. Bookmark this page and come back when it does not any more.
How It Is
Powercfg -sleepstudy is supposed to generated an HTML report in C:\ProgramData\Microsoft\Windows\SleepStudy. I had used if successfully before, a report dating back two weeks proves that. But when I run the command today all I get is this:
“An unexpected error condition has occurred. Unable to perform operation. You may not have permission to perform this operation.”
Analysis
With an error like that the first thing I do is monitor the program’s execution with Sysinternals Process Monitor. There might be some missing file or incorrectly set permission that causes the problem. The ProcMon trace also shows nicely which files are created by the analyzed program.
I quickly found that PowerCfg creates the file %temp%\sleepstudy-report.xml and writes to it. A little bit later it opens the file and closes it again. That is near the end of the trace. In situations like this one it can be helpful to know which DLL and optionally which function is doing the work. You can get that information easily by double-clicking an event and switching to the tab Stack. When %temp%\sleepstudy-report.xml is created the function doing the work is SaveSleepStudyReport in energy.dll. When the file is opened again the function is TransformSleepStudyReport as this screenshot shows:
Sure enough, the file sleepstudy-report.xml was sitting in the temp directory and it was updated when I reran the PowerCfg command. Getting the data obviously worked. It seemed that only the last step, the conversion from XML to human-friendly HTML was broken.
Where the Data Comes From
With a little digging around I found that the actual data is collected via Event Tracing for Windows (ETW) and stored in C:\Windows\System32\wdi\SleepStudy. On my tablet that directory contained 66 .ETL files, some from that same day, so the data collection obviously worked.
The command powercfg -sleepstudy does nothing but read the existing (binary) data, convert it first to XML and then to HTML.
More Analysis
As a next step I wanted to know why the function TransformSleepStudyReport failed, in other words I wanted to know its return code. In order to get that an API monitoring tool like Rohitab API Monitor is required. Rohitab’s program is surprisingly easy to easy while being extremely powerful. Unfortunately there seems to be a bug affecting the tool on Windows 8.1 – I could not get it to launch PowerCfg.exe elevated, neither did the new process notification work.
I turned to OllyDbg, a powerful free debugger that unfortunately is lacking a bit on the UX side. I have by no means mastered this tool, but I was able to determine that TransformSleepStudyReport is being called with just two parameters: the input file and the output file. To my dismay I was not able to determine that function’s return value.
Writing My Own
That was when I decided to write my own little tool that calls TransformSleepStudyReport in energy.dll. I knew it would not be difficult because I had the parameters, and in my own tool I would finally see the function’s return value.
I did, and I got the return value. Converted to a string it said “unknown error”. Not much better than PowerCfg’s output “An unexpected error condition has occurred”.
I had suspected the intermediate XML with to be faulty for some time, but with my own tool I was finally able to test my hypothesis. What if there was a syntax error in the XML? I uploaded the file to an online XML checker that promptly complained about this line:
Do you see what’s wrong?
Hint: double quotes in a string enclosed in double quotes
I replaced the double quotes in the string with ". The line now looked like this:
Then I ran my tool again – and the HTML report appeared!
DIY
This is what you can do when powercfg -sleepstudy fails with unexpected error condition:
- Download my tool TransformSleepStudyReport.exe
- Fix the quotes in %temp%\sleepstudy-report.xml
- Run: TransformSleepStudyReport.exe C:\Users\Username\AppData\Local\Temp\sleepstudy-report.xml report.html
6 Comments
Hello Helge, thanks for publishing your analysis. I had exact same issue and seems to be caused by the bug with the quotes as you described. I think this will affect all systems with german language settings after some period of time. i had a clean system and suddenly the error occured after couple of weeks. Maybe the preset warning text can also be edited in registry. But to shorten the solution i simply ran:
powercfg sleepstudy -duration 10
of course this will “only” take last 10 days, but for sleepreport check this is far enough in my opionion. The good thing: this prevents the warning from beeing included in xml and so html is generated fine too! But your way also works fine with all the data. Thx a lot, without your blog i never would have tried -duration parameter…
Regards
Ausdroid
Great tip to extend the duration.
I needed to use a higher value than 10 (28 in my case), but the error disappeared and a correct html file was created.
thanks!
Hi Helge, and Ausdroid,
excellent work by Helge, and useful workaround proposal by Ausdroid. (I hope someone informed MS about this silly bug, by the way…)
In my case, using the -duration switch, I had to decrease the no. of days to 7 before the error disappeared. Could it be that the nunber is sensitive to the date of the last attempt of running sleep study?
Bests,
M.
Hi,
i have got the same problem and followed your steps.
I created a batchfile with the following code as you mentioned
TransformSleepStudyReport.exe C:\Users\DanielMarkus\AppData\Local\Temp\sleepstudy-report.xml report.html
and put it into the C:\Users\DanielMarkus\AppData\Local\Temp folder
I also made the quotes right and checked it with a xml checker. It’s correct.
But when i run the batch file as admin, i got the following error
C:\Users\DanielMarkus\AppData\Local\Temp>TransformSleepStudyReport.exe C:\Users\
DanielMarkus\AppData\Local\Temp\sleepstudy-report.xml report.html
LoadLibrary failed with: Das angegebene Modul wurde nicht gefunden.
Hello,
i have the same problem. I assume that the provided TransformSleepStudyReport.exe does not work on x64 systems. I’ve tried to write my tool for calling TransformSleepStudyReport for x64 but without success yet.
I’ve managed to make the x64 version. It worked for me. You can download it here:
https://dl.dropboxusercontent.com/u/5363574/TransformSleepStudyReport.x64.zip