/build/static/layout/Breadcrumb_cap_w.png
06/10/2019 94 views

$computers=Get-content "<network path to text file>\slist.txt"
foreach($computer in $computers) {
    $tc=Test-Connection -ComputerName $computer -Count 1 -Quiet
    if ($tc -eq $false) {
        $report=[PSCustomObject]@{
            'Computer Name'    = $computer
            'Computer Status'  = $tc
            'OS'               = $null
            'Manufacturer'     = $null
            'Model'            = $null
            'Cores'            = $null
        }
    }
    else {
        $obj=systeminfo /s $computer /fo csv | findstr /c:'OS Name' /c:'System Manufacturer' /c:'Model'
        $proc=Get-WmiObject -class Win32_Processor -ComputerName BBCSRV | select @{LABEL='NumberofCores';EXPRESSION={$_.numberofcores}}
        $report=[PSCustomObject]@{
            'Computer Name'    = $computer
            'Computer Status'  = $tc
            'OS'               = $obj.OSName
            'Manufacturer'     = $obj.SystemManufacturer
            'Model'            = $obj.Model
            'Cores'            = $proc.NumberofCores
        }
    }
}
$report | Export-Csv "<path to save outputfile>\VirPhys.csv" -Append -NoTypeInformation

0 Comments   [ + ] Show comments

Comments


All Answers

1

You don't need separate objects for $report, PSCustomObject is a collection already, so it appends dynamically. I refactored your script to use the more modern Cim Instance classes and a few other tweaks, but thiis will return the same as yours.


Function Get-ComputerInformation {
param(
[Parameter(Mandatory)]
[string[]] $ComputerName
)

foreach ($computer in $ComputerName) {
$tc = Test-Connection -ComputerName $computer -Count 1 -Quiet

If (($TC)) {
$CPU = Get-CimInstance -ClassName Win32_Processor -ComputerName $Computer -ErrorAction Stop
$OS = Get-CimInstance -ClassName Win32_OperatingSystem -ComputerName $Computer -ErrorAction Stop
$System = Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $Computer -ErrorAction Stop
}
Else {$CPU,$OS,$System = $null}
[PSCustomObject]@{
'Computer Name' = $computer
'Computer Status' = $tc
'OS' = $OS.Caption
'Manufacturer' = $System.Manufacturer
'Model' = $System.Model
'Cores' = $CPU.NumberofCores
}
}
}

PS> Get-ComputerInformation -ComputerName 'localhost','localhost','computerA','computerB'


Computer Name : localhost
Computer Status : True
OS : Microsoft Windows 10 Enterprise
Manufacturer : Dell Inc.
Model : XPS 13 9370
Cores : 4

Computer Name : localhost
Computer Status : True
OS : Microsoft Windows 10 Enterprise
Manufacturer : Dell Inc.
Model : XPS 13 9370
Cores : 4

Computer Name : computerA
Computer Status : False
OS :
Manufacturer :
Model :
Cores :

Computer Name : computerB
Computer Status : False
OS :
Manufacturer :
Model :
Cores :
Answered 06/10/2019 by: isudothings
Purple Belt

  • I'm still learning how to use powershell, so please bear with me, but....is there a way when using a function to put the text file of servers in the code instead of running another line to input them? How do I output this to a csv?
    • Sure, you just call your function like this, then pipe it to a csv.

      myServers.txt has one hostname per line and no whitespace.

      Get-ComputerInformation -ComputerName (Get-Content -Path 'C:\temp\myServers.txt') |
      Export-Csv -Path 'C:\temp\myOutput.csv' -NoTypeInformation -Append


      Alternately, you can define the servers in the code itself using a string array.

      $myServers=@(
      'server01'
      'server02'
      )

      ...

      Get-ComputerInformation -ComputerName $myServers |
      Export-Csv -Path 'C:\temp\myOutput.csv' -NoTypeInformation -Append


      NOTE: If you have machines with more than one socket, you will need to convert your $CPU.NumberofCores prior to export, as CSV does not know how to handle PS objects. [string]$CPU.NumberOfCores should work in that case.