Help using custom psobject while using foreach to loop through computer list

I am still new to learning powershell and I am having trouble with the code I wrote below. I keep receiving an RPC error when trying to send the commands to remote computers. Please look my code over to let me know what I’m doing wrong. Thanks for your help!


$machines = get-content C:\Users\office\Desktop\Machines.txt
$array = @()

foreach ($machine in $machines){
$FreeGB = (gwmi win32_logicaldisk -ComputerName $machine -Filter drivetype=3).freespace /1gb -as [int]
$Serial = (gwmi win32_bios -ComputerName $machine).serialnumber
$Architecture = (gwmi win32_operatingsystem -ComputerName $machine).osarchitecture
$OS = (gwmi win32_operatingsystem -ComputerName $machine).caption
$computername = (gwmi win32_operatingsystem -ComputerName $machine).pscomputername

$properties = @{Computer = $computername;
FreeGB = $FreeGB;
Serial = $Serial;
Architecture = $Architecture;
OS = $OS

$output = New-Object psobject -Property $properties}

$array += $output}
Write-Output $output | ft -AutoSize

So, a couple of observations.

You’re burning a lot of time querying the same WMI class multiple times. Just query it once, get all the properties, and then pick whichever ones you want.

$wmi_os = Gwmi Win32_Operatingsystem
$os = $wmi_os.caption

There’s no need whatsoever to accumulate your results in an array. Just write-output each object as you create it. See

Now, in regards to your actual question, it’d be a lot easier to help if you shared the error message you’re getting.