Solved

Powershell Output to HTML

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

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 68

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Synchronize a new Active Directory domain with an existing Office 365 tenant
Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now