Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
529 views
in Technique[技术] by (71.8m points)

windows - What can I do about "WMIC is deprecated"?

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...