locked
Server Uptime report and need to send mail using power shell RRS feed

  • Question

  •  

     Hi SanjayPaul,

    I use your powershell server uptime script it is really nice, I try to reach you ask question through your post but no luck, So i want some modification in that script could you please help me in that script in need one more column Last reboot date and time and that output file i need to send mail daily basis. it will be very help full for me.

    Script details:-

    EXAMPLE 
    Uptime_Report.ps1
    ######################################################################>

    $Now = Get-Date
    Function GetStatusCode

    Param([int] $StatusCode)  
    switch($StatusCode)
    {
    0 {"Success"}
    11001 {"Buffer Too Small"}
    11002   {"Destination Net Unreachable"}
    11003   {"Destination Host Unreachable"}
    11004   {"Destination Protocol Unreachable"}
    11005   {"Destination Port Unreachable"}
    11006   {"No Resources"}
    11007   {"Bad Option"}
    11008   {"Hardware Error"}
    11009   {"Packet Too Big"}
    11010   {"Request Timed Out"}
    11011   {"Bad Request"}
    11012   {"Bad Route"}
    11013   {"TimeToLive Expired Transit"}
    11014   {"TimeToLive Expired Reassembly"}
    11015   {"Parameter Problem"}
    11016   {"Source Quench"}
    11017   {"Option Too Big"}
    11018   {"Bad Destination"}
    11032   {"Negotiating IPSEC"}
    11050   {"General Failure"}
    default {"Failed"}
    }
    }
    Function GetUpTime
    {
    param([string] $LastBootTime)
    $Now = Get-Date
    $Uptime = (Get-Date) - [System.Management.ManagementDateTimeconverter]::ToDateTime($LastBootTime)
    "$($Uptime.Days) Days; $($Uptime.Hours) Hrs; $($Uptime.Minutes) Mins; $($Uptime.Seconds) Secs" 
    }
    #Change value of the following parameter as needed
    $OutputFile = ".\Uptime-" + [DateTime]::Now.ToString("yyyy-MM-dd-HH-mm") + ".htm"
    $ServerList = Get-Content ".\ServerList.txt"
    $SlNo = 0
    $Result = @()
    Foreach($ServerName in $ServerList)
    {
    $SlNo = $SlNo + 1
    $pingStatus.StatusCode = $null
    $pingStatus = Get-WmiObject -Query "Select * from win32_PingStatus where Address='$ServerName'"
    $Uptime = $null
    if($pingStatus.PrimaryAddressResolutionStatus -eq 0)
    {
    if($pingStatus.StatusCode -eq 0)
    {
    try {
    $OperatingSystem = Get-WmiObject Win32_OperatingSystem -ComputerName $ServerName -ErrorAction Stop
    $Uptime = GetUptime( $OperatingSystem.LastBootUpTime )
    }
    catch {
    $Uptime = "NA"
    }
    }

    $Result += New-Object PSObject -Property @{
    SlNo = $SlNo
    ServerName = $ServerName
    IPV4Address = $pingStatus.IPV4Address
    Status = GetStatusCode( $pingStatus.StatusCode )
    Uptime = $Uptime
    }
    }
    else {
    $Result += New-Object PSObject -Property @{
    SlNo = $SlNo
    ServerName = $ServerName
    IPV4Address = $pingStatus.IPV4Address
    Status = GetStatusCode( $pingStatus.PrimaryAddressResolutionStatus )
    Uptime = $Uptime
    }
    }
    }

    if($Result -ne $null)
    {
    $HTML = '<style type="text/css">
    #Header{font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;width:100%;border-collapse:collapse;}
    #Header td, #Header th {font-size:14px;border:1px solid #98bf21;padding:3px 7px 2px 7px;}
    #Header th {font-size:14px;text-align:left;padding-padding-bottom:4px;background-color:#A7C942;color:#fff;}
    #Header tr.alt td {color:#000;background-color:#EAF2D3;}
    </Style>'
        $HTML += "<HTML><BODY><Table border=1 cellpadding=0 cellspacing=0 id=Header>
        <TR>
        <TH><B>Sl No.</B></TH>
        <TH><B>Server Name</B></TH>
        <TH><B>IP Address</B></TD>
        <TH><B>Ping Status</B></TH>
        <TH><B>Uptime</B></TH>
        </TR>"
        Foreach($Entry in $Result)
        {
            if($Entry.Status -ne "Success")
            {
                $HTML += "<TR bgColor=Red>"
            }
    elseif($Entry.Uptime -eq "NA")
    {
    $HTML += "<TR bgColor=Yellow>"
    }
    else
    {
    $HTML += "<TR>"
    }
    $HTML += "
    <TD>$($Entry.SlNo)</TD>
    <TD>$($Entry.ServerName)</TD>
    <TD>$($Entry.IPV4Address)</TD>
    <TD>$($Entry.Status)</TD>
    <TD>$($Entry.Uptime)</TD>
    </TR>"
        }
    $HTML += "<p>
    Time : $($Now) <br>
             </P>"
    $HTML += "<p style={font-family:arial;color:red;font-size:14px;text-align:center;}><u><b>Server Uptime</b></u></p>"
    $HTML += "</Table></BODY></HTML>"
    $HTML | Out-File $OutputFile
    }invoke-item .\$outputfile

     


    Thursday, July 16, 2020 1:37 AM

All replies

  • Hi Sanjay,

    I use your powershell script for server uptime report it's really nice to use, but i want some information on that output file server name, ping status, ip address, uptime on that i want date also which date reboot happen on that server and i need to send mail that output report file automatic .  once job run complete that output file need to send mail to mention mail id automatic.

    i try that it will come mail but i face one issue on that once that server uptime script run complete i got mail also but i received how many server we have  that much mail i received suppose i have 10 servers i receive 10 mails. can you help on this sissue.

    • Merged by jrv Thursday, July 16, 2020 1:51 AM DUPLICATE
    Sunday, July 12, 2020 5:11 PM
  • Who is Sanjay.  For help with scripts found on the Internet you need to contact a consultant or the author of the script.  This forum is only for questions about scripts you have written.  It is not a script repair or fr4ee script modifying forum.


    \_(ツ)_/

    Sunday, July 12, 2020 5:37 PM
  • Who is Sanjay.  For help with scripts found on the Internet you need to contact a consultant or the author of the script.  This forum is only for questions about scripts you have written.  It is not a script repair or fr4ee script modifying forum.

    Please contact a consultant to help you.  This is not a forum for Gallery scripts.


    \_(ツ)_/

    Thursday, July 16, 2020 1:47 AM
  • Please carefully review the following links to set your expectation for posting in technical forums.


    \_(ツ)_/

    Thursday, July 16, 2020 1:48 AM