需要连续循环命令

发布于 2025-01-22 23:39:16 字数 906 浏览 0 评论 0原文

我有一个脚本需要在时间间隔1分钟连续循环。

我正在使用该脚本来将电池百分比与用户相关联

$results=(Get-CimInstance -ClassName Win32_Battery).EstimatedChargeRemaining

if ($results -gt 20)
{

$ButtonType = [System.Windows.Forms.MessageBoxButtons]::OK
$MessageIcon = [System.Windows.Forms.MessageBoxIcon]::Information
$MessageBody = "Remove the charger. Your battery percentage $results"
$MessageTitle = "Laptop Battery Backup status"
$Result = [System.Windows.Forms.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon)

}
elseif ($results -lt 10) {

$ButtonType = [System.Windows.Forms.MessageBoxButtons]::OK
$MessageIcon = [System.Windows.Forms.MessageBoxIcon]::Warning
$MessageBody = "Connect the charger. Your battery percentage $results"
$MessageTitle = "Laptop Battery Backup status"
$Result = [System.Windows.Forms.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon)

}

I have a script I need to loop continuously with time interval 1 minute.

I am using the script is used for intimate the battery percentage to the user

$results=(Get-CimInstance -ClassName Win32_Battery).EstimatedChargeRemaining

if ($results -gt 20)
{

$ButtonType = [System.Windows.Forms.MessageBoxButtons]::OK
$MessageIcon = [System.Windows.Forms.MessageBoxIcon]::Information
$MessageBody = "Remove the charger. Your battery percentage $results"
$MessageTitle = "Laptop Battery Backup status"
$Result = [System.Windows.Forms.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon)

}
elseif ($results -lt 10) {

$ButtonType = [System.Windows.Forms.MessageBoxButtons]::OK
$MessageIcon = [System.Windows.Forms.MessageBoxIcon]::Warning
$MessageBody = "Connect the charger. Your battery percentage $results"
$MessageTitle = "Laptop Battery Backup status"
$Result = [System.Windows.Forms.MessageBox]::Show($MessageBody,$MessageTitle,$ButtonType,$MessageIcon)

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七分※倦醒 2025-01-29 23:39:16

这让我很感兴趣,所以我玩耍并做了一些您可能想要的更改。

下面的代码将不会提示您如果连接连接的充电器,并且如果充电器在满足适当的条件时连接,则不会提示您连接该充电器。

我在脚本顶部设置了变量的全部充电和低电量百分比,以使其易于定位和更改,我还将它们设置为将电池保持在20-80%之间我理解的电池。

同样,设置为3分钟的消息之间的时间也不应足够快,以至于每分钟需要检查它们,即使3分钟也可能经常进行。

最后,我提供了一个逃生,您想使用取消按钮关闭程序。

$FullChgPct = 78
$NeedChgPct = 22

$MessageTitle = "Laptop Battery Backup status"
$Result       = "OK"
$ButtonType   =
   [System.Windows.Forms.MessageBoxButtons]::OKCancel

While ($Result -eq "OK") {

  $Battery=Get-CimInstance -ClassName Win32_Battery
  $ChgPct = $Battery.EstimatedChargeRemaining
  $Mains  = $Battery.Status        
  
  if ($ChgPct -gt $FullChgPct -and
      $Mains  -eq 2)   {
  
  $MessageIcon = 
    [System.Windows.Forms.MessageBoxIcon]::Information
  $MessageBody = 
     "Remove the charger. Your battery percentage $ChgPct"
  $Result = 
    [System.Windows.Forms.MessageBox]::Show(
     $MessageBody,$MessageTitle,$ButtonType,$MessageIcon)
  
  }

  If ($ChgPct -lt $NeedChgPct -and
      $Mains  -eq 1) {
  
  $MessageIcon = [System.Windows.Forms.MessageBoxIcon]::Warning
  $MessageBody = 
     "Connect the charger. Your battery percentage $ChgPct"
  $Result = 
    [System.Windows.Forms.MessageBox]::Show(
     $MessageBody,$MessageTitle,$ButtonType,$MessageIcon)
  
  }

  # Notify user every 3 Minutes
  If ($Result -eq "OK") { Start-Sleep -Seconds 300 }
  
} #End While 

This intrigued me so I played around and made some changes you might want.

The code below will not prompt you to disconnect the charger if it is not connected and will not prompt you to connect the charger if it is connected when the appropriate conditions are met.

I've set the full charge and low charge percentages in variables at the top of the script to make them easy to locate and change and I also set them to keep the battery between 20-80% the recommended range for longevity of Lithium-Ion batteries as I understand it.

Also the time between messages set to 3 minutes as batteries shouldn't drain fast enough that they need to be checked every minute, even 3 minutes may be too often.

Lastly, I provided an escape incase you want to turn the program off with the cancel button.

$FullChgPct = 78
$NeedChgPct = 22

$MessageTitle = "Laptop Battery Backup status"
$Result       = "OK"
$ButtonType   =
   [System.Windows.Forms.MessageBoxButtons]::OKCancel

While ($Result -eq "OK") {

  $Battery=Get-CimInstance -ClassName Win32_Battery
  $ChgPct = $Battery.EstimatedChargeRemaining
  $Mains  = $Battery.Status        
  
  if ($ChgPct -gt $FullChgPct -and
      $Mains  -eq 2)   {
  
  $MessageIcon = 
    [System.Windows.Forms.MessageBoxIcon]::Information
  $MessageBody = 
     "Remove the charger. Your battery percentage $ChgPct"
  $Result = 
    [System.Windows.Forms.MessageBox]::Show(
     $MessageBody,$MessageTitle,$ButtonType,$MessageIcon)
  
  }

  If ($ChgPct -lt $NeedChgPct -and
      $Mains  -eq 1) {
  
  $MessageIcon = [System.Windows.Forms.MessageBoxIcon]::Warning
  $MessageBody = 
     "Connect the charger. Your battery percentage $ChgPct"
  $Result = 
    [System.Windows.Forms.MessageBox]::Show(
     $MessageBody,$MessageTitle,$ButtonType,$MessageIcon)
  
  }

  # Notify user every 3 Minutes
  If ($Result -eq "OK") { Start-Sleep -Seconds 300 }
  
} #End While 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文