Reputation: 87
so im building a script in PowerShell. The script is supposed to copy data nonstop from a log on a local server in my house (this is coded and works).
I have it so that when the power disconnects a UPS kicks in if the power for some reason dissapears / blackout. When this happens my code switches the power mode to battery saving while at the same time sends an sms to my phone (the sms part is due to be coded in later), also when this happens the script will start copying the log from the server to a text file (works).
Now for my actual question. Whats the best way to run this script? I'm thinking that if the script is running 24/7 it might slow down the server.
So what I am wondering is what's the best way to start the script? Is there something in windows that can start a program/exe file if the power supply disconnects?
If not then I guess a loop that just runs 24/7 and once batterystatus goes from 2 to 1 then it exits the loop and starts the actual script, but is this best done on a separate PowerShell file? Like, the separate PowerShell file runs the loop and has an:
if ($batterystatus -eq 1){
start "C:user\MainScript.exe"
}
Exit
Any smart ideas to code this? :)
Im on my phone right now so cant submit any sample code but I don't think it's needed anyways hehe :)
Thanks
Upvotes: 1
Views: 1680
Reputation: 51
According to Using Windows PowerShell to Determine if a Laptop Is on Battery Power You can create a script that checks if the machine is on battery power (C:\User\Check-PowerStateAndCopyIfOnBattery.ps1):
#Check-PowerStateAndCopyIfOnBattery.ps1
if ((Get-WmiObject -Class BatteryStatus -Namespace root\wmi).PowerOnLine -eq $false)
{
while((Get-WmiObject -Class BatteryStatus -Namespace root\wmi).PowerOnLine -eq $false)
{
Start-Process "C:\User\MainScript.exe" -Wait
}
}
And then create a Scheduled Task that triggers when the power state changes (System EventID 105). You can save the xml below as Check-PowerStateAndCopyIfOnBattery.xml and import it (see: How to import a scheduled task from an XML file?).
<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
<Triggers>
<EventTrigger>
<Enabled>true</Enabled>
<Subscription><QueryList><Query Id="0" Path="System"><Select Path="System">*[System[Provider[@Name='Microsoft-Windows-Kernel-Power'] and EventID=105]]</Select></Query></QueryList></Subscription>
</EventTrigger>
</Triggers>
<Settings>
<MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
<DisallowStartIfOnBatteries>false</DisallowStartIfOnBatteries>
<StopIfGoingOnBatteries>false</StopIfGoingOnBatteries>
<AllowHardTerminate>true</AllowHardTerminate>
<StartWhenAvailable>true</StartWhenAvailable>
<RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
<IdleSettings>
<StopOnIdleEnd>false</StopOnIdleEnd>
<RestartOnIdle>false</RestartOnIdle>
</IdleSettings>
<AllowStartOnDemand>true</AllowStartOnDemand>
<Enabled>true</Enabled>
<Hidden>false</Hidden>
<RunOnlyIfIdle>false</RunOnlyIfIdle>
<WakeToRun>false</WakeToRun>
<ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
<Priority>7</Priority>
</Settings>
<Actions Context="Author">
<Exec>
<Command>Powershell.exe</Command>
<Arguments>-ExecutionPolicy Bypass C:\User\Check-PowerStateAndCopyIfOnBattery.ps1 -RunType $true -Path C:\User\</Arguments>
<WorkingDirectory>C:\User\</WorkingDirectory>
</Exec>
</Actions>
</Task>
Upvotes: 1
Reputation: 1782
You could do this with wmi and an event:
# check every 120 seconds
$poll = 120
# estimated battery charge remaining threshold
$remaining = 10
# query
$query = "Select * from __InstanceModificationEvent within $poll where TargetInstance ISA 'Win32_Battery' AND TargetInstance.EstimatedChargeRemaining<=$remaining"
# what to do when the event occurs
$action={
Start-Process -FilePath "C:user\MainScript.exe"
}
Register-WmiEvent -Query $query -SourceIdentifier "Battery Monitor" -Action $action
# check event with
# Get-EventSubscriber -SourceIdentifier "Battery Monitor"
# unregister event with
# Get-EventSubscriber -SourceIdentifier "Battery Monitor" | Unregister-Event
Upvotes: 2