Link to home
Start Free TrialLog in
Avatar of Brian
BrianFlag for United States of America

asked on

Export PowerShell Results to HTML

Hello Experts,

I have the following PowerShell Script that I received help with. I need to be able to convert the results to and HTML, CSV and Txt file. I need the results for each format. Please see my PowerShell below.

$bios = Get-WmiObject -Class Win32_BIOS
$cpu  = Get-WmiObject -Class Win32_Processor
$os   = Get-WmiObject -Class Win32_OperatingSystem
$sys  = Get-WmiObject -Class Win32_ComputerSystem
$key  = Get-WmiObject -Class SoftwareLicensingService

$sysProperties = [ordered]@{
	'-- PC --' = ''

        'Domain' = $sys.domain
	'PC Name' = $os.pscomputername
	'User Name' = $sys.UserName

	'-- Model --' = ''

	'Manufacturer' = $sys.manufacturer
	'Model' = $sys.model
	'Serial Number' = $bios.serialnumber
	'Product Number' = $sys.SystemSKUNumber

	'-- Hardware --' = ''

	'Processor' = $cpu.Name
	'Installed RAM' = $sys.totalphysicalmemory / 1GB -as [int]

	'-- OS --' = ''

	'Edition' = $os.caption
	'OS System Type' = $os.osarchitecture
	'OS Build' = $os.buildnumber
        'Product Key' =$key.OA3xOriginalProductKey;
}
$Separator = '-' * ($sysProperties.Values | Measure-Object -Property Length -Maximum).Maximum
@($sysProperties.Keys) | Where-Object {$_ -like '--*'} | ForEach-Object {$sysProperties[$_] = $Separator}
$sysProperties

Open in new window

SOLUTION
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Brian

ASKER

I get an error when I add that after $sysProperties. The error I get is below:

ConvertTo-HTML : The term 'ConvertTo-HTML' is not recognized as the name of a cmdlet, function, script file, or operable program.
Avatar of Brian

ASKER

Below is how I added what you suggested:

$bios = Get-WmiObject -Class Win32_BIOS
$cpu  = Get-WmiObject -Class Win32_Processor
$os   = Get-WmiObject -Class Win32_OperatingSystem
$sys  = Get-WmiObject -Class Win32_ComputerSystem
$key  = Get-WmiObject -Class SoftwareLicensingService

$sysProperties = [ordered]@{
	'-- PC --' = ''

        'Domain' = $sys.domain
	'PC Name' = $os.pscomputername
	'User Name' = $sys.UserName

	'-- Model --' = ''

	'Manufacturer' = $sys.manufacturer
	'Model' = $sys.model
	'Serial Number' = $bios.serialnumber
	'Product Number' = $sys.SystemSKUNumber

	'-- Hardware --' = ''

	'Processor' = $cpu.Name
	'Installed RAM' = $sys.totalphysicalmemory / 1GB -as [int]

	'-- OS --' = ''

	'Edition' = $os.caption
	'OS System Type' = $os.osarchitecture
	'OS Build' = $os.buildnumber
        'Product Key' =$key.OA3xOriginalProductKey;
}
$Separator = '-' * ($sysProperties.Values | Measure-Object -Property Length -Maximum).Maximum
@($sysProperties.Keys) | Where-Object {$_ -like '--*'} | ForEach-Object {$sysProperties[$_] = $Separator}
$sysProperties | ConvertTo-HMTL | Out-File C:\Temp\Results.html

Open in new window

Avatar of Brian

ASKER

I was able to get the PowerShell cmd to create the .html file. But the results are not correct. Please see below:

Count
IsReadOnly
Keys
Values
IsFixedSize
SyncRoot
IsSynchronized
17
False
System.Collections.Specialized.OrderedDictionary+OrderedDictionaryKeyValueCollection
System.Collections.Specialized.OrderedDictionary+OrderedDictionaryKeyValueCollection
False
System.Object
False
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Brian

ASKER

@footech,

Thank you for the replying. However, when I run that in PS I get the following error. Please see the screenshot that I attached.

User generated image
Odd, works fine in the ISE, but pasting the entire thing in the console gives the error.
This works with the commented out portions deleted.
$bios = Get-WmiObject -Class Win32_BIOS
$cpu  = Get-WmiObject -Class Win32_Processor
$os   = Get-WmiObject -Class Win32_OperatingSystem
$sys  = Get-WmiObject -Class Win32_ComputerSystem
$key  = Get-WmiObject -Class SoftwareLicensingService

$sysProperties = [pscustomobject][ordered]@{

    'Domain' = $sys.domain
    'PC Name' = $os.pscomputername
    'User Name' = $sys.UserName

    'Manufacturer' = $sys.manufacturer
    'Model' = $sys.model
    'Serial Number' = $bios.serialnumber
    'Product Number' = $sys.SystemSKUNumber

    'Processor' = $cpu.Name
    'Installed RAM' = $sys.totalphysicalmemory / 1GB -as [int]

    'Edition' = $os.caption
    'OS System Type' = $os.osarchitecture
    'OS Build' = $os.buildnumber
    'Product Key' =$key.OA3xOriginalProductKey;
}
$sysProperties

Open in new window

Avatar of Brian

ASKER

If I copy and paste the following code above in PS Command and PS ISE it runs fine. But if I run the code in Post ID: 42232878 were we are trying to export the results to .txt or .html I get that error that I screenshot.
With the last code in #a42233464 it still creates the $sysProperties object, from which you can then pipe to either of the following.
$sysProperties | ConvertTo-HMTL | Out-File C:\Temp\Results.html -encoding ascii
$sysProperties | Export-CSV C:\Temp\Results.csv

Open in new window

Avatar of Brian

ASKER

@footech, thank you. It worked... The only odd thing though is when I export to .csv it add this #TYPE System.Management.Automation.PSCustomObject to the first row and column of the csv file. Everything below that is fine though.
You can add the -NoTypeInformation parameter to Export-CSV to avoid that.
Avatar of Brian

ASKER

Thank you very much for all your help!!!