【问题标题】:Test battery status using PowerShell使用 PowerShell 测试电池状态
【发布时间】:2023-01-24 02:48:19
【问题描述】:

我正在尝试使用 PowerShell 告诉我计算机何时使用电池或交流电源。

我希望我的脚本在笔记本电脑的充电器拔下时向我发送 Windows 通知。

目前,我尝试使用递归函数每 5 秒测试一次电池状态,但它不起作用......

请放纵我的水平,3 小时前我对 PowerShell 一无所知……而且我上次编写代码是很久以前的事了!

Function Test-IsOnBattery
{

$battery = Get-WmiObject Win32_Battery

If ($battery.BatteryStatus -eq 2) {
    
    Write-Host "PC sur secteur."
    Start-Sleep -Seconds 5

    return Test-IsOnBattery
    }

Else {
    
    Write-Host "PC sur batterie."

    New-BurntToastNotification -Text "Battery Notification" , "Batterie plus sur secteur !"
    }
}

【问题讨论】:

  • 但什么是“但它不起作用...“?你有错误吗?会发生什么/不会发生什么?
  • 你的功能对我来说很好。刚刚测试过它并且工作正常。我肯定会使用 while 循环而不是递归,但除此之外看起来不错
  • PowerShell 没有任何尾调用优化方式。用递归来实现一个可能无界的循环,肯定是迟早要耗尽栈的,所以不要这样做。在这种情况下也没有实际需要,因为它不会使代码更简单。

标签: powershell recursion


【解决方案1】:

内森,

这是您可以使用的脚本,您可以从计划任务而不是循环运行,并让它在启动时启动并每隔几分钟重复一次。


<#+---------------------------------------------------------------------------+
  | PowerShell Pgm: BatteryStatus.ps1                                         |
  | Programmed By : The Computer Mentor                                       |
  |           aka : RetiredGeek @ askWoody.com & StackOverFlow.com            |
  | Created       : 06 Mar 2013                                               |
  | Last Updated  : 23 Jan 2023                                               |          |
  | Current Ver.  : 6.0                                                       |
  +---------------------------------------------------------------------------+
#>

Clear-Host

Add-Type -AssemblyName "System.Windows.Forms"
$StatusMsg = {
  [Windows.Forms.MessageBox]::Show($Message, $Title,
  [Windows.Forms.MessageBoxButtons]::OK ,
  [Windows.Forms.MessageBoxIcon]::Information)}

$Message = ""
$Title   = "Battery Status:"

<#+-----------------------------------------------------+
  | BatterStatus Values                                 |
  |Other   (1)  The battery is discharging.             |
  |Unknown (2)  The system has access to AC so no       |
  |             battery is being discharged. However,   |
  |             the battery is not necessarily charging.|
  |Fully Charged (3)                                    |
  |Low (4)                                              |
  |Critical (5)                                         |
  |Charging (6)                                         |
  |Charging and High (7)                                |
  |Charging and Low (8)                                 |
  |Charging and Critical (9)                            |
  |Undefined (10)                                       |
  |Partially Charged (11)                               |
  +-----------------------------------------------------|#> 

$GWArgs = @{ Class        = "Win32_Battery"
             ComputerName = "LocalHost" 
           }

$MyBattery = Get-CIMInstance @GWArgs

If ($Null -eq $MyBattery) {
  $Message = "No Battery Present"
}

Else {

  $BatteryRemaining = $MyBattery.EstimatedChargeRemaining
  
  if(($BatteryRemaining -lt 30) -and 
      $MyBattery.BatteryStatus -eq 1) {
     $Message = "Battery Low...Please Charge Me!"
  }
  Elseif(($BatteryRemaining -gt 90) -and 
          $MyBattery.BatteryStatus -ne 1) {
             $Message = 
             "Battery CHARGED above 90%...Please Unplug Me!"
  }

} #End Else

if($Message -ne "") {
  $Null = & $StatusMsg
}

<#
  +----------------------------------------------------------+
  | Notes:                                                   |
  | 1. To call as a scheduled task do the following in the   |
  |    Action Pane                                           |
  |   A. Action:                   Start a Program           |
  |   B. Program/script:           powershell.exe            |
  |   D. Set the Trigger run At Logon and then repeat        |
  |      every few minutes.                                  |
  +----------------------------------------------------------+
#>

如果要使用 Toast 消息。只需替换我有 $Status Mgs 行的逻辑。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-14
    • 1970-01-01
    • 1970-01-01
    • 2012-07-21
    • 1970-01-01
    相关资源
    最近更新 更多