welcome to SO. There are a few things wrong with your script that you should take care of.
- 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. - 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)
- 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