?
Solved

Dump Output of a script to Html

Posted on 2013-12-27
22
Medium Priority
?
286 Views
Last Modified: 2013-12-31
I have the  command below that retrieves information about services and dump the output into HTML format. However the format of the HTML is cumbersome. I need a clean one, with columns well spaced, etc.... the better way it looks is the preferred way.

Thank you


Get-WmiObject win32_service | select-object DisplayName, State, Startmode | sort startmode | ConvertTo-HTML | Out-File C:\Test.htm


services
0
Comment
Question by:jskfan
[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
  • 14
  • 8
22 Comments
 
LVL 70

Expert Comment

by:Qlemo
ID: 39742870
That look is a result of the default CSS used, which displays column headers (<th>) in bold and centered. The proper way is to supply a different CSS, but you can also use direct formatting instead by replacing some of the created tags:
Get-WmiObject win32_service |
  select-object DisplayName, State, Startmode |
  sort startmode |
  ConvertTo-HTML |
  % { $_.Replace('<th>', '<td><strong>').Replace('</th>', '</strong></td>') } |
  Out-File C:\Test.htm

Open in new window

The column width is already correct, as that width is needed to display the largest text contained in the column.
0
 

Author Comment

by:jskfan
ID: 39742878
$a = "<style>"
$a = $a + "BODY{background-color:peachpuff;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"
$a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"
$a = $a + "</style>"

Get-WmiObject win32_service | select-object DisplayName, State, Startmode | sort startmode | ConvertTo-HTML -head $a| Out-File C:\Test.htm

Invoke-Expression C:\Test.htm
0
 

Author Comment

by:jskfan
ID: 39742888
the script above worked fine....the problem there are some services that are set to start automatic, but with Delayed start.....
The script will show them Stopped as status (which is True), but does not show Delayed Start

how can I make it show that?

Thanks
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 70

Expert Comment

by:Qlemo
ID: 39742896
"the script above" refers to http:#a39742870 (mine) or http:#39742878 (yours)? Because I would tell your approach "pretty" in any way ;-).
0
 

Author Comment

by:jskfan
ID: 39742900
I want the script to show Delayed Start
0
 

Author Comment

by:jskfan
ID: 39742908
I also need computer name to be displayed on Top
0
 

Author Comment

by:jskfan
ID: 39742926
I can write computer name on the script , but I do not know how to display in HTML

Get-WmiObject win32_service -computername Mycomputer01, etc,,,,,,
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39742933
Well, getting the computer name on top isn't difficult:
Get-WmiObject win32_service |
  select-object DisplayName, State, Startmode |
  sort startmode |
  ConvertTo-HTML -Fragment -PreContent "<h1>$env:ComputerName</h1>" |
  % { $_ -Replace '<th>', '<td><strong>' -Replace '</th>', '</strong></td>'  } |
  Out-File C:\Test.htm

Open in new window

Getting "Automatic (Delayed)" can't be done with WMI or the Get-Service cmdlet, sadly. We have to read the registry.
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39742953
This is one approach adding a "Delayed" column:
Get-WmiObject win32_service |
  % {
    Add-Member -PassThru -InputObject $_ NoteProperty Delayed $(
      if ((Get-ItemProperty "HKLM:System\CurrentControlSet\Services\$($_.Name)").DelayedAutoStart -eq 1) { "x" }
    )
  } | 
  select-object DisplayName, State, Startmode, Delayed |
  sort startmode, DisplayName |
  ConvertTo-HTML -Fragment -PreContent "<h1>$env:ComputerName</h1>" |
  % { $_ -Replace '<th>', '<td><strong>' -Replace '</th>', '</strong></td>'  } |
  Out-File C:\Test.htm

Open in new window

0
 

Author Comment

by:jskfan
ID: 39742954
That's what I found out ...what about the computer name, if you can stick it in HTML
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39742958
It's there, in big letters.
0
 

Author Comment

by:jskfan
ID: 39742970
Yes please...at the top of the HTML page
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39742974
Again: Look at my code. -PreContent "<h1>$env:ComputerName</h1>" is doing that.
0
 

Author Comment

by:jskfan
ID: 39742994
Excellent, if you can put borders around columns and rows (like Excel) will be perfect.

Thanks
0
 
LVL 70

Accepted Solution

by:
Qlemo earned 2000 total points
ID: 39743374
I'm not experienced with HTML styling, so I'll take a minimal approach. Feel free to modify the "style sheet":
$style = @"
  <style>
    TABLE {border-collapse: collapse}
    TD {border-width: 1px;padding: 5px;border-style: dotted;border-color: black}
  </style>
"@

Get-WmiObject win32_service |
  % {
    Add-Member -PassThru -InputObject $_ NoteProperty Delayed $(
      if ((Get-ItemProperty "HKLM:System\CurrentControlSet\Services\$($_.Name)").DelayedAutoStart -eq 1) { "x" }
    )
  } | 
  select-object DisplayName, State, Startmode, Delayed |
  sort startmode, DisplayName |
  ConvertTo-HTML  -PreContent "<h1>$env:ComputerName</h1>$style" |
  % { $_ -Replace '<th>', '<td><strong>' -Replace '</th>', '</strong></td>'  } |
  Out-File C:\Test.html

Open in new window

0
 

Author Comment

by:jskfan
ID: 39746758
your script worked great when I run it for the local computer, but when I run it against remote computer, I get errors
0
 

Author Comment

by:jskfan
ID: 39746835
oops I guess I see it now
0
 

Author Comment

by:jskfan
ID: 39746839
Sorry I cannot guess it...
when I run it for the local computer, but when I run it against remote computer, I get errors
0
 

Author Comment

by:jskfan
ID: 39746859
I am running this script, when I add Get-WmiObject win32_service -computername Remotecomputername

I get errors

$style = @"
  <style>
    TABLE {border-collapse: collapse}
    TD {border-width: 1px;padding: 5px;border-style: dotted;border-color: black}
  </style>
"@

Get-WmiObject win32_service |
  % {
    Add-Member -PassThru -InputObject $_ NoteProperty Delayed $(
      if ((Get-ItemProperty "HKLM:System\CurrentControlSet\Services\$($_.Name)").DelayedAutoStart -eq 1) { "x" }
    )
  } |
  select-object DisplayName, State, Startmode, Delayed |
  sort startmode, DisplayName |
  ConvertTo-HTML  -PreContent "<h1>$env:computername</h1>$style" |
  % { $_ -Replace '<th>', '<td><strong>' -Replace '</th>', '</strong></td>'  } |
  Out-File C:\Test.html
0
 
LVL 70

Expert Comment

by:Qlemo
ID: 39747767
Of course it does not work remote. It is set up to access the local registry, and inserts the local computername as title.
Preparing a script to run against another PC remotely is always a different beast. If you have such requirement, please state it from start next time, as we (Experts) often try to give the most simple solution.
0
 

Author Comment

by:jskfan
ID: 39748603
Qlemo

No problem, I will post a new question about that. Thanks for your help
0
 

Author Closing Comment

by:jskfan
ID: 39748612
Excellent!
0

Featured Post

Bringing Advanced Authentication to the SMB Market

WatchGuard announces the acquisition of advanced authentication provider, Datablink, with one mission – to bring secure authentication to SMB, mid-market, and distributed enterprises with a cloud-based solution, ideal for resale via their established channel & MSSP community.

Question has a verified solution.

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

In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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…
Suggested Courses

764 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