none
Displaying the powershell output into html RRS feed

  • Question

  • 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 this regard

    Naveen Basati

    Wednesday, September 9, 2015 4:08 AM

Answers

  • 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


    \_(ツ)_/

    • Marked as answer by BASATI Thursday, September 10, 2015 3:56 AM
    Wednesday, September 9, 2015 6:13 AM
  • 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


    Regards,

    Satyajit

    Please“Vote As Helpful” if you find my contribution useful or “MarkAs Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.


    • Edited by Satyajit321 Wednesday, September 9, 2015 9:36 AM
    • Marked as answer by BASATI Thursday, September 10, 2015 3:56 AM
    Wednesday, September 9, 2015 9:35 AM

All replies

  • 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


    \_(ツ)_/

    • Marked as answer by BASATI Thursday, September 10, 2015 3:56 AM
    Wednesday, September 9, 2015 6:13 AM
  • 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


    Regards,

    Satyajit

    Please“Vote As Helpful” if you find my contribution useful or “MarkAs Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.


    • Edited by Satyajit321 Wednesday, September 9, 2015 9:36 AM
    • Marked as answer by BASATI Thursday, September 10, 2015 3:56 AM
    Wednesday, September 9, 2015 9:35 AM
  • Unfortunately string variables don't convert to HTML in any useful way.


    \_(ツ)_/

    Wednesday, September 9, 2015 9:43 AM
  • That's true though.

    Regards,

    Satyajit

    Please “Vote As Helpful” if you find my contribution useful or “Mark As Answer” if it does answer your question. That will encourage me - and others - to take time out to help you.

    Wednesday, September 9, 2015 12:10 PM
  • 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!!!!!


    Naveen Basati

    Thursday, September 10, 2015 3:58 AM