1

im trying to take a list of computers and pull information from them and export it into an excel sheet where i have all information of the systems per row. im trying to get information like:

  • Computername
  • OS
  • BIOS
  • Last boot
  • Etc

the code i wrote:

$computers = Get-Content "C:\example\Computers.txt)"


$OSinfo = Get-WmiObject Win32_OperatingSystem -ComputerName $computers | Select PSComputerName, Caption, OSArchitecture, Version, BuildNumber
$BIOSinfo = Get-WmiObject -Class Win32_BIOS -ComputerName $computers | Select-Object PSComputerName, Manufacturer, SerialNumber, SMBIOSBIOSVersion
$lastboot = Get-CimInstance win32_operatingsystem -ComputerName $computers | select csname, lastbootuptime

$objects +=$OSinfo +=$BIOSinfo +=$lastboot

$objects | Export-csv -Path "C:\example\output.csv"

however, i cant figure out how to put all this information into one spreadsheet tab. i also cant figure out how to tell the script if it cant ping or find it, to just say "offline"

3
  • Can you use this script instead? community.spiceworks.com/scripts/show/4899-get-systeminfo Commented Apr 5, 2022 at 14:26
  • i am now seeing the information in PowerShell, but the excel export isnt giving me the information. im going to review the link you provided Commented Apr 5, 2022 at 14:40
  • adding objects with different properties into one CSV will truncate the objects to only the properties in the 1st object. for an example of one way to get it all into one csv, lookee ... basic remote parallel SystemInfo demo script - Pastebin.com — pastebin.com/cGL5biWH Commented Apr 5, 2022 at 14:51

1 Answer 1

1

This should work for you:

$computers = Get-Content "C:\example\Computers.txt"

# Define the properties we want to select from each query
$osColumns = 'PSComputerName', 'Caption', 'OSArchitecture', 'Version', 'BuildNumber'
$biosColumns = 'PSComputerName', 'Manufacturer', 'SerialNumber', 'SMBIOSBIOSVersion'
$lastbootColumns = 'csname', 'lastbootuptime'

# Obtain the desired information from Wmi/Cim
$OSinfo = Get-WmiObject Win32_OperatingSystem -ComputerName $computers | Select-Object $osColumns
$BIOSinfo = Get-WmiObject -Class Win32_BIOS -ComputerName $computers | Select-Object $biosColumns
$lastboot = Get-CimInstance win32_operatingsystem -ComputerName $computers | Select-Object $lastbootColumns

# Iterate over the computers and collect the computer-specific information
$computerInfo = foreach( $computer in $computers ) {
  $thisOSInfo = $OSInfo | Where-Object { $_.PSComputerName -eq $computer }
  $thisBIOSInfo = $BIOSinfo | Where-Object { $_.PSComputerName -eq $computer }
  $thisLastboot = $lastboot | Where-Object { $_.csname -eq $computer }

  # This row object will be returned as a PSCustomObject for export
  $row = @{}
  foreach( $prop in @( $thisOSInfo.psobject.Properties.Name ) ) {
    if( $prop -in $osColumns ) {
      $row[$prop] = $thisOSInfo.$prop
    }
  }

  foreach( $prop in @( $thisBIOSInfo.psobject.Properties.Name ) ) {
    if( $prop -in $biosColumns ) {
      $row[$prop] = $thisBIOSInfo.$prop
    }
  }

  foreach( $prop in @( $thisLastboot.psobject.Properties.Name ) ) {
    if( $prop -in $lastbootColumns ) {
      $row[$prop] = $thisLastboot.$prop
    }
  }
  
  [PSCustomObject]$row
}

$computerInfo | Export-Csv -NoTypeInformation \path\to\report.csv


The way this works:

  • Well-define the columns we want from each query. We'll use those later to make sure we only grab properties we want from the resulting query responses. Note that Get-CimInstance will have the csname property instead of PSComputerName.
  • Query WMI classes for information on known computers, store each response in its own variable.
  • Iterate over each computer queried, building that row of information from each query response.
    • Only add the query property if it exists in the corresponding *Columns variable. This is less necessary for your code but becomes necessary when exporting some other, non-WMI/Cim objects, or if you don't use Select-Object earlier, to reduce noise.
  • Each row is defined as a hastable for ease of modification, then converted to a PSCustomObject to it behaves more like an immutable object upon return. This is necessary for the output of Export-Csv to output the way you want.
  • Export the computer information with Export-Csv to a CSV file, which is now an array of PSCustomObjects where each row should be a single computer you've queried.
Sign up to request clarification or add additional context in comments.

1 Comment

ok see i see how you made this work, i was using one for each, but you made it all work together. thank you!!

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.