Question

I'm trying to write a powershell script to report the video cards used by all machines in a given range of the domain (GUM1 to GUM150). I've actually successfully made the machines report their video cards via remote powershell, but there are two problems;

  1. The machines are not queried (or reported?) in the order they are defined in the txt list.

  2. The hostname is not reported back, which means I have a jumbled list of results and no corresponding machine name.

Here's what I've got so far;

$comp = get-content c:\users\myusername\pcs.txt

Where pcs.txt is a list of the machines by hostname, each name on a new line.

Then;

get-wmiobject -class CIM_VideoController caption -namespace root/cimv2 -computername $comp | sort-object -property machinename | format-table machinename, caption > gpus.txt

The reference book I'm using stated that using get-wmiobject automatically includes the machinename (i.e. the hostname) in the results of the object that returns. But I can't seem to make it appear in the list beside the GPU names (caption) it returns, it just returns a table with the GPU names and a blank column for machinename.

I've seen a lot of examples of querying details of a videocard, but they all return an extensive set of details about the card in a long list, per machine, whereas I want ONLY the hostname and video card name returned in a table all together for easy copy/paste into a spreadsheet. I'm not sure CIM_VideoController is the best WMI object for this but none of the others I've seen return both GPU and hostname with the results object.

Any ideas?

Was it helpful?

Solution

welcome to SO. There are a few things wrong with your script that you should take care of.

  1. You are calling calling one WMI query on a list of servers. You name the variable $comp, but it holds an array of comps, and the -computername parameter does not accept a list of computernames. You can fix this by using a foreach loop.
  2. You are attempting to access a property of a WMI class that does not exist. Use the documentation to find what are the actual properties you need http://msdn.microsoft.com/en-us/library/aa388668(v=vs.85).aspx (machinename is not a property)
  3. Once you implement the first point there is no need to try to sort the object, your loop will go through the text file, query the computer, and append the results in the order specified in your pcs.txt

This one liner will take care of all of that:

get-content c:\users\myusername\pcs.txt | % { get-wmiobject -class CIM_VideoController -namespace root/cimv2 -computername $_ | format-table SystemName, caption >> gpus.txt}

This gets the array of pcs from the text file, pipes each one into the wmi query (% is a foreach loop, and $_ is the object in the pipe, in this case each line of pcs.txt), and then appends the results of each individual query onto gpus.txt, > just overwrites each time. If you wish to overwrite the file each time you run, then you should collect all the output first in variable, and then output once the loop is finished. The below scripts does that, and also provides a more readable solution (just less fancy)

$table = @()
$comps = get-content c:\users\myusername\pcs.txt
foreach($comp in $comps) {
    $table += get-wmiobject -class CIM_VideoController -namespace root/cimv2 -computername $comp  | format-table SystemName, caption
}
$table > gpus.txt
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top