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).EstimatedCh argeRemain ing
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.De finition
$files = get-childitem -path $Scriptpath -recurse -filter *.inf
foreach ($file in $files)
{
Write-host "Injecting driver $file"
pnputil -i -a $file.FullName
}
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).EstimatedCh
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.De
$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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
This code worked perfectly.
Dave