Solved

Powershell Output to HTML

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
A procedure for exporting installed hotfix details of remote computers using powershell
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

789 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