关于“ WMIC已过时”,我该怎么办? [英] What can I do about "WMIC is deprecated"?

查看:1152
本文介绍了关于“ WMIC已过时”,我该怎么办?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直依靠这两个命令:

I've been relying on these two commands:

wmic memorychip get capacity        // Outputs how much RAM there is (in a convoluted manner).
wmic diskdrive get Status,Model     // Checks whether the HDDs/SSDs on the system are (supposedly) still "OK" and working.

今天,我随便键入 wmic以查看是否可以将JSON输出输出到上述命令。它以红色文字打印的第一件事是:

Today, I casually typed "wmic" to see if I could get JSON output to the above commands. The first thing it printed, in red text, was this:

WMIC is deprecated.

我对此感到非常震惊。已弃用?好吧...那我绝对不应该依赖它。那么,这两个命令的现代替代品是什么?它们甚至存在吗?为什么他们只告诉我们它已被零更多信息弃用?

I was pretty shocked by this. It's deprecated? Alright... Then I definitely should not be relying on it. What are the "modern alternatives" for those two commands, then? Do they even exist? Why do they just tell us that it's "deprecated" with zero further information?

推荐答案

如评论中所述,WMIC是一种实用工具充当与WMI通信的接口。不赞成使用WMI本身,而是只是界面。由于Microsoft正在推动PowerShell,所以我相信正式的继任者wmic将是PowerShell Commandlet Get-WmiObject 。可以在Microsoft文档中找到如何使用它: LINK

As mentioned in comments, WMIC is utility that acts as interface to communication with WMI. It's not WMI itself that is being deprecated, but "just" the interface. Since Microsoft is pushing PowerShell, I believe official successor wmic would be PowerShell commandlet Get-WmiObject. How to use this can be found on Microsoft documentation: LINK

[更新] 正如注释中正确指出的那样,命令行开关 Get- WmiObject 应该在一天之内停用,不建议使用。唯一合适的方法是 Get-CimInstance ,其语法与 Get-WmiObject 几乎相同。请参阅Microsoft文档: LINK

[UPDATED] As correctly pointed out within comment, commandlet Get-WmiObject shall sunset one day and is not encouraged to be used. Only proper method is Get-CimInstance, which has pertty much same syntax as Get-WmiObject. See Microsoft documentation: LINK

对于您的特殊情况,PowerShell替代方案如下:

For your particular case PowerShell alternative would be following:


wmic内存芯片获得容量

wmic memorychip get capacity

Get-CimInstance -ClassName Win32_PhysicalMemory |选择对象容量


wmic磁盘驱动器获取状态,型号

wmic diskdrive get Status,Model

Get-CimInstance -ClassName Win32_diskdrive |选择对象状态,模型

wmic 中的命令通常来自WMI类名称,但这并不是真正的经验法则。使用PowerShell,您将改为使用真实的类名访问WMI,因此,如果需要,您可能需要寻找其他类

Commands in wmic are usually derived from WMI class names, but it's not really rule of thumb. With PowerShell you are accessing WMI by it's real class name instead, so you may need to seek for other classes if needed

PowerShell 超过 wmic 的是,输出是一个对象,您可以轻松地继续使用输出,而wmic返回的字符串只是您最终需要解析的字符串例如,如果在脚本内部使用,则会带来例如输出格式-您可以轻松地将任何输出重新格式化为例如您提到的JSON,只需将命令通过另一个管道传递到Commandlet ConvertTo-Json 中,您将获得预期的输出。

Undisputed advantage to PowerShell over wmic is that output is an object and you can easily continue working with the output, while wmic returns a string only that you eventually need to parse for example if used inside scripts and that brings another benefit of e.g. output formatting - you can easily reformat any output for example to as you mentioned JSON, just pass your command through another pipe into commandlet ConvertTo-Json and you will have your expected output.

示例:

Get-CimInstance -ClassName Win32_diskdrive |选择状态,型号| ConvertTo-JSON

输出:

{
status: OK,
model: SAMSUNG MZNTY256HDHP-000L7
}

希望有帮助

这篇关于关于“ WMIC已过时”,我该怎么办?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