Get powershell output of mentioned script in HTML

I have found a very good script here made by our own EE friend Krzysztof Pytko in below link, this gives me all the information I need however I need the output in html file under tabular format.

Can you guys help me with it.
LVL 18
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

The script is built to output its data in a report format, making it more easily human readable.  It's got a lot (347) of Write-Host commands, and those all only output to the screen (you can't redirect them to a file or anything).  So to try to refactor this to output into HTML tables would be a lot of work - not only changing all the Write-Host commands, but adding a number of others to combine data into arrays, convert to HTML, etc.  I'm not sure how you would even combine all the output in a tabular format, given what the output is like now.  At the very least you would have to provide sample output from the script as is, then reformat it as you want it to appear.

In other words, I think it's too much to ask here.  Perhaps in Gigs.
Life1430Author Commented:
Hey Footech,

Thank you for your time here, I understand it is lot of work can you or anyone help me with a script that would fetch below details in HTML tabular format. I have deleted most of the extra information from the script output however below would I need specifically,

Forest name
Active Directory schema version
Microsoft Exchange version
Forest Functional Level
Domains in this forest
IP address (v4)    :  
IP address (v6)    :  (none)
OS type            :  
SYSVOL replication :  DFS-R
Schema master
Domain Naming master
PDC Emulator master
RID master
Infrastructure master
DNS domain name
NetBIOS domain name
Domain Functional Level
SYSVOL replication method
List of Domain Controllers
List of Read-Only Domain Controllers
Domain objects statistic:
Total number of computer objects :
Server systems
Total number of user objects  :
Total number of Domain Administrators:
Server roles installed on this DC
OS Version of DC
System Model
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
My approach would be to write a replacement function for write-host with the parameters used, and creating HTML code instead.
Jason CrawfordTransport NinjaCommented:
I'm confused on the scope of this script.  Are you running it on a specific Domain Controller that you want to list the OS and RAM for?  If so you're going to have to figure out how to find the Exchange version when running the script on a DC.  Not hard but you'll most likely do some remoting.  You're going back and forth between forest and domain points of view so for instance Domain Administrators...what if there is more than one domain in the forest?  That throws off several of the data points you're requesting.  I'm guessing this is some kind of discovery script for documentation purposes?  I would break this up a bit and maybe rethink what you're gathering and why you're gathering it.  I went ahead and grabbed some of the low hanging fruit to start you off.  

#Requires -version 3

Import-Module ActiveDirectory

$forest = Get-ADForest
$ip = Get-NetIPAddress
$ip4 = $ip | Where-Object {$_.AddressFamily -eq 'IPv4' -and $_.InterfaceAlias -notlike '*loopback*'}
$ip6 = $ip | Where-Object {$_.AddressFamily -eq 'IPv6' -and $_.InterfaceAlias -like '*ethernet*'}
$dc = Get-ADDomainController
$sm = $dc | Where-Object {$_.OperationMasterRoles -like '*SchemaMaster*'}
$dnm = $dc | Where-Object {$_.OperationMasterRoles -like '*DomainNamingMaster*'}
$pdc = $dc | Where-Object {$_.OperationMasterRoles -like '*PDCEmulator*'}
$rid = $dc | Where-Object {$_.OperationMasterRoles -like '*RIDMaster*'}
$im = $dc | Where-Object {$_.OperationMasterRoles -like '*InfrastructureMaster*'}
$computers = Get-ADComputer -Filter *

