Command Line WMI: Query Remote Machines

Welcome back to our look at Windows Management Instrumentation, or WMI. In part one we we looked at basic syntax and querying for the local computer, discovering ways to gather WMI information using the command line tool WMIC. But where WMI really is worth your time is the ability to gather information from remote computers. Generally, most WMI classes are the same across operating systems, so I try to test locally first and then run my command against remote computers.

Today in part two of this three-part series, we’ll learn how to query remote machines and work with WMIC right from the command line. Later, in part three, we’ll take a closer look at some advanced ways of formatting data.

However, let me take a moment to point out that WMI continues to change with each new operating system release. New classes are added and sometimes even new properties. Just because you can get information from a Windows 7 computer doesn’t necessarily mean Windows XP has the same information. If you are ever in doubt, check the documentation on MSDSN (Win32 Classes).

Alternate Credentials

WMI uses my current credentials. However, it is possible to specify alternate credentials when querying remote computers. You can’t specify a different user for the local computer. If you are specifying multiple remote computers, the same credential will be used for all of them. The credential must have local admin rights on the remote computer.

If there’s any chance your password might contain special characters, enclose it in quotes, but be careful. When you specify an alternate credential, WMIC keeps it and uses it for future commands. This can result in problems like this:

PowerShell

1

2

3

4

wmic:root\cli>computersystem list full

Node-SERENITY

ERROR:

Description=User credentials cannot be used forlocal connections

You can always check what context WMIC is running under.

PowerShell

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

wmic:root\cli>context

NAMESPACE:root\cimv2

ROLE:root\cli

NODE(S):SERENITY

IMPLEVEL:IMPERSONATE

[AUTHORITY:N/A]

AUTHLEVEL:PKTPRIVACY

LOCALE:ms_409

PRIVILEGES:ENABLE

TRACE:OFF

RECORD:N/A

INTERACTIVE:ON

FAILFAST:OFF

OUTPUT:STDOUT

APPEND:STDOUT

USER:jdhitsolutions\administrator

AGGREGATE:ON

The fix is to define a null value for USER.

PowerShell

1

2

wmic:root\cli>/user:""

Password ignored fornull user.

This also implies that you can set a USER and PASSWORD value ahead of time and it will always be used without having to explicitly specify the parameter.

Saving Output

Most likely you will want to save the output. WMIC offers several options. Again, we’ll ask for help.

The default is STDOUT or to the console which we’ve seen. I like the Clipboard option. Where this gets tricky is that you have to specify OUTPUT at the beginning of your expression.

PowerShell

1

wmic:root\cli>/node:Quark/output:clipboard computersystem list full

But now I have the results in the Clipboard, so I can paste them wherever I need. Of course, if you need a text file anyway, go ahead and create it.

PowerShell

1

wmic:root\cli>/node:Quark/output:"c:\work\quark-sys.txt"computersystem list full

Again, the – is a special character so I enclosed my file path in quotes. You can also append to any of these options, which is handy if you want to create a report from multiple commands.

PowerShell

1

2

3

4

5

6

7

8

9

10

wmic:root\cli>/append/?

APPEND-Specifies the mode foroutput redirection.

USAGE:

/APPEND:

NOTE:::=(STDOUT|CLIPBOARD|)

STDOUT-Output will be redirected to the STDOUT.

CLIPBOARD-Output will be copied on to CLIPBOARD.

-Output will be appended to the specified file.

These are the basics. I’ll cover some advanced options in another article.

Command Line Options

Finally, although we’ve been doing all of this in an interactive mode, if you have a command that works, you can run it right from the command prompt. This is a one line command.

PowerShell

1

C:\>wmic/node:@c:\work\mycomputers.txt/Output:"C:\Work\ProcessorReport.txt"cpu get Systemname,Name,Description,Manufacturer,AddressWidth,DeviceID/format:list

Conclusion

Once you learn what aliases and properties you need, it is not difficult to capture an amazing amount of system management information. I’ll be back next time to look at some advanced ways of formatting data.

MEMBER LOGIN:

BECOME A PETRI MEMBER:

Don't have a login but want to join the conversation? Sign up for a Petri Account

About the Contributor

Jeffery Hicks is an IT veteran with over 25 years of experience, much of it spent as an IT infrastructure consultant specializing in Microsoft server technologies with an emphasis in automation and efficiency. He is a multi-year recipient of the Microsoft MVP Award in Windows PowerShell. He works today as an independent author, teacher and consultant. Jeff has written for numerous online sites and print publications and is a frequent speaker at technology conferences and user groups. His latest book is PowerShell Scripting and Toolmaking.