Solved

Powershell Output to HTML

Posted on 2014-07-31
4
206 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
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…

738 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