Link to home
Create AccountLog in
Avatar of Suresh Kumar
Suresh Kumar

asked on

Output to HTML in powershell

[cmdletbinding()]
param
(
    [Parameter(Position=0,ValuefromPipeline=$true)]
    [string[]$ComputerName = $env:ComputerName
 
)
 
$vers=Invoke-Command -ComputerName $ComputerName  -ScriptBlock { opcagt -version }
$a = "<style>"
$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 + "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>"
$vers| ConvertTo-html -Head $a -body "<H2>Version </H2>" | Out-file -Filepath C:\tmp\ver.txt
echo $ComputerName ,$vers
invoke-Expression c:\tmp\ver.txt
$vers | ConvertTo-html -Head $a -body "<H2>Version </H2>" | Out-file -Filepath C:\tmp\ver.txt
Get-Content 'C:\tmp\ver.txt' | Foreach-Object {$_ -replace '^<html.*$', ("<html>")}| Set-Content 'C:\tmp\ver1.txt'
cat C:\tmp\ver1.txt


This script works fine in the remote computer to find the OV version .but when tried to get the output in HTML..it gives me a different output like PSCcomputerName,RunspaceId,PSShow ComputerName,Length

Can some one help me on this
Avatar of Tahir Qureshi
Tahir Qureshi
Flag of Australia image

try this

$report = $reportpath 
 
Clear-Content $report  
Add-Content $report "<html>"  
Add-Content $report "<head>"  
Add-Content $report "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>"  
Add-Content $report '<title>Exchange Status Report</title>'  
add-content $report '<STYLE TYPE="text/css">'  
add-content $report  "<!--"  
add-content $report  "td {"  
add-content $report  "font-family: Tahoma;"  
add-content $report  "font-size: 11px;"  
add-content $report  "border-top: 1px solid #999999;"  
add-content $report  "border-right: 1px solid #999999;"  
add-content $report  "border-bottom: 1px solid #999999;"  
add-content $report  "border-left: 1px solid #999999;"  
add-content $report  "padding-top: 0px;"  
add-content $report  "padding-right: 0px;"  
add-content $report  "padding-bottom: 0px;"  
add-content $report  "padding-left: 0px;"  
add-content $report  "}"  
add-content $report  "body {"  
add-content $report  "margin-left: 5px;"  
add-content $report  "margin-top: 5px;"  
add-content $report  "margin-right: 0px;"  
add-content $report  "margin-bottom: 10px;"  
add-content $report  ""  
add-content $report  "table {"  
add-content $report  "border: thin solid #000000;"  
add-content $report  "}"  
add-content $report  "-->"  
add-content $report  "</style>"  
Add-Content $report "</head>"  
Add-Content $report "<body>"  
add-content $report  "<table width='100%'>"  
add-content $report  "<tr bgcolor='Lavender'>"  
add-content $report  "<td colspan='7' height='25' align='center'>"  
add-content $report  "<font face='tahoma' color='#003399' size='4'><strong>Active Directory Health Check</strong></font>"  
add-content $report  "</td>"  
add-content $report  "</tr>"  
add-content $report  "</table>"  
  
add-content $report  "<table width='100%'>"  
Add-Content $report  "<tr bgcolor='IndianRed'>"  
Add-Content $report  "<td width='5%' align='center'><B>Identity</B></td>"  
Add-Content $report  "<td width='10%' align='center'><B>PingSTatus</B></td>"  
Add-Content $report  "<td width='10%' align='center'><B>NetlogonService</B></td>"  
Add-Content $report  "<td width='10%' align='center'><B>NTDSService</B></td>"  
Add-Content $report  "<td width='10%' align='center'><B>DNSServiceStatus</B></td>"  
Add-Content $report  "<td width='10%' align='center'><B>NetlogonsTest</B></td>" 
Add-Content $report  "<td width='10%' align='center'><B>ReplicationTest</B></td>" 
Add-Content $report  "<td width='10%' align='center'><B>ServicesTest</B></td>" 
  
Add-Content $report "</tr>"  


# example

  
################Ping Test###### 
 
foreach ($DC in $DCServers){ 
$Identity = $DC 
                Add-Content $report "<tr>" 
if ( Test-Connection -ComputerName $DC -Count 1 -ErrorAction SilentlyContinue ) { 
Write-Host $DC `t $DC `t Ping Success -ForegroundColor Green 
  
        Add-Content $report "<td bgcolor= 'GainsBoro' align=center>  <B> $Identity</B></td>"  
                Add-Content $report "<td bgcolor= 'Aquamarine' align=center>  <B>Success</B></td>"  
 
                ##############Netlogon Service Status################ 
        $serviceStatus = start-job -scriptblock {get-service -ComputerName $($args[0]) -Name "Netlogon" -ErrorAction SilentlyContinue} -ArgumentList $DC 
                wait-job $serviceStatus -timeout $timeout 
                if($serviceStatus.state -like "Running") 
                { 
                 Write-Host $DC `t Netlogon Service TimeOut -ForegroundColor Yellow 
                 Add-Content $report "<td bgcolor= 'Yellow' align=center><B>NetlogonTimeout</B></td>" 
                 stop-job $serviceStatus 
                } 
                else 
                { 
                $serviceStatus1 = Receive-job $serviceStatus 
                 if ($serviceStatus1.status -eq "Running") { 
            Write-Host $DC `t $serviceStatus1.name `t $serviceStatus1.status -ForegroundColor Green  
                $svcName = $serviceStatus1.name  
                $svcState = $serviceStatus1.status           
                Add-Content $report "<td bgcolor= 'Aquamarine' align=center><B>$svcState</B></td>"  
                  } 
                 else  
                  {  
                 Write-Host $DC `t $serviceStatus1.name `t $serviceStatus1.status -ForegroundColor Red  
               $svcName = $serviceStatus1.name  
               $svcState = $serviceStatus1.status           
               Add-Content $report "<td bgcolor= 'Red' align=center><B>$svcState</B></td>"  
                  }  
                } 

}  
else 
              { 
Write-Host $DC `t $DC `t Ping Fail -ForegroundColor Red 
        Add-Content $report "<td bgcolor= 'GainsBoro' align=center>  <B> $Identity</B></td>"  
                Add-Content $report "<td bgcolor= 'Red' align=center>  <B>Ping Fail</B></td>"  
        Add-Content $report "<td bgcolor= 'Red' align=center>  <B>Ping Fail</B></td>"  
        Add-Content $report "<td bgcolor= 'Red' align=center>  <B>Ping Fail</B></td>"  
        Add-Content $report "<td bgcolor= 'Red' align=center>  <B>Ping Fail</B></td>"  
        Add-Content $report "<td bgcolor= 'Red' align=center>  <B>Ping Fail</B></td>" 
        Add-Content $report "<td bgcolor= 'Red' align=center>  <B>Ping Fail</B></td>" 
        Add-Content $report "<td bgcolor= 'Red' align=center>  <B>Ping Fail</B></td>" 
}          
        
}  


Add-Content $report "</tr>" 
############################################Close HTMl Tables########################### 
 
 
Add-content $report  "</table>"  
Add-Content $report "</body>"  
Add-Content $report "</html>"

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of footech
footech
Flag of United States of America image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of Suresh Kumar
Suresh Kumar

ASKER

Thanks ..it worked fine..
what in case if we want the Computer name in the output

I get the ouput like
version
11.14.014

I want something like
Verison
Computer Name
11.14.014

want the computername in the ouput
can you help me with this