Command Line WMI: Query Remote Machines

Posted on April 19, 2012 by Jeff Hicks in Windows Server with 0 Comments

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).

Querying Remote Computers

So, let me go back into an interactive WMIC session on my Windows 7 computer.

After a little trial and error to get the syntax right, I have a command that works.

To connect to a remote computer, all I have to do is insert the /Node parameter in front. When you look at help in WMIC, pay close attention to the order parameters and switches specified.

Normally, you can simply specify the computername, but because the name has a dash, I had to enclose it in quotes. Hopefully this is more along the lines of what you will do:

But what if I wanted to query both of these machines?

Or you can put the names in a text file. With this option, I don’t have to enclose the name with a dash in quotes.

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.

The username must be in the domain\username format. Because I didn’t specify a password parameter, I was prompted. But I could have done this:

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:

You can always check what context WMIC is running under.

The fix is to define a null value for 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.

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.

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.

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.

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.

Sponsored