Link to home
Start Free TrialLog in
Avatar of hpops
hpops

asked on

PowerShell script to evaulate battery life and take action

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

}
ASKER CERTIFIED SOLUTION
Avatar of Raheman M. Abdul
Raheman M. Abdul
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hpops
hpops

ASKER

Thanks Raheman.

This code worked perfectly.

Dave