Solved

Powershell Output to HTML

Posted on 2014-07-31
4
213 Views
Last Modified: 2014-07-31
Hi,

I have a script that gives me on screen some output. It works great.
I would like to have this output to be converted to a HTML file.
Already tried some things, but I think the difficult part is the PSCustomObject.
How can this be done ?

The script:


	$ComputerName = "Computer"
   
    foreach ($Computer in $ComputerName) {
        $RegBase = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine,$Computer)
        $RegUninstall = $RegBase.OpenSubKey('SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall')
        $RegUninstall.GetSubKeyNames() | 
        ForEach-Object {
            $DisplayName = ($RegBase.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$_")).GetValue('DisplayName')
            if ($DisplayName) {
                New-Object -TypeName PSCustomObject -Property @{
                    ComputerName = $Computer
                    ProgramName = $DisplayName
					
                }
            }
        }
    } 

Open in new window


Thanks in advance.
0
Comment
Question by:Loyall
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 19

Expert Comment

by:Kash
ID: 40231660
can't you use out-file to html as below example

Get-Service | Select-Object Status, Name, DisplayName | ConvertTo-HTML | Out-File C:\Scripts\Test.htm

read through full article >>> http://technet.microsoft.com/en-us/library/ff730936.aspx

if you save your query in another variable and then point the result to out-file html then it should work
0
 
LVL 40

Accepted Solution

by:
footech earned 250 total points
ID: 40232043
You just need to pipe to ConvertTo-HTML.  In your code above, this is made just slightly more difficult with the use of the foreach statement, since that doesn't work with the pipeline.  A way around that is to surround the entire foreach statement with array notation ( @() ), and then you can pipe that to ConvertTo-HTML.  See the following pseudo-code.
@(foreach ($Computer in $ComputerName) {
 }) | ConvertTo-HTML

Open in new window


Another way is to re-write to utilitize the ForEach-Object cmdlet instead of the foreach statement, as it can use the pipepline.
0
 
LVL 70

Assisted Solution

by:Qlemo
Qlemo earned 250 total points
ID: 40232444
The script isn't difficult to rewrite to use foreach-object, so I would do that. Most simple approach:
"Computer" | % {
  $Computer = $_
  $RegBase = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine,$Computer)
  $RegUninstall = $RegBase.OpenSubKey('SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall')
  $RegUninstall.GetSubKeyNames() | % {
    $DisplayName = ($RegBase.OpenSubKey("SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\$_")).GetValue('DisplayName')
    if ($DisplayName) {
      New-Object PSCustomObject -Property @{
        ComputerName = $Computer
        ProgramName  = $DisplayName
      }
    }
  }
} | ConvertTo-HTML -fragment > File.HTML

Open in new window

Whether to use -fragment or not depends on your demands.
0
 
LVL 2

Author Closing Comment

by:Loyall
ID: 40232653
Footech and Qlemo, tried both solutions and they both do the trick.
Thanks a lot !
0

Featured Post

[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …

635 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question