Solved

PowerShell script to evaulate battery life and take action

Posted on 2014-12-05
2
149 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
2 Comments
 
LVL 18

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now