Reputation: 15
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;
The machines are not queried (or reported?) in the order they are defined in the txt list.
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?
Upvotes: 1
Views: 3062
Reputation: 5570
welcome to SO. There are a few things wrong with your script that you should take care of.
$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.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
Upvotes: 2