Search code examples
powershellcsvforeachimportexport-to-csv

Remotely get serial number and model from list file and export it to arranged excel in PowerShell


I have Nice PowerShell code that Remotely gets the serial number and model (along with other Details) from list and export it to nice view in PowerShell.

However, i want to export it to an arranged csv file, so instead of this:

enter image description here

I'll have that:

enter image description here

Here's the Original code:

$ArrComputers =  "yakovcomputer"
#Specify the list of PC names in the line above. "." means local system

Clear-Host
foreach ($Computer in $ArrComputers) 
{
    $computerSystem = get-wmiobject Win32_ComputerSystem -Computer $Computer
    $computerBIOS = get-wmiobject Win32_BIOS -Computer $Computer
    $computerOS = get-wmiobject Win32_OperatingSystem -Computer $Computer
    $computerCPU = get-wmiobject Win32_Processor -Computer $Computer
    $computerHDD = Get-WmiObject Win32_LogicalDisk -ComputerName $Computer -Filter drivetype=3
        write-host "System Information for: " $computerSystem.Name -BackgroundColor DarkCyan
        "-------------------------------------------------------"
        "Manufacturer: " + $computerSystem.Manufacturer
        "Model: " + $computerSystem.Model
        "Serial Number: " + $computerBIOS.SerialNumber
        "CPU: " + $computerCPU.Name
        "HDD Capacity: "  + "{0:N2}" -f ($computerHDD.Size/1GB) + "GB"
        "HDD Space: " + "{0:P2}" -f ($computerHDD.FreeSpace/$computerHDD.Size) + " Free (" + "{0:N2}" -f ($computerHDD.FreeSpace/1GB) + "GB)"
        "RAM: " + "{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB"
        "Operating System: " + $computerOS.caption + ", Service Pack: " + $computerOS.ServicePackMajorVersion
        "User logged In: " + $computerSystem.UserName
        "Last Reboot: " + $computerOS.ConvertToDateTime($computerOS.LastBootUpTime)
        ""
        "-------------------------------------------------------"
}

So i try to add Import-Csv C:list.txt | ForEach-Object (that have the yakovcomputer name in it) at the start

(Undecided if to use $computers = Get-Content c:\list.txt instead),

And add export-csv c:\temp\list.csv command at the end.

So now it is look like that (Edited code):

Import-Csv C:list.csv | ForEach-Object
    {
        $computerSystem = get-wmiobject Win32_ComputerSystem -Computer $Computer
        $computerBIOS = get-wmiobject Win32_BIOS -Computer $Computer
        $computerOS = get-wmiobject Win32_OperatingSystem -Computer $Computer
        $computerCPU = get-wmiobject Win32_Processor -Computer $Computer
        $computerHDD = Get-WmiObject Win32_LogicalDisk -ComputerName $Computer -Filter drivetype=3
    }
export-csv c:\temp\list.csv

But it keeps giving errors all the time.

I'm aware that I'm writing something (code) wrong here, but, what is the correct way to achieve that?


Solution

  • If your list.txt is just a plain text file of computer names, with one name per line, Get-Content will be enough. You can access the current line (current item) with $_ or $PSItem.

    Export-Csv requires objects. So you would have to convert your data into objects. There are several ways to do it, most of them require creating a HashTable. Here is my preferred version:

    Get-Content c:\list.txt | foreach
    {
        $computerSystem = Get-WmiObject Win32_ComputerSystem -Computer $_
        $computerBIOS = Get-WmiObject Win32_BIOS -Computer $_
        $computerOS = Get-WmiObject Win32_OperatingSystem -Computer $_
        $computerCPU = Get-WmiObject Win32_Processor -Computer $_
        $computerHDD = Get-WmiObject Win32_LogicalDisk -ComputerName $_ -Filter drivetype=3
        New-Object PSObject -Property @{
            "Manufacturer" = $computerSystem.Manufacturer
            "Model" = $computerSystem.Model
            "Serial Number" = $computerBIOS.SerialNumber
            "CPU" = $computerCPU.Name
            "HDD Capacity"  = ("{0:N2}" -f ($($computerHDD|measure Size -sum).Sum/1GB) + "GB")
            "HDD Space" = ("{0:P2}" -f ($($computerHDD|measure FreeSpace -sum).Sum/$($computerHDD|measure Size -sum).Sum) + " Free (" + "{0:N2}" -f ($($computerHDD|measure FreeSpace -sum).Sum/1GB) + "GB)")
            "RAM" = ("{0:N2}" -f ($computerSystem.TotalPhysicalMemory/1GB) + "GB")
            "Operating System" = ($computerOS.caption + ", Service Pack: " + $computerOS.ServicePackMajorVersion)
            "User logged In" = $computerSystem.UserName
            "Last Reboot" = $computerOS.ConvertToDateTime($computerOS.LastBootUpTime)
        }
    } | Export-Csv c:\temp\list.csv -NoTypeInformation
    

    Note the $($computerHDD|measure Size -sum).Sum constructs, to add up the values for all HDDs if there are multiple.

    Also, consider using CIM instead of WMI instead.