<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Ionut Nica &#187; WMI classes</title>
	<atom:link href="http://www.rivnet.ro/tag/wmi-classes/feed" rel="self" type="application/rss+xml" />
	<link>http://www.rivnet.ro</link>
	<description>Windows, Scripting, Virtualization, Cloud Computing - tricks for getting around in the world of Technology</description>
	<lastBuildDate>Tue, 31 Jan 2012 12:10:26 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Log Battery and Power Levels using Powershell</title>
		<link>http://www.rivnet.ro/2010/05/log-battery-and-power-levels-using-powershell.html</link>
		<comments>http://www.rivnet.ro/2010/05/log-battery-and-power-levels-using-powershell.html#comments</comments>
		<pubDate>Sun, 09 May 2010 14:02:48 +0000</pubDate>
		<dc:creator>Ionut Nica</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[Windows 7]]></category>
		<category><![CDATA[Battery]]></category>
		<category><![CDATA[Energy]]></category>
		<category><![CDATA[WMI classes]]></category>

		<guid isPermaLink="false">http://www.rivnet.ro/?p=385</guid>
		<description><![CDATA[This is a let&#8217;s say lighter post, I came up while trying to compare battery life of my laptop and some buddies of mine. I wanted to know, how fast my battery depleted using different settings, use profile and power saving modes. Then I did some digging around Microsoft&#8217;s MSDN site, and I found some [...]]]></description>
			<content:encoded><![CDATA[<p>This is a let&#8217;s say lighter post, I came up while trying to compare battery life of my laptop and some buddies of mine. I wanted to know, how fast my battery depleted using different settings, use profile and power saving modes. Then I did some digging around Microsoft&#8217;s MSDN site, and I found some interesting WMI classes, that apparently provide a lot of &#8220;power related data&#8221;. I also wanted to have a way to log this data, and that&#8217;s how I ended up learning how to create a new event-log file and write data to it to use that as a log. So this is what I will try to show: <strong>get power related data</strong> and <strong>write it to the Event-Log</strong>.</p>
<h3>&#8220;Energy&#8221; Related WMI Classes</h3>
<p>Here are a few interesting classes I stumbled upon. Some of them are only available under Windows7 probably also Vista, but I&#8217;m not sure.</p>
<ul>
<li><strong>WmiMonitorBrightness</strong> &#8211; gives information about monitor brightness. For example these line give the max. &#8220;value&#8221; and current value of brightness</li>
</ul>
<pre class="brush: powershell; title: ; notranslate">$MaxBrightness = get-wmiobject -class WmiMonitorBrightness -Namespace root/wmi).level | measure-object -Maximum).maximum
$CrtBrightness = &quot;{0:P0}&quot; -f ((get-wmiobject -class WmiMonitorBrightness -Namespace root/wmi).CurrentBrightness/$MaxBrightness)</pre>
<ul>
<li><strong>Win32_PowerPlan</strong> &#8211; provides information and identifiers about the powerplans defined. In this class ALL powerplans are defined, and just the active plan has an &#8220;IsActive&#8221; flag attached it, here&#8217;s how to get it:</li>
</ul>
<pre class="brush: powershell; title: ; notranslate">$powerplan = (Get-WmiObject -Class win32_powerplan -Namespace 'root/cimv2/power' | where {$_.IsActive -eq $true}).ElementName</pre>
<ul>
<li><strong>Win32_Processor</strong> &#8211; gets information about the CPU (I was interested in the CPU load for statistical purposes). This one was pretty easy to find, the value was written in plain sight. Take a look:</li>
</ul>
<pre class="brush: powershell; title: ; notranslate">$cpu = (Get-WmiObject Win32_Processor).LoadPercentage</pre>
<ul>
<li><strong>Win32_Battery</strong> &#8211; Provides information about the battery itself (estimated time, remaining load, power status). Running &#8220;<strong>Get-WmiObject -Class Win32_Battery | gm&#8221;</strong> take a closer look at these members:
<ul>
<li><strong>BatteryStatus</strong> &#8211; this will toggle between &#8217;1&#8242; meaning on Battery and &#8217;2&#8242; meaning on AC Power</li>
<li><strong>EstimatedRuntime</strong> &#8211; this will be the number of minutes running on battery, as the OS estimates it, and if you get a  very high value (tens of thousands) when you plug the AC Power, it means the battery is charging</li>
<li><strong>EstimatedChargeRemaining</strong> &#8211; percentage-wise representation of battery charge remaining</li>
</ul>
</li>
</ul>
<h3>Powershell + Event-Log &#8220;101&#8243;</h3>
<p>I used this battery and power experiment to learn more about working and writing data to the Event-Log. I wanted to create a new &#8220;Event-Log&#8221; in Windows (windows 7 as you probably know allows for a lot of application logs) and then write events to it. Then at any point you can export the Event-log to csv. The following creates an Event-Log, with the name &#8220;BatteryMonitor&#8221; from the Information category (for my uses &#8220;Source&#8221; was not needed but it is a required parameter:</p>
<pre class="brush: powershell; title: ; notranslate">New-EventLog -Source BattMon -LogName BatteryMonitor -CategoryResourceFile Information</pre>
<p>You can also check if an Event-log is created exists you can use this scriptlet (the answer lies in WMI this time, I didn&#8217;t find a cmdlet that does it faster):</p>
<pre class="brush: powershell; title: ; notranslate">(get-wmiobject -class &quot;Win32_NTEventlogFile&quot; | where {$_.LogFileName -like 'BatteryMonitor'} | measure-object ).count -eq '1'</pre>
<p>Finally here&#8217;s how to write to the event-log, a new event. This bit I used in a script to mark the execution of the script in the event-log:</p>
<pre class="brush: powershell; title: ; notranslate">Write-EventLog -LogName BatteryMonitor -Source BattMon -EventID 65533 -Message 'Starting new Execution of BatteryCharge Monitor Script. The script will pump here CSV values. Values are listed in this order, as CSV: PowerPlan,PowStatMsg,ChargeRemMsg,RemTimeMsg,RAM,CPU,CrtBrightness' -EntryType Information -ComputerName $env:computername -ErrorAction:SilentlyContinue</pre>
<p>So that is about it, as usual I tried to tie all of these scriptlets into a usable script, you can download it from <a href="http://www.rivnet.ro/wp-content/uploads/2010/05/win7_battlevel.zip" target="_blank">here</a>.</p>
<p><a class="a2a_dd a2a_target addtoany_share_save" href="http://www.addtoany.com/share_save#url=http%3A%2F%2Fwww.rivnet.ro%2F2010%2F05%2Flog-battery-and-power-levels-using-powershell.html&amp;title=Log%20Battery%20and%20Power%20Levels%20using%20Powershell" id="wpa2a_2"><img src="http://www.rivnet.ro/wp-content/plugins/add-to-any/share_save_171_16.png" width="171" height="16" alt="Share"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://www.rivnet.ro/2010/05/log-battery-and-power-levels-using-powershell.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

