原文地址:http://app.en25.com/e/es.aspx?s=1403&e=5231&elq=4427f8ad0f9245da87fb793497c0753a

原文:

If your script needs to know whether your computer has a battery, you can ask WMI. Here is a small function:

function Has-Battery {
 
        @(Get-WmiObject Win32_Battery).Count -ne 0

        if (@(Get-WmiObject Win32_Battery).Count -ne 0) {

                 $true

        } else {

                 $false

        }
}

Note the use of @() which wraps the result into an array so you can check the number of batteries. Most systems have only one, but there are some systems with more.

 

 

翻译:

如果你的代码需要知道在你的机器上是否有电池,可以靠WMI。下面小函数可以实现这个方法:

function Has-Battery {
 
        @(Get-WmiObject Win32_Battery).Count -ne 0

        if (@(Get-WmiObject Win32_Battery).Count -ne 0) {

                 $true

        } else {

                 $false

        }
}

需要主意的是@()把结果放到一个序列中所以可以检索到多电池的情况。大多数系统都只有一个,但也有一部分系统有多个电池。

 

 

笔记:

复习WMI

@()的说道。

相关文章:

  • 2021-11-14
  • 2021-09-22
  • 2021-10-11
  • 2021-09-26
  • 2021-08-19
  • 2022-02-02
  • 2021-11-18
  • 2021-07-15
猜你喜欢
  • 2021-12-12
  • 2022-01-16
  • 2021-07-08
  • 2022-02-22
  • 2021-12-03
  • 2022-02-09
  • 2022-01-21
相关资源
相似解决方案