Displaying the powershell output into html

Hi Guys,

I got a code from scripting guys repository. That best suits for my object. but the thing I want to display the result of the code to the HTML page. How can i do it. Sorry if I didnt provided enough/relavent information. Here is the code

Function Ping-Host
{
    #Parameter Definition
    Param
    (
    [Parameter(position = 0)] $Hosts,
    [Parameter] $ToCsv
    )
        #Funtion to make space so that formatting looks good
        Function Make-Space($l,$Maximum)
        {
        $space =""
        $s = [int]($Maximum - $l) + 1
        1..$s | %{$space+=" "}

        return [String]$space
        }
    #Array Variable to store length of all hostnames
    $LengthArray = @() 
    $Hosts | %{$LengthArray += $_.length}

    #Find Maximum length of hostname to adjust column witdth accordingly
    $Maximum = ($LengthArray | Measure-object -Maximum).maximum
    $Count = $hosts.Count

    #Initializing Array objects 
    $Success = New-Object int[] $Count
    $Failure = New-Object int[] $Count
    $Total = New-Object int[] $Count
    cls
    #Running a never ending loop
    while($true){

    $i = 0 #Index number of the host stored in the array
    $out = "| HOST$(Make-Space 4 $Maximum)| STATUS | SUCCESS  | FAILURE  | ATTEMPTS  |" 
    $Firstline=""
    1..$out.length|%{$firstline+="_"}

    #output the Header Row on the screen
    Write-Host $Firstline 
    Write-host $out -ForegroundColor White -BackgroundColor Black

    $Hosts|%{
    $total[$i]++
    If(Test-Connection $_ -Count 1 -Quiet -ErrorAction SilentlyContinue)
    {
    $success[$i]+=1
    #Percent calclated on basis of number of attempts made
    $SuccessPercent = $("{0:N2}" -f (($success[$i]/$total[$i])*100))
    $FailurePercent = $("{0:N2}" -f (($Failure[$i]/$total[$i])*100))

    #Print status UP in GREEN if above condition is met
    Write-Host "| $_$(Make-Space $_.Length $Maximum)| UP$(Make-Space 2 4)  | $SuccessPercent`%$(Make-Space ([string]$SuccessPercent).length 6) | $FailurePercent`%$(Make-Space ([string]$FailurePercent).length 6) | $($Total[$i])$(Make-Space ([string]$Total[$i]).length 9)|" -BackgroundColor Green
    }
    else
    {
    $Failure[$i]+=1

    #Percent calclated on basis of number of attempts made
    $SuccessPercent = $("{0:N2}" -f (($success[$i]/$total[$i])*100))
     $FailurePercent = $("{0:N2}" -f (($Failure[$i]/$total[$i])*100))

    #Print status DOWN in RED if above condition is met
    Write-Host "| $_$(Make-Space $_.Length $Maximum)| DOWN$(Make-Space 4 4)  | $SuccessPercent`%$(Make-Space ([string]$SuccessPercent).length 6) | $FailurePercent`%$(Make-Space ([string]$FailurePercent).length 6) | $($Total[$i])$(Make-Space ([string]$Total[$i]).length 9)|" -BackgroundColor Red
    }
    $i++

    }

    #Pause the loop for few seconds so that output 
    #stays on screen for a while and doesn't refreshes

    Start-Sleep -Seconds 10
    cls
    }
}

Ping-Host '192.168.1.62' 
Could you guys help me out in t
September 9th, 2015 12:10am

As you will see and hear here from many; we don't modify code found on the Internet.  Code samples in the repository are there for you to use and modify.  If you do not know how to write the code then it is up to you to learn.  We can answer specific questions.

Start here: https://technet.microsoft.com/en-us/scriptcenter/dd793612.aspx?f=255&MSPPError=-2147217396

Free Windows Admin Tool Kit Click here and download it now
September 9th, 2015 2:15am

Hi Basati,

As jrv said we can help you point in the right direction, from where you have to help yourself ofcourse.

In short this is where you should focus on:

1. Write-Host sections are the onces displaying the current output to screen, you should get it to a $variable instead.

$variable+="String"

2. Then use $variable | ConvertTo-HTML to get the html formatting

Go through this guide below, follow along you should get a hang of it by the end.

Creating Formatted HTML Output:

https://technet.microsoft.com/en-us/library/ff730936.aspx?f=255&MSPPError=-2147217396

September 9th, 2015 5:37am

Unfortunately string variables don't convert to HTML in any useful way.

Free Windows Admin Tool Kit Click here and download it now
September 9th, 2015 5:45am

That's true though.
September 9th, 2015 8:11am

Thanks Satyajit,

You and jrv are guiding the people in the right way. Thanks for that. but I am sure I will come up with a solution and post it as an answer. Thanks for the support. Scripting Guys forum always rockz!!!!!

Free Windows Admin Tool Kit Click here and download it now
September 9th, 2015 11:59pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics