PowerTip of the Day-Check for a Battery

原文地址: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

@()的说道。

posted @   哥本哈士奇(aspnetx)  阅读(336)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示