Solved

PowerShell script to evaulate battery life and take action

Posted on 2014-12-05
2
159 Views
Last Modified: 2014-12-05
I found a PowerShell script that determines battery life but I need to add conditions to take action. The action would be to run the main script or simply exit if battery life is less than 40%. I'm very new to PowerShell and would like to combine these scripts into one if possible?

As a bonus, it would be nice to capture logging on the state of the battery during script runtime.

Thanks,
Dave


Battery life script


function Get-LaptopBatteryStatus
{
$charge = (Get-WmiObject -Class Win32_Battery).EstimatedChargeRemaining
if ($charge -ge 100)
{ return "White" }
if ($charge -ge 50)
{ return "Green" }
if ($charge -ge 25)
{ return "yellow" }
return "red"
}

Main script


$ScriptPath = Split-Path -parent $MyInvocation.MyCommand.Definition

$files = get-childitem -path $Scriptpath -recurse -filter *.inf

foreach ($file in $files)

{

    Write-host "Injecting driver $file"

    pnputil -i -a $file.FullName

}
0
Comment
Question by:hpops
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 19

Accepted Solution

by:
Raheman M. Abdul earned 500 total points
ID: 40483179
function Get-LaptopBatteryStatus
{
$charge = (Get-WmiObject -Class Win32_Battery).EstimatedChargeRemaining
if($charge -le 40)
{
return
}
else
{
$ScriptPath = Split-Path -parent $MyInvocation.MyCommand.Definition
$files = get-childitem -path $Scriptpath -recurse -filter *.inf
foreach ($file in $files)
{
    Write-host "Injecting driver $file"
    pnputil -i -a $file.FullName
} 

}
}

Get-LaptopBatteryStatus

Open in new window

0
 

Author Comment

by:hpops
ID: 40483774
Thanks Raheman.

This code worked perfectly.

Dave
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question