2

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 .

3
  • win32_logicaldisk returns a collection of drives because a computer can have multiple drives. So you need a foreach loop iterating over each drive Commented Sep 13, 2017 at 7:16
  • What exactly do you expect that Disk row in the CSV to look like? Commented Sep 13, 2017 at 7:16
  • Under Disk column, I need the Disk letter (D:, E:, etc), capacity and free size in GB, maybe It needs 3 columns but I don't know how to achieve this. Commented Sep 13, 2017 at 7:24

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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.

11 Comments

The Disk column is blank. Not sure why.
Let's make it simpler. i just need the DeviceId, capacity and free space. No need for Media type. I think since every computer has multiple drives, then the disk out put comes out as null. Since disk output is trying to add the info into 1 column only. Am I right?
Are you opening it in Excel to check? Because the top line is blank so at first it looks empty but it's multiline so just expand the row and it should be there.
Yes, it's completely blank.
What version of windows?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.