Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

PowerShell script to evaulate battery life and take action

Posted on 2014-12-05
2
Medium Priority
?
166 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 2000 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

Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

Question has a verified solution.

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

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 the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
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). …
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

715 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