Avatar of jskfan
jskfanFlag for Cyprus asked on

Dump Output of a script to Html

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
Scripting LanguagesVB ScriptPowershell

Avatar of undefined
Last Comment
jskfan

8/22/2022 - Mon
Qlemo

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.
ASKER
jskfan

$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
ASKER
jskfan

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
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Qlemo

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

I want the script to show Delayed Start
ASKER
jskfan

I also need computer name to be displayed on Top
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
ASKER
jskfan

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,,,,,,
Qlemo

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.
Qlemo

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

This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
ASKER
jskfan

That's what I found out ...what about the computer name, if you can stick it in HTML
Qlemo

It's there, in big letters.
ASKER
jskfan

Yes please...at the top of the HTML page
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Qlemo

Again: Look at my code. -PreContent "<h1>$env:ComputerName</h1>" is doing that.
ASKER
jskfan

Excellent, if you can put borders around columns and rows (like Excel) will be perfect.

Thanks
ASKER CERTIFIED SOLUTION
Qlemo

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
ASKER
jskfan

your script worked great when I run it for the local computer, but when I run it against remote computer, I get errors
Your help has saved me hundreds of hours of internet surfing.
fblack61
ASKER
jskfan

oops I guess I see it now
ASKER
jskfan

Sorry I cannot guess it...
when I run it for the local computer, but when I run it against remote computer, I get errors
ASKER
jskfan

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
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Qlemo

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.
ASKER
jskfan

Qlemo

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

Excellent!
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy