I built this, which is working fine, it takes about 7-9 seconds to run and display.
I'm wondering, is there a faster/optimal way of building this custom object?
As you can see, I want all the drivers information from Win32_PNPsigneddriver but to that, I add two other properties from Win32_PNPentity (configmanagererrorcode and status) based on the DeviceID.
This way, the final object contains all drivers and shows if there is an error for the device using that driver.
$poste = "COMPUTER1234"
$DriversUp = Get-WmiObject -computername $poste Win32_PNPsigneddriver |
Where-Object {$_.DeviceName -ne $null}
$Devices = Get-WmiObject -computername $poste Win32_PNPentity
$DriversDevices = foreach ($driver in $DriversUp) {
$driver |
Select-Object DeviceClass, Manufacturer, DeviceName,
FriendlyName, DriverName, InfName,
#{name='Status';expression={$Devices | Where-Object {$_.DeviceID -eq "$($driver.DeviceID)"} | Select-Object -ExpandProperty status}},
#{name='ConfigManagerErrorCode';expression={$Devices | Where-Object {$_.DeviceID -eq "$($driver.DeviceID)"} | Select-Object -ExpandProperty ConfigManagerErrorCode}},
#{name='DriverDate';expression={[DateTime]::ParseExact(($_.DriverDate).Split('.')[0], "yyyyMMddHHmmss", [System.Globalization.CultureInfo]::InvariantCulture)}},
DriverVersion
}
$DriversDevices |
Sort-Object DeviceClass |
Out-GridView -Title "$poste - Drivers utilisés"
Like I said everything works fine already. However, I'm curious to know if there's a faster way!
Starting in PowerShell 3.0, the Get-WmiObject cmdlet has been superseded by Get-CimInstance.
$poste = "COMPUTER1234"
$cimses = New-CimSession -ComputerName $poste
$p = & {$args} DeviceClass Manufacturer DeviceName FriendlyName DriverName `
InfName DriverVersion DeviceID DriverDate
Get-CimInstance -CimSession $cimses -ClassName Win32_PnPSignedDriver `
-Property $p -Filter 'DeviceName != NULL' |
ForEach-Object {
$dev = Get-CimInstance -CimSession $cimses -ClassName Win32_PnPEntity `
-Property Status, ConfigManagerErrorCode `
-Filter "PNPDeviceID='$($_.DeviceID.Replace('\', '\\'))'"
[pscustomobject]#{
DeviceClass = $_.DeviceClass
Manufacturer = $_.Manufacturer
DeviceName = $_.DeviceName
FriendlyName = $_.FriendlyName
DriverName = $_.DriverName
InfName = $_.InfName
Status = $dev.Status
ConfigManagerErrorCode = $dev.ConfigManagerErrorCode
DriverDate = '{0:yyyyMMddHHmmss}' -f $_.DriverDate
DriverVersion = $_.DriverVersion
}
} | Sort-Object DeviceClass | Out-GridView -Title "$poste - Drivers utilisés"
Remove-CimSession -CimSession $cimses
On my machine, the type of the Win32_PnPSignedDriver.DriverDate member is DateTime.
Get-CimClass -ClassName Win32_PnPSignedDriver |
Select-Object -ExpandProperty CimClassProperties |
Where-Object Name -eq 'DriverDate' |
Select-Object CimType | Format-Table -AutoSize
CimType
-------
DateTime
The bottleneck is searching for a device every time using Where-Object.
The following code uses Group-Object to solve the issue.
$poste = "COMPUTER1234"
$DriversUp = Get-WmiObject -ComputerName $poste Win32_PnPSignedDriver -Filter "DeviceName != NULL"
$Devices = Get-WmiObject -ComputerName $poste Win32_PnPEntity -Property DeviceID,Status,ConfigManagerErrorCode
#($DriversUp; $Devices) | Group-Object DeviceID | Where-Object Count -eq 2 | ForEach-Object {
$driver, $device = $_.Group
[pscustomobject]#{
DeviceClass = $driver.DeviceClass
Manufacturer = $driver.Manufacturer
DeviceName = $driver.DeviceName
FriendlyName = $driver.FriendlyName
DriverName = $driver.DriverName
InfName = $driver.InfName
Status = $device.Status
ConfigManagerErrorCode = $device.ConfigManagerErrorCode
DriverDate = [datetime]::ParseExact($driver.DriverDate.Substring(0, 14), "yyyyMMddHHmmss", $null)
DriverVersion = $driver.DriverVersion
}
} | Sort-Object DeviceClass | Out-GridView -Title "$poste - Drivers utilisés"
This is how I would do it, not big difference in performance, but more clear IMO:
Used an ArrayList for the results, also replaced the Where-Object on the Get-WmiObject with -Filter, it's a bit faster...
$poste = "COMPUTER1234"
$DriversUp = Get-WmiObject -computername $poste Win32_PNPsigneddriver -Filter "DeviceName != NULL"
$Devices = Get-WmiObject -computername $poste Win32_PNPentity
$DriversDevices = New-Object System.Collections.ArrayList
foreach ($driver in $DriversUp) {
$row = "" | Select DeviceClass,Manufacturer,DeviceName,FriendlyName,DriverName,
InfName,Status,ConfigManagerErrorCode,DriverDate,DriverVersion
$row.DeviceClass = $driver.DeviceClass
$row.Manufacturer = $driver.Manufacturer
$row.DeviceName = $driver.DeviceName
$row.DriverName = $driver.DriverName
$row.InfName = $driver.InfName
$row.Status = ($Devices | ? {$_.DeviceID -eq $driver.DeviceID}).Status
$row.ConfigManagerErrorCode = ($Devices | ? {$_.DeviceID -eq $driver.DeviceID}).ConfigManagerErrorCode
$row.DriverDate = [datetime]::ParseExact(($driver.DriverDate.Split('.')[0]),"yyyyMMddHHmmss",$null)
$row.DriverVersion = $driver.DriverVersion
[void]$DriversDevices.Add($row)
}
$DriversDevices | Sort-Object DeviceClass | Out-GridView -Title "$poste - Drivers utilisés"
Related
Get-WmiObject -Class Win32_OperatingSystem -ComputerName (Get-Content "C:\Temp\Servers.txt") | SELECT-Object PSComputerName, #{Name="Memory (RAM in GB)";Expression={[Math]::Round($_.TotalVisibleMemorySize/1024/1024)}} | Format-Table
Get-WmiObject -Class Win32_logicaldisk -ComputerName (Get-Content "C:\Temp\Servers.txt") | Select-Object PSComputerName, DriveType, DeviceID, VolumeName, #{Name="Size";Expression={[math]::ceiling($_.Size /1GB)}} , #{Name="FreeSpace";Expression={[math]::ceiling($_.FreeSpace /1GB)}}, Compressed | where DriveType -eq 3 | Format-Table
Get-WmiObject -Class Win32_OperatingSystem -ComputerName (Get-Content "C:\Temp\Servers.txt")| Select-Object PSComputerName, BuildNumber, BuildType, Caption, CodeSet, OSArchitecture, SystemDrive, TotalVisibleMemorySize, Version | Format-Table
Get-WmiObject -Class win32_product -ComputerName (Get-Content "C:\Temp\Servers.txt") | Select-Object Name, Version, Vendor, InstallDate | Format-Table
Get-WmiObject -Class Win32_Service -ComputerName (Get-Content "C:\Temp\Servers.txt") | Select-Object PSComputerName, DisplayName, StartName, PathName, StartMode| where DisplayName -Like "*xyz*" |Format-Table
I have till now managed to piece together the above to get the information I need from serveral servers, however now I want to format it so that I can collate information for each server in a format that I can display
for eg.
Server : ABC
RAM : 64 GB
Number of Processors : 8
Disk :
Table of disk Sizes Etc
Any pointers would be appreciated
With all these properties, you would get a nested object array, which probably is easiest to view in JSON format.
I have changed all Get-WmiObject into the newer and faster Get-CimInstance cmdlets below
$result = Get-Content "C:\Temp\Servers.txt" | ForEach-Object {
# create an ordered hashtable to store the results for each server
$pcinfo = [ordered]#{}
# System info
$data = Get-CimInstance -ClassName Win32_ComputerSystem -ComputerName $_
$pcinfo['Computer'] = $data.PSComputerName
$pcinfo['Memory (RAM in GB)'] = '{0:N2}' -f ($data.TotalPhysicalMemory / 1GB)
# OS info
$data = Get-CimInstance -ClassName Win32_OperatingSystem -ComputerName $_
$pcinfo['BuildNumber'] = $data.BuildNumber
$pcinfo['BuildType'] = $data.BuildType
$pcinfo['Caption'] = $data.Caption
$pcinfo['CodeSet'] = $data.CodeSet
$pcinfo['OSArchitecture'] = $data.OSArchitecture
$pcinfo['SystemDrive'] = $data.SystemDrive
$pcinfo['TotalVisibleMemorySize'] = $data.TotalVisibleMemorySize
$pcinfo['Version'] = $data.Version
# Product info (array of objects)
$pcinfo['Products'] = Get-CimInstance -ClassName Win32_Product -ComputerName $_ |
Select-Object Name, Version, Vendor, InstallDate
# Local fixed disk info (array of objects)
$pcinfo['FixedDrives'] = Get-CimInstance -ClassName Win32_LogicalDisk -ComputerName $_ -Filter 'DriveType=3' |
Sort-Object DeviceID |
Select-Object DriveType, DeviceID, VolumeName,
#{Name="Size";Expression={"{0:N2} GB" -f ($_.Size / 1GB)}},
#{Name="FreeSpace";Expression={"{0:N2} GB" -f ($_.FreeSpace / 1GB)}},
Compressed
# Services info (array of objects)
$pcinfo['Services'] = Get-CimInstance -ClassName Win32_Service -ComputerName $_ |
Where-Object { $_.DisplayName -like '*Adobe*' } |
Select-Object DisplayName, StartName, PathName, StartMode
# convert the hashtable to PSObject and output
[PsCustomObject]$pcinfo
}
# output the whole structure as JSON for easier reading and optionally save it to file
$result | ConvertTo-Json -Depth 3 # | Set-Content -Path 'Path\To\Output.json' -Force
I am very close to what I am looking for, I have all of the correct data and it is just not formatting correctly. Currently it is all in one column, and what I would like is for the headers to be in a row, and then each entry to be in its own row as well. Here is my current code:
$servers = Get-ADComputer -Filter "Name -like 'f****p*'" | Sort-Object | Select -ExpandProperty Name
$servers | foreach {
$server = $_
$serverIP = Test-Connection -ComputerName $server -Count 1 | Select -ExpandProperty IPV4Address
$serverIPString = $serverIP.IPAddresstoString
$scope = Get-DhcpServerv4Scope -ComputerName $server
$options = Get-DhcpServerv4OptionValue -ComputerName $server
New-Object -TypeName psobject -Property #{
ServerName = $server
ServerIP = $serverIPString
ScopeName = $scope.Name | Out-String
StartIP = $scope.StartRange.IPAddressToString | Out-String
EndIP = $scope.EndRange.IPAddressToString | Out-String
SubnetMask = $scope.SubnetMask.IPAddressToString | Out-String
Duration = $scope.LeaseDuration.Days | Out-String
OptionName = $options.Name | Out-String
OptionID = $options.OptionID | Out-String
OptionValue = $options.Value | Out-String
}
} | Select-Object ServerName, ServerIP, ScopeName, StartIP, EndIP, SubnetMask, Duration, OptionName, OptionID, OptionValue | Out-File C:\temp\FPScopes.csv
Currently the output looks like this example (using Duration and Options as an example, as it has the most generic data to share):
What I am looking for would be to have each object type as a column header, and have the entries in a separate row underneath the header:
Etc....
Any assistance would be greatly appreciated.
Thank you! :)
Ry
My final code, after help from this forum:
Add-Content -Value "ServerName,ServerIP,ScopeName,StartRange,EndRange,SubnetMask,Duration,OptionName,OptionID,Value" -Path c:\temp\POSScopes.csv
Get-ADComputer -Filter {Name -like 'p****p01' -or Name -like 'p****p02' -or Name -like 'q****001' -or Name -like 'q****002'} | Sort-Object | Select -ExpandProperty Name | Out-File C:\Temp\POSServers.txt
$servers = Get-Content C:\Temp\POSServers.txt
$servers | foreach {
$server = $_
$serverIP = Test-Connection -ComputerName $server -Count 1 | Select -ExpandProperty IPV4Address
$serverIPString = $serverIP.IPAddresstoString
$scope = Get-DhcpServerv4Scope -ComputerName $server
$options = Get-DhcpServerv4OptionValue -ComputerName $server
New-Object -TypeName psobject -Property #{
ServerName = $server
ServerIP = $serverIPString
ScopeName = $scope.Name | Out-String
StartIP = $scope.StartRange.IPAddressToString | Out-String
EndIP = $scope.EndRange.IPAddressToString | Out-String
SubnetMask = $scope.SubnetMask.IPAddressToString | Out-String
Duration = $scope.LeaseDuration.Days | Out-String
OptionName = $options.Name | Out-String
OptionID = $options.OptionID | Out-String
OptionValue = $options.Value | Out-String
}
for ($i = ($Options.Count -1); $i -gt -1; $i--) {
Add-Content -Value "$($server),$($serverIPString),$($scope.Name),$($scope.StartRange.IPAddressToString),$($scope.EndRange.IPAddressToString),$($scope.SubnetMask.IPAddressToString),$($scope.LeaseDuration.Days),$($options[$i].Name),$($options[$i].OptionID),$($Options[$i].Value)" -Path C:\temp\POSScopes.csv
}
}
Add-Content -Value "ServerName,ServerIP,ScopeName,StartRange,EndRange,SubnetMask,Duration,OptionName,OptionID,Value" -Path c:\temp\POSScopes.csv
Get-ADComputer -Filter {Name -like 'p****p01' -or Name -like 'p****p02' -or Name -like 'q****001' -or Name -like 'q****002'} | Sort-Object | Select -ExpandProperty Name | Out-File C:\Temp\POSServers.txt
$servers = Get-Content C:\Temp\POSServers.txt
$servers | foreach {
$server = $_
$serverIP = Test-Connection -ComputerName $server -Count 1 | Select -ExpandProperty IPV4Address
$serverIPString = $serverIP.IPAddresstoString
$scope = Get-DhcpServerv4Scope -ComputerName $server
$options = Get-DhcpServerv4OptionValue -ComputerName $server
New-Object -TypeName psobject -Property #{
ServerName = $server
ServerIP = $serverIPString
ScopeName = $scope.Name | Out-String
StartIP = $scope.StartRange.IPAddressToString | Out-String
EndIP = $scope.EndRange.IPAddressToString | Out-String
SubnetMask = $scope.SubnetMask.IPAddressToString | Out-String
Duration = $scope.LeaseDuration.Days | Out-String
OptionName = $options.Name | Out-String
OptionID = $options.OptionID | Out-String
OptionValue = $options.Value | Out-String
}
for ($i = ($Options.Count -1); $i -gt -1; $i--) {
Add-Content -Value "$($server),$($serverIPString),$($scope.Name),$($scope.StartRange.IPAddressToString),$($scope.EndRange.IPAddressToString),$($scope.SubnetMask.IPAddressToString),$($scope.LeaseDuration.Days),$($options[$i].Name),$($options[$i].OptionID),$($Options[$i].Value)" -Path C:\temp\POSScopes.csv
}
}
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 .
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
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.
Objective: How to extract server information?
For each server name listed in servers.txt, I would like to get the following information (in this format):
Server name, IP Address, OS name, Total Physical Memory, Processors, each drive letter and size, System Model
Comma separated and new line for each server.
Below is my PowerShell code. Can your guys give a hint on why this does not work? Also why I get an error with New-Object statement?
foreach ($ComputerName in (Get-Content -Path .\servers.txt)) {
$HashProps = #{
'tHostname' = Get-WmiObject Win32_Computersystem -ComputerName $ComputerName | Select-Object -ExpandProperty Name
'tIP' = [System.Net.Dns]::GetHostAddresses($computername)
'tOS' = Get-WmiObject -ComputerName $ComputerName -Class Win32_OperatingSystem | Select-Object -ExpandProperty Caption
'tMemory' = Get-WmiObject Win32_PhysicalMemory | Measure-Object -Property capacity -Sum | foreach { "$("{0:n2}" -f ( $_.Sum/1GB ) )" }
'tcpu' = Get-WmiObject Win32_processor | Select-Object name, numberofcores
'tDisks' = Get-WmiObject Win32_LogicalDisk | foreach { "$($_.DeviceID) $("{0:n2}" -f ( $_.Size/ 1GB ) )" }
'tsysmodel' = Get-Wmiobject Win32_computersystem | Select-Object model
}
New-Object -TypeName psObject -Property $HashProps |
ConvertTo-Csv -NoTypeInformation | Out-File -Append .\output.csv
}
I am open for a other approach, if this is easier.
Have you verified that each of those lines actually return what you want?
I just threw this into the ISE and it works fine:
$f = gwmi win32_computersystem | select name,model,totalphysicalmemory
$hash = #{
'name' = $f.name
'model' = $f.model
'memory' = $("{0:n2}" -f ( $f.totalphysicalmemory/1GB ) )
}
New-Object -TypeName psobject -Property $hash | ConvertTo-Csv -NoTypeInformation | Out-File -Append .\test.csv
Also, if you want the properties to appear in a specific order in the CSV, it will take some additional magic, otherwise they're put in alphabetically.
A little bit pimped, maybe this will help you:
$Servers = Foreach ($ComputerName in (Get-Content -Path .\Servers.txt)) {
$CS = Get-WmiObject Win32_ComputerSystem -ComputerName $ComputerName
$OS = Get-WmiObject Win32_OperatingSystem -ComputerName $ComputerName
$PM = Get-WmiObject Win32_PhysicalMemory -ComputerName $ComputerName
$PR = Get-WmiObject Win32_processor -ComputerName $ComputerName
$LD = Get-WmiObject Win32_LogicalDisk -ComputerName $ComputerName
$IP = [System.Net.Dns]::GetHostAddresses($ComputerName)
[PSCustomObject]#{
ServerName = $CS | Select-Object -ExpandProperty Name
IPAddress = $IP | Select-Object -ExpandProperty IPAddressToString
OS = $OS | Select-Object -ExpandProperty Caption
Memory = $PM | Measure-Object -Property Capacity -Sum | foreach { "$("{0:n2}" -f ( $_.Sum/1GB ) )" }
CPU = $PR | Select-Object Name, NumberOfCores
Disks = $LD | foreach { "$($_.DeviceID) $("{0:n2}" -f ( $_.Size/ 1GB ) )" }
Model = $CS | Select-Object -ExpandProperty Model
}
}
$File = Join-Path $env:TEMP 'Ouptut.csv'
$Servers | Export-Csv -Path $File -NoTypeInformation -Delimiter ';'
Start-Process $File
I've got an script and I want to remove the white spaces that powershell puts by default in the output result. Is there any way of doing it?
=======Computer1=======
Microsoft Windows 8.1 Pro
Name : Computer1
Model : Vostro 200
Manufacturer : Dell Inc.
SerialNumber : 012345
This is what I want:
=======Computer1=======
Microsoft Windows 8.1 Pro
Name : Computer1
Model : Vostro 200
Manufacturer : Dell Inc.
SerialNumber : 012345
This is my script:
$Computers=Import-Csv C:\Powershell\test.csv
$ResultsPath="C:\Powershell\test.txt"
foreach ($i in $Computers.Name) {
"="*7 + $i + "="*7
if (Test-Connection $i -quiet) {
(Get-WmiObject -class Win32_OperatingSystem -ComputerName $i).Caption
Get-WmiObject -class Win32_Computersystem -ComputerName $i | Select-Object Name, Model, Manufacturer | Format-List
Get-WmiObject win32_SystemEnclosure -ComputerName $i | Select-Object SerialNumber | Format-List }
else { "nothing" }
}
While Trim will do what you need, this is not a PowerShell way. Here is revised script, that works with objects internally and writes output the way you want.
$Computers = Import-Csv 'C:\Powershell\test.csv'
$ResultsPath = 'C:\Powershell\test.txt'
foreach ($i in $Computers.Name) {
$Header = '='*7 + $i + '='*7
Write-Output $Header
if (Test-Connection $i -quiet)
{
$Os = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $i | Select-Object -ExpandProperty Caption
$Info = Get-WmiObject -Class Win32_Computersystem -ComputerName $i | Select-Object Name, Model, Manufacturer
$Sn = Get-WmiObject -Class Win32_SystemEnclosure -ComputerName $i | Select-Object -ExpandProperty SerialNumber
$PC = New-Object -TypeName psobject -Property #{
OperatingSystem = $Os
Name = $Info.Name
Model = $Info.Model
Manufacturer = $Info.Manufacturer
SerialNumber = $Sn
} | Select-Object OperatingSystem, Name, Model, Manufacturer, SerialNumber
Write-Output ($PC | Format-List | Out-String).Trim()
}
else
{
Write-Output 'nothing'
}
}
Convert output to string and trim it:
"="*7 + $i + "="*7
if (Test-Connection $i -quiet) {
(Get-WmiObject -class Win32_OperatingSystem -ComputerName $i).Caption
(Get-WmiObject -class Win32_Computersystem -ComputerName $i | Select-Object Name, Model, Manufacturer | Format-List | Out-String).Trim()
(Get-WmiObject win32_SystemEnclosure -ComputerName $i | Select-Object SerialNumber | Format-List | Out-String).Trim() }
else { "nothing" }