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.