gpt4 book ai didi

windows - PowerShell:断电时启动脚本的最佳方法

转载 作者:行者123 更新时间:2023-12-03 00:58:10 26 4
gpt4 key购买 nike

因此,我在PowerShell中构建了脚本。该脚本应该不间断地从我家本地服务器上的日志中复制数据(此代码已编码并且可以工作)。

我有它,这样当电源断开时,如果由于某种原因电源消失或断电,UPS就会启动。发生这种情况时,我的代码将电源模式切换为省电模式,同时向我的手机发送了一条短信(短信部分将在以后进行编码),同时,脚本也会开始从服务器复制日志到一个文本文件(有效)。

现在是我的实际问题。什么是运行此脚本的最佳方法?我在想,如果脚本运行24/7,则可能会减慢服务器速度。

所以我想知道的是启动脚本的最佳方法是什么?
如果电源断开,Windows中是否有某些东西可以启动程序/ exe文件?

如果不是,那么我猜想一个循环仅运行24/7,并且一旦batterystatus从2变为1,它就会退出循环并启动实际脚本,但是最好在单独的PowerShell文件中完成此操作吗?就像,单独的PowerShell文件运行循环并具有:

if ($batterystatus -eq 1){
start "C:user\MainScript.exe"
}
Exit

有什么聪明的想法可以编码吗? :)

我现在在手机上,因此无法提交任何示例代码,但我认为还是不需要它了:)

谢谢

最佳答案

您可以使用wmi和一个事件来执行此操作:

# 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

关于windows - PowerShell:断电时启动脚本的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59969141/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com