We help IT Professionals succeed at work.

output in HTML in Powershell

Suresh Kumar
Suresh Kumar asked
on
288 Views
Last Modified: 2017-04-24
[cmdletbinding()]
param
(
[Parameter(Position=0,ValuefromPipeline=$true)]
[string[]$ComputerName = $env:COMPUTERNAME
)
cluster node /status

If ($? -eq "True") {

$cluster = cluster node /status

$a = @'
<style type='text/css'> .nxn-ret-table{width:100%; border-collapse:collapse; font-family: Arial;} /* Define the background color for all the ODD background rows */ .nxn-ret-table tr:nth-child(odd){ background: #DAE0E1;} /* Define the background color for all the EVEN background rows */ .nxn-ret-table tr:nth-child(even){background: #f7f7f7; } pre { font-family: Arial; overflow: auto;} </style>

'@

$cluster | ConvertTo-Html -As List -Head $a -body "<H2>Cluster Status</H2>" | Out-File -FilePath C:\tmp\user.txt
Get-Content 'C:\tmp\user.txt' | Foreach-Object {$_ -replace '<table>', ("<table class='nxn-ret-table'>")} | Foreach-Object {$_ -replace '^<html.*$', ("<html>")} | Set-Content 'C:\tmp\user1.txt'

cat C:\tmp\user1.txt
}
else {Write-Host "<html><h1>Cluster is not configured on $ComputerName</h1></html>"}

when i run the above script .,output comes like

Node           Node ID Status

-------------- ------- ---------------------

WSOMQAVDCI02         1 Up

wsomqavdci01         2 Up

But when trying to get the output in HTML.it does not work.

Can you please help in getting the out in HTML format
Comment
Watch Question

PowerShell Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
This problem has been solved!
(Unlock this solution with a 7-day Free Trial)
UNLOCK SOLUTION
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
Try it like this:
[CmdletBinding()]
Param(
	[Parameter(Position=0,ValuefromPipeline=$true)]
	[string[]]$ComputerName = $env:COMPUTERNAME
)
$Head = @'
<style type='text/css'> .nxn-ret-table{width:100%; border-collapse:collapse; font-family: Arial;} /* Define the background color for all the ODD background rows */ .nxn-ret-table tr:nth-child(odd){ background: #DAE0E1;} /* Define the background color for all the EVEN background rows */ .nxn-ret-table tr:nth-child(even){background: #f7f7f7; } pre { font-family: Arial; overflow: auto;} </style>

'@
Try {
	Import-Module FailoverClusters -ErrorAction Stop
	Get-ClusterNode -ErrorAction Stop |
		Select-Object Name, ID, State |
		ConvertTo-Html -As List -Head $Head -body "<H2>Cluster Status</H2>" |
		Foreach-Object {$_ -replace '<table>', ("<table class='nxn-ret-table'>")} |
		Foreach-Object {$_ -replace '^<html.*$', ("<html>")} |
		Set-Content 'C:\tmp\user1.txt'
		Get-Content -Path 'C:\tmp\user1.txt'
} Catch {
	Write-Host "<html><h1>Cluster is not configured on $ComputerName</h1></html>"
}

Open in new window

Chris DentPowerShell Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
It'll need a handler for a remote call to make sense of the ComputerName parameter. The second replace is weird and can probably just go.
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018

Commented:
The remote support wasn't used in the original (and "ValueFromPipeline=$tTrue" is rather pointless as well, since there's no Process {} block.), so I think that's just a "copy and paste" from a script template.
It would need the cluster name in this case, though ("Get-ClusterNode -Cluster <ClusterName> -ErrorAction Stop"), not a node name.
The second "replace" removes the XML namespace that ConvertTo-Html adds to the <html> tag (<html xmlns="http://www.w3.org/1999/xhtml">).
Chris DentPowerShell Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
I stand by "weird" ;)

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions