0

I am trying to query computer system information and hardware information (Number of monitors connected) through PowerShell. I am doing this by pulling all of the computer's names and uploading them to a CSV file and have PowerShell move the CSV file into an array. Then querying the data for each item in the CSV and producing a new CSV. Currently I have the below code. It works when I assign $computer to a single computer but then prints out the computer's information the same number of times as items in the original CSV file. The only thing that does not work as it does not print any monitor information. When I try to run the script utilizing the CSV file, I get the below errors. Any help is greatly appreciated.

$computers = Get-Content -Path "\FayCom.txt"
$array1 = @()

foreach ($computer in $computers) 
{
    $query = Get-WmiObject -Class win32_computersystem -Computer $computer
    $name = $query.Name
    $model = $query.Model
    $users = $query.Username
    $Monitors = Get-WmiObject WmiMonitorID -Namespace root\wmi -Computer $computer |findstr "GENUS"
    
    $Object = New-Object PSObject
    $Object | Add-Member -MemberType NoteProperty -Name "ComputerName" -Value $name
    $Object | Add-Member -MemberType NoteProperty -Name "Model" -Value $model
    $Object | Add-Member -MemberType NoteProperty -Name "LoggedOnUser" -Value $users
    $Object | Add-Member -MemberType NoteProperty -Name "Monitors" -Value $Monitors
    $array1 += $Object
    }
$array1 | Export-Csv -Path "\test1.txt" -NoTypeInformation

ERRORS

Get-WmiObject : The RPC server is unavailable.
At line:3 char:14
+ ...    $query = Get-WmiObject -Class win32_computersystem -Computer $comp ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Get-WmiObject : The RPC server is unavailable.
At line:7 char:17
+ ... $Monitors = Get-WmiObject WmiMonitorID -Namespace root\wmi -Computer  ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Get-WmiObject : The RPC server is unavailable.
At line:3 char:14
+ ...    $query = Get-WmiObject -Class win32_computersystem -Computer $comp ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Get-WmiObject : The RPC server is unavailable.
At line:7 char:17
+ ... $Monitors = Get-WmiObject WmiMonitorID -Namespace root\wmi -Computer  ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

I tried to pull the data from a CSV file and convert it into an array. Then have the query iterate over each item to pull the needed data. Needed data being (PC Name, PC Model, Last User Logged in, Number of monitors connected and the monitor information). The current code I have works for one computer besides providing the monitor information but will not iterate over the array.

mklement0
  • 382,024
  • 64
  • 607
  • 775
DaHawg
  • 73
  • 5
  • 1
    As an aside: The CIM cmdlets (e.g., `Get-CimInstance`) superseded the WMI cmdlets (e.g., `Get-WmiObject`) in PowerShell v3 (released in September 2012). Therefore, the WMI cmdlets should be avoided, not least because PowerShell (Core) v6+, where all future effort will go, doesn't even _have_ them anymore. Note that WMI still _underlies_ the CIM cmdlets, however. For more information, see [this answer](https://stackoverflow.com/a/54508009/45375). – mklement0 Nov 07 '22 at 21:35
  • 1
    As an aside: Extending arrays in a loop with `+=` is inefficient, because a _new_ array must be created behind the scenes _in every iteration_, given that arrays are of fixed size; a much more efficient approach is to use a `foreach` loop as an _expression_ and let PowerShell itself collect the outputs in an array: `[array] $outputs = foreach (...) { ... }` - see [this answer](https://stackoverflow.com/a/60708579/45375). In case you need to create arrays manually, e.g. to create _multiple_ ones, use an efficiently extensible list type - see [here](https://stackoverflow.com/a/60029146/45375). – mklement0 Nov 07 '22 at 21:36
  • As an aside: Unless you're still using PSv2, a much simpler and more efficient way to construct custom objects is to use the following syntactic sugar: `[pscustomobject] @{ foo = 1; bar = 'two' }` - see [this answer](https://stackoverflow.com/a/45293404/45375) – mklement0 Nov 07 '22 at 21:37
  • The `Get-WmiObject` error message implies that the target computers are either not reachable (e.g. because they're powered off) or don't exist. – mklement0 Nov 07 '22 at 21:38
  • 1
    Yes, to simplify things, **The RPC server is unavailable.** is your error, so basically you have no connectivity to the target machine. Where is your list of computers coming from? Are you using bare names or FQDNs? What happens if you do `Test-WSMan`? Is it using SSL? You can check with `Test-WSMan -UseSSL`. If that fails, is WinRM running on the target machines and/or are the WinRM ports blocked by a firewall? If the test succeeds, does your user context have perms? If it is using SSL, some advice here: https://devblogs.microsoft.com/powershell/cim-cmdlets-some-tips-tricks/ – LeeM Nov 08 '22 at 02:51
  • So to throw a major wrench in this issue. My company has winrm disabled across the entire company. Is there a way to still accomplish this without that? Just found this out this morning when I went home and rewrote the entire thing. – DaHawg Nov 08 '22 at 13:47
  • The CIM cmdlets (see first comment) use a different, firewall-friendly remoting protocol (the same one as PowerShell remoting) - perhaps that is allowed in your company. – mklement0 Nov 08 '22 at 14:26
  • Get an error that the client cannot connect. Because WinRm service is not running. The only way I have been able to get anything to work has been with the Get-wmiObject – DaHawg Nov 08 '22 at 16:07

0 Answers0