Author Posts

March 21, 2018 at 6:11 am

Hi Team,
Wrote a short script. Which pull out some hardware details. I am trying to save the output text / html file with basic formatting.
Following is the script what I wrote.

#Hardware Check
##CPU
$proc=Get-CimInstance -ClassName CIM_processor |select Name, MaxclockSpeed, NumberofCores
##Memory
$mem=Get-CimInstance -ClassName CIM_physicalmemory |
select Caption,Manufacturer, @{n="Size(GB)"; e={$_.capacity /1GB}},ConfiguredClockSpeed
##HDD
$Hdd=Get-CimInstance -ClassName CIM_logicaldisk|
select DeviceID, @{n="TotalSize"; e={$_.Size /1GB -as[int]}}
##Operating System
$os=Get-CimInstance -ClassName CIM_OperatingSystem|select Caption, OSArchitecture

#Output

Write-Host "Hardware Summary:"
Write-Host CPU
$proc |FL

Write-Host Memory
$mem |fl

Write-Host HardDisk
$Hdd |fl

Write-Host operating System
$os |fl

I am trying to run it by .\myscRipt.ps1 > myOutput.txt
You can try and see what is the output style.
The issue I am facing; headings which I want to put (which is mentioned after write-host)is not redirecting to the text file. Also, there are too many extra spaces which I don't like. Following is the output I am expecting. I manually edit the contents before posting here but need this type of output using the script.

Hardware Summary:
CPU:
Name : Intel(R) Core(TM) i5-6200U CPU @ 2.30GHz
MaxclockSpeed : 2400
NumberofCores : 2

Memory:

Caption : Physical Memory
Manufacturer : Samsung
Size(GB) : 8
ConfiguredClockSpeed : 1600

Harddisk:

DeviceID : C:
TotalSize : 464

Operating System:

Caption : Microsoft Windows 10 Enterprise
OSArchitecture : 64-bit

This is the story. Need your valuable guidance to rectify the issue. Please help me to figure out what mistakes I am doing. And also suggest me what should I write into the script which saves the output directly to a file. Once I execute the file will be automatically created in some place/desktop.

Regards,
Sankha.

March 21, 2018 at 7:41 am

write-host explicitly send the data to the host.
You should NEVER use this in a script.
Just remove all write-host

replace : Write-Host "Hardware Summary:"
with : "Hardware Summary:"

and you should have what you want.
If you want some more editing, you need to do that in the script, depending of the output you want.

March 21, 2018 at 7:46 am

This might do the trick for you

$outputFile = c:\temp\output.txt
##CPU
$proc=Get-CimInstance -ClassName CIM_processor |select Name, MaxclockSpeed, NumberofCores
##Memory
$mem=Get-CimInstance -ClassName CIM_physicalmemory |
select Caption,Manufacturer, @{n="Size(GB)"; e={$_.capacity /1GB}},ConfiguredClockSpeed
##HDD
$Hdds=Get-CimInstance -ClassName CIM_logicaldisk| select DeviceID, @{n="TotalSize"; e={$_.Size /1GB -as[int]}}
##Operating System
$os=Get-CimInstance -ClassName CIM_OperatingSystem|select Caption, OSArchitecture

#Output
$outputHDD = $(foreach($hdd in $hdds) {
@"
DeviceID            : $($Hdd.DeviceID)
TotalSize(GB)       : $($Hdd.TotalSize)
"@
})

$output = @"
Hardware Summary:
CPU
Name                : $($proc.name)
MaxclockSpeed       : $($proc.MaxclockSpeed)
NumberofCores       : $($proc.NumberofCores)

MEMORY
Caption             : $($mem.Caption)
Manufacturer        : $($mem.Manufacturer)
Size(GB)            : $($mem.'Size(GB)')
ConfiguredClockSpeed: $($mem.ConfiguredClockSpeed)

HARDDISK
$outputHDD

OPERATINGSYSTEM
Operating System    : $($os.Caption)
OSArchitecture      : $($os.OSArchitecture)
"@

Out-File -InputObject $output -FilePath $outputFile

March 21, 2018 at 12:15 pm

You mentioned HTML output, I would strongly suggest that you look at the help files for ConvertTo-Html and the -Fragment parameter. This will guide down the correct path to creating output that can be formatted with CSS making your reports look more professional and easily consumed.

Start here:

Help ConvertTo-Html -Full 

March 26, 2018 at 2:59 pm

Great, Thanks Man, So easy.. Actually I never tried without the write host. Thanks a lot. You made my day.