$results = [PSCustomObject]@{
    'Forest Name' = $
    'Schema Version' = (Get-ADObject (get-adrootdse).schemaNamingContext -Property objectVersion).objectVersion
    'Forest Functional Level' = (Get-ADForest).forestmode
    'Forest Domains' = $
    'IPv4 Address' = $ip4.IPAddress
    'IPv6 Address' = $ip6.IPAddress
    'Schema Master' = $sm.hostname
    'Domain Naming Master' = $dnm.hostname
    'PDC Emulator' = $pdc.hostname
    'RID master' = $rid.hostname
    'InfrastructureMaster' = $im.hostname
    'AD Computer Count' = $computers.count

$htmlresults = $results | ConvertTo-Html

$htmlresults | Out-File "$env:USERPROFILE\Desktop\discovery.html"

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Life1430Author Commented:
Script from Jason was helpful and on the track however was not meeting my purpose completely So I went and found a script with some information (I lost the link of that script) and after multiple changes I created below.

# Import Module Active Directory:
      Write-Host "Importing Active Directory Module" -ForegroundColor 'Green'
       Import-Module -Name ActiveDirectory

      $a = "<style>"
      $a = $a + "BODY{background-color:cian ;}"
      $a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
      $a = $a + "TH{border-width: 1px;padding: 5px;border-style: solid;border-color: black;background-color:thistle}"
      $a = $a + "TD{border-width: 1px;padding: 5px;border-style: solid;border-color: black;background-color:PaleGoldenrod}"
      $a = $a + "</style>"

#### HTML file

# Setting Variables
      $date = (Get-Date -Format d_MMMM_yyyy).toString()
      $filePATH = "$env:userprofile\Desktop\"
      $fileNAME = "AD_Info_" + $date + ".html"
      $file = $filePATH + $fileNAME
# Active Directory Variables
      $adFOREST = Get-ADForest
      $adDOMAIN = Get-ADDomain

      $hostname = $env:computername                
    $adFORESTNAME = $adFOREST.Name
    $Schema = Get-ADObject (Get-ADRootDSE).schemaNamingContext -Property objectVersion |Select -ExpandProperty objectversion
      $adFORESTMODE = $adFOREST.ForestMode
      $adFORESTDOMAIN = $adFOREST | select -ExpandProperty Domains
      $adFORESTROOTDOMAIN = $adFOREST.RootDomain
      $adFORESTSchemaMaster = $adFOREST.SchemaMaster
      $adFORESTNamingMaster = $adFOREST.DomainNamingMaster
      $adFORESTGlobalCatalog = $adFOREST | select -ExpandProperty GlobalCatalogs
      $adFORESTSites = $adFOREST  |  select -ExpandProperty Sites

      $adDomainName = $adDOMAIN.Name
    $Domaincontrollername= (Get-WmiObject -class Win32_OperatingSystem).Caption
      $adDOMAINNetBiosName = $adDOMAIN.NetBIOSName
      $adDOMAINDomainMode = $adDOMAIN.DomainMode
      $adDOMAINParentDomain = $adDOMAIN.ParentDomain
    $adDomainName = "$env:userdnsdomain"
      $adDOMAINPDCEMu = $adDOMAIN.PDCEmulator
      $adDOMAINRIDMaster = $adDOMAIN.RIDMaster
      $adDOMAINInfra = $adDOMAIN.InfrastructureMaster
      $adDOMAINChildDomain = $adDOMAIN | select -ExpandProperty ChildDomains
      $adDOMAINReplica = $adDOMAIN | select -ExpandProperty ReplicaDirectoryServers
      $adDOMAINReadOnlyReplica = $adDOMAIN | select -ExpandProperty ReadOnlyReplicaDirectoryServers
    $IPaddressv4 = $ip4 |select -ExpandProperty IPAddress
    $GatewayIP = Get-NetIPConfiguration | Foreach IPv4DefaultGateway |select -ExpandProperty Nexthop
    $DNSServer = Get-NetIPConfiguration | select DNSSERVER -ExpandProperty DNSSERVER |Select ServerAddresses -ExpandProperty Serveraddresses
    $Manufacturer = get-wmiobject win32_computersystem | select Manufacturer -ExpandProperty Manufacturer
    $AllDC = Get-ADDomainController -Filter * | Select-Object -ExpandProperty name
    $AllUsers = (get-aduser -filter *).count
    $AllComputers = (Get-ADComputer -filter *).count
# File Creation

      if (Test-Path "$env:userprofile\Desktop\$filename" ) {
            Write-Warning "file already exists, i am deleting it."
            Remove-Item "$env:userprofile\Desktop\$filename" -Verbose -Force
            Write-Host "Creating a New file Named as $fileNAME" -ForegroundColor 'Green'
            New-Item -Path $filePATH -Name $fileNAME -Type file | Out-Null
      else {
            Write-Host "Creating a New file Named as $fileNAME" -ForegroundColor 'Green'
            New-Item -Path $filePATH -Name $fileNAME -Type file | Out-Null
###HTML Conversion

      ConvertTo-Html  -Head $a  -Title "Active Directory Information" -Body "<h1> Detailed information for :  $adFORESTNAME </h1>" > $file

      ConvertTo-Html  -Head $a -Body "<h2> Active Directory Forest Information. </h2>"  >> $file
      ConvertTo-Html -Body "<table>
                      <tr><td> Forest Name: </td><td><b> $adFORESTNAME </b></td></tr> `
                                <tr><td> Forest Functional Level: </td><td><b> $adFORESTMODE </b></td></tr> `
                      <tr><td> Domain Functional Level: </td><td><b> $adDOMAINDomainMode </b></td></tr> `
                      <tr><td> Forest Schema Version: </td><td><b> $schema </b></td></tr> `
                                <tr><td> Root Domain : </td><td><b> $adFORESTROOTDOMAIN </b></td></tr> `      
                      <tr><td> Domain Names under this forest: </td><td><b> $adFORESTDOMAIN </b></td></tr> `
                                <tr><td> Current Domain Name: </td><td><b> $adDomainName </b></td></tr> `
                      <tr><td> Current Domain NetBios Name: </td><td><b> $adDOMAINNetBiosName </b></td></tr> `
                      <tr><td> Parent Domain : </td><td><b> $adDOMAINParentDomain </b></td></tr> `
                      <tr><td> Child Domains : </td><td><b> $adDOMAINChildDomain </b></td></tr> `
                      <tr><td> Operating System of this DC: </td><td><b> $Domaincontrollername </b></td></tr> `
                      <tr><td> Domain Naming Master: </td><td><b> $adFORESTNamingMaster </b></td></tr> `      
                                <tr><td> Schema Master: </td><td><b> $adFORESTSchemaMaster </b></td></tr> `      
                                 <tr><td> Domain PDC Emulator : </td><td><b> $adDOMAINPDCEMu </b></td></tr> `      
                                <tr><td> Domain RID Master: </td><td><b> $adDOMAINRIDMaster </b></td></tr> `      
                                 <tr><td> Domain InfraStructure Master : </td><td><b> $adDOMAINInfra </b></td></tr> `
                      <tr><td> Global Catalog Servers : </td><td><b> $adFORESTGlobalCatalog </b></td></tr> `
                      <tr><td> DC in domain : </td><td><b> $AllDC</b></td></tr> `
                      <tr><td> Number of Users : </td><td><b> $AllUsers</b></td></tr> `
                      <tr><td> Number of Computers : </td><td><b> $AllComputers</b></td></tr> `
                                <tr><td> Forest Domain Sites : </td><td><b> $adFORESTSites </b></td></tr></table>" >> $file

      ConvertTo-Html  -Head $a -Body "<h2> Active Directory Domain Controller Information (This DC). </h2>"  >> $file                                     
      ConvertTo-Html -Body "<table><tr><td> Computer Name: </td><td><b> $hostname </b></td></tr> `
                      <tr><td> Domain Name: </td><td><b> $adDomainName </b></td></tr> `
                                <tr><td> Manufacturer : </td><td><b> $Manufacturer</b></td></tr> `
                              <tr><td> Replicated Servers : </td><td><b> $adDOMAINReplica</b></td></tr> `
                      <tr><td> IP address : </td><td><b> $IPaddressv4</b></td></tr> `
                      <tr><td> Gateway IP : </td><td><b> $GatewayIP</b></td></tr> `
                      <tr><td> DNS Server : </td><td><b> $DNSServer</b></td></tr> `
                      <tr><td> Read Only Replicated Server : </td><td><b> $adDOMAINReadOnlyReplica </b></td></tr></table>" >> $file

      $Report = "The Report is generated On  $(get-date) by $((Get-Item env:\username).Value) on computer $((Get-Item env:\Computername).Value)"
      $Report  >> $file

      Invoke-Expression $file

#### End ###
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.