Solved

Powershell Output to HTML

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

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

Is Your AD Toolbox Looking More Like a Toybox?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

A brief introduction to what I consider to be the best editor for PowerShell.
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
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…

756 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