What can I do about “WMIC is deprecated”?

南楼画角 提交于 2020-08-04 04:03:10

问题


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.

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?


回答1:


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

[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

For your particular case PowerShell alternative would be following:

wmic memorychip get capacity

Get-CimInstance -ClassName Win32_PhysicalMemory | Select-Object capacity

wmic diskdrive get Status,Model

Get-CimInstance -ClassName Win32_diskdrive | Select-Object status, model

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

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.

Example:

Get-CimInstance -ClassName Win32_diskdrive | select status, model | ConvertTo-JSON

Output:

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

Hope this helps




回答2:


Unless powershell can be used like a dos runtime to complete a command for Java program etc., it does not meet requirements since it's removing tools that other programs depend on with no replacement.



来源:https://stackoverflow.com/questions/57121875/what-can-i-do-about-wmic-is-deprecated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!