How to fix this Powershell script using Get-WmiObject?

2.3k Views Asked by At

I have this piece of code that gets the information from the computers in the domain and outputs to a csv file. I tried to add a new line of code to also grab t he Disk information for the computers but I can't get it working as expected.

# Get the list of all computer names and export to CSV file
Get-ADComputer -Filter * | select Name | Export-Csv -Path 'C:\temp\computers.csv' -NoTypeInformation

# Import the computer names from CSV file and get the system information
$computers = Import-Csv “C:\Temp\computers.csv” | ForEach {

    $computerSystem = Get-WmiObject Win32_ComputerSystem -ComputerName $_.Name
    $computerOS = Get-WmiObject Win32_OperatingSystem -ComputerName $_.Name
    $computerCPU = Get-WmiObject Win32_Processor -ComputerName $_.Name
    $computerSN = Get-WmiObject Win32_bios -ComputerName $_.Name | Select-Object SerialNumber
    $computerDisk = Get-WmiObject win32_logicaldisk -ComputerName $_.Name | Select-Object DeviceId

    [PSCustomObject]@{
        'PCName' = $computerSystem.Name    
        'Model' = $computerSystem.Model   
        'RAM' = "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB)    
        'CPU' = $computerCPU.Name    
        'OS' = $computerOS.caption   
        'SN' = $computerSN.SerialNumber
        'User' = $computerSystem.UserName 
        'Disk' = $computerDisk.DeviceId | Format-Table DeviceId, MediaType, @{n="Size";e={[math]::Round($_.Size/1GB,2)}},@{n="FreeSpace";e={[math]::Round($_.FreeSpace/1GB,2)}}
    }

} | Export-Csv 'C:\Temp\system-info.csv' -NoTypeInformation

This is the line of codes for disk.

$computerDisk = Get-WmiObject win32_logicaldisk -ComputerName $_.Name | Select-Object DeviceId

And...

'Disk' = $computerDisk.DeviceId | Format-Table DeviceId, MediaType, @{n="Size";e={[math]::Round($_.Size/1GB,2)}},@{n="FreeSpace";e={[math]::Round($_.FreeSpace/1GB,2)}}

The other parameters work but only the disk info section isn't working. the output is: System.Object[] instead of displaying the info .

2

There are 2 best solutions below

11
On

You can do this to get the result I think you're after:

$Computers = Import-Csv 'C:\Temp\computers.csv'

$Computers | ForEach {

    $computerSystem = Get-WmiObject Win32_ComputerSystem -ComputerName $_.Name
    $computerOS = Get-WmiObject Win32_OperatingSystem -ComputerName $_.Name
    $computerCPU = Get-WmiObject Win32_Processor -ComputerName $_.Name
    $computerSN = Get-WmiObject Win32_bios -ComputerName $_.Name | Select-Object SerialNumber
    $computerDisk = Get-WmiObject win32_logicaldisk -ComputerName $_.Name | Select-Object DeviceId, MediaType, @{n="Size";e={[math]::Round($_.Size/1GB,2)}},@{n="FreeSpace";e={[math]::Round($_.FreeSpace/1GB,2)}}

    [PSCustomObject]@{
        'PCName' = $computerSystem.Name    
        'Model' = $computerSystem.Model   
        'RAM' = "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB)    
        'CPU' = $computerCPU.Name    
        'OS' = $computerOS.caption   
        'SN' = $computerSN.SerialNumber
        'User' = $computerSystem.UserName 
        'Disk' = $computerDisk | Format-Table | Out-String
    }

} | Export-Csv 'C:\Temp\system-info.csv' -NoTypeInformation

The first problem was that in the $computerDisk = line you were using Select-Object to return only the DeviceID property, but then were later trying to use the other properties.

The second problem was that you need to pipe Format-Table to Out-String when you output it to convert it to string format so that Export-CSV doesn't treat it as an object.

0
On

This has worked or me somewhat but it only grabs the info for the first drive. Also, the free space is larger than the Disk size which is weird.

$Computers = Import-Csv 'C:\Temp\computers.csv'

$Computers | ForEach {

    $computerSystem = Get-WmiObject Win32_ComputerSystem -ComputerName $_.Name
    $computerOS = Get-WmiObject Win32_OperatingSystem -ComputerName $_.Name
    $computerCPU = Get-WmiObject Win32_Processor -ComputerName $_.Name
    $computerSN = Get-WmiObject Win32_bios -ComputerName $_.Name | Select-Object SerialNumber
    $computerDisk = Get-WmiObject win32_logicaldisk -ComputerName $_.Name | Select-Object DeviceId, Size, FreeSpace

    [PSCustomObject]@{
        'PCName' = $computerSystem.Name    
        'Model' = $computerSystem.Model   
        'RAM' = "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB)    
        'CPU' = $computerCPU.Name    
        'OS' = $computerOS.caption   
        'SN' = $computerSN.SerialNumber
        'User' = $computerSystem.UserName 
        'Disk' = $computerDisk.DeviceId  | Format-Table | Out-String
        'Size' = $computerDisk.Size  | Format-Table | Out-String
        'Free Space' = $computerDisk.FreeSpace  | Format-Table | Out-String
    }

} | Export-Csv 'C:\Temp\system-info.csv' -NoTypeInformation