Availability Server uptime

hello.

Not loking for any help. Im lokoing to share what ive created with the hope it may help someone else? My requirement was to report on server availability for the previous month as a percentage. Couldnt find these requirements anywhere. Please see below. Hope this helps someone.

<#

.SYNOPSIS

Simple script to provide server availability uptime statistics

.DESCRIPTION

This script will define the amount of time a server has been unavaialble for in the past 31 days.

Scans the system event log for 6005 and 6006 event id's using an XML filter (faster). These event id's are startup and shutdown entires,

the first and last thing that happens on a server. We subtract the amount of time between the two events

and calculate the down time.

.NOTES

Author Matt Nicholas

#>

#$Diff = 86400000*$DaysAgo # Milliseconds in a day

$startDate

 

 

= get-date $(get-date).AddMonths(-1).ToString("01/MM/yyyy")

$endDate

 

 

= $startDate.AddMonths(1).AddMilliseconds(-1)

 

 

$XMLFilter = "<QueryList>

<Query Id=`"0`" Path=`"System`">

<Select Path=`"System`">*[System[( (EventID = 6005 or EventID = 6006) ) and TimeCreated[@SystemTime&gt;='

 

 

$($startDate.tostring("yyyy-MM-dd"))T00:00:00.000Z' and @SystemTime&lt;='$($endDate.tostring("yyyy-MM-dd"))T22:59:59.999Z']]]</Select>

</Query>

</QueryList>"

 

 

 

 

# Get the list of tartget servers

[

 

 

string[]]$Computers = (Get-Content "c:\utils\servers.txt")

# Foreach target server

 

 

 

ForEach

 

 

($computer in $Computers)

{

 

 

 

# Get the reboot info from event log on current target server

 

 

$RebootEvents = get-winevent -FilterXML $XMLFilter -computername $Computer | sort-object -Descending -property TimeCreated

 

 

$down = $null

 

 

$up = $null

 

 

[timespan]$TotalDownTime = New-TimeSpan

 

 

Foreach($rebootEvent in $RebootEvents)

{

 

 

if ($rebootEvent.ID -eq 6006)

{

 

 

$down = $rebootEvent.TimeCreated

 

}

 

#end if eventID

 

 

Else

{

 

 

$up = $rebootEvent.TimeCreated

}

 

#end else

 

 

if($down -AND $up)

{

 

 

if( 22679 48649 230 false Format output display Hi developer,

Here is my logic script:
$fileList = dir 'H:\aa.txt'

$regex = "(([-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?))|((?<=\"")((\w+(\s+\w+)?|\d+))(?<!\""))|[a-zA-Z]]>foreach ($file in $fileList){
    $matchInfo = @((Get-Content $file) | ForEach-Object{ $_.Trim()}| Select-String -Pattern  $regex -AllMatches)
    $output = foreach($minfo in $matchInfo){
                foreach($match in @($minfo.Matches | Foreach {$_.Groups[0].value})){
                    $match 
                }
            }
    $output
}

where aa.txt:
aa aa aa
bb bb bb

I run it and display output like this:

aa aa aa bb bb bb

But i need to display output like this:

aa,aa,aa
bb,bb,bb

Any Idea?
Thanks

 

Parents
  • Thanks for sharing, be sure to upload it to the Scripts section of this page as well. I cleaned up your code a bit as it was on multiple lines which made the script fail.

    <#
    .SYNOPSIS
    Simple script to provide server availability uptime statistics
    .DESCRIPTION
    This script will define the amount of time a server has been unavaialble for in the past 31 days.
    Scans the system event log for 6005 and 6006 event id's using an XML filter (faster). These event id's are startup and shutdown entires,
    the first and last thing that happens on a server. We subtract the amount of time between the two events
    and calculate the down time.
    .NOTES
    Author Matt Nicholas
    #>
    #$Diff = 86400000*$DaysAgo # Milliseconds in a day
    $startDate = get-date $(get-date).AddMonths(-1).ToString("01/MM/yyyy")
    $endDate = $startDate.AddMonths(1).AddMilliseconds(-1)

    $XMLFilter = "<QueryList>
    <Query Id=`"0`" Path=`"System`">
    <Select Path=`"System`">*[System[( (EventID = 6005 or EventID = 6006) ) and TimeCreated[@SystemTime&gt;='
    $($startDate.tostring("yyyy-MM-dd"))T00:00:00.000Z' and @SystemTime&lt;='$($endDate.tostring("yyyy-MM-dd"))T22:59:59.999Z']]]</Select>
    </Query>
    </QueryList>"

    # Get the list of tartget servers
    [string[]]$Computers = (Get-Content "c:\utils\servers.txt")
    # Foreach target server
    ForEach ($computer in $Computers) {
    # Get the reboot info from event log on current target server
    $RebootEvents = get-winevent -FilterXML $XMLFilter -computername $Computer | sort-object -Descending -property TimeCreated
    $down = $null
    $up = $null
    [timespan]$TotalDownTime = New-TimeSpan
    Foreach($rebootEvent in $RebootEvents) {
    if ($rebootEvent.ID -eq 6006) {
    $down = $rebootEvent.TimeCreated
    } #end if eventID
    Else {
    $up = $rebootEvent.TimeCreated
    } #end else
    if($down -AND $up) {
    if($down -ge $up) {
    Write-Host -foregroundColor Red "*** Invalid data. Ignoring $($up)"
    $up = $down
    }
    #end if down is greater than up
    [timespan]$CurrentDownTime = new-TimeSpan -start $down -end $up
    $TotalDownTime += $currentDownTime
    $down = $null
    $up = $null
    } #end if down and up  
    } #end foreach

    $periodDifference = $endDate - $startdate
    $minutesInMonth = $diff.mins
    $minutesInDay = 24*60
    $percentUpTime = (100 - ($TotalDownTime.TotalMinutes/$periodDifference.TotalMinutes)*100)
    "This computes to $($percentUptime.ToString("###.###")) percent uptime on $computer for the previous month period"
    }
  • Hi Meneer ,

    How to suppress the error below after the server name ?

    Get-WinEvent : No events were found that match the specified selection criteria.
    At C:\Users\Admin\8bacc91d-9b53-4d9e-9822-522d088e0f9a.ps1:30 char:18
    + ... ootEvents = Get-WinEvent -FilterXML $XMLFilter -computername $Compute ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ObjectNotFound: (:) [Get-WinEvent], Exception
    + FullyQualifiedErrorId : NoMatchingEventsFound,Microsoft.PowerShell.Commands.GetWinEventCommand

    <#
    .SYNOPSIS
    Simple script to provide server availability uptime statistics
    .DESCRIPTION
    This script will define the amount of time a server has been unavailable for in the past 31 days.
    Scans the system event log for 6005 and 6006 event id's using an XML filter (faster). These event id's are startup and shutdown entires,
    the first and last thing that happens on a server. We subtract the amount of time between the two events
    and calculate the down time.
    .NOTES
    Author Matt Nicholas
    Modified by Jaap Brasser
    #>
    #$Diff = 86400000*$DaysAgo # Milliseconds in a day
    $startDate = Get-Date $(Get-Date).AddMonths(-1).ToString("01/MM/yyyy")
    $endDate = $startDate.AddMonths(1).AddMilliseconds(-1)

    $XMLFilter = "<QueryList>
    <Query Id=`"0`" Path=`"System`">
    <Select Path=`"System`">*[System[( (EventID = 6005 or EventID = 6006) ) and TimeCreated[@SystemTime&gt;='$($startDate.tostring("yyyy-MM-dd"))T00:00:00.000Z' and @SystemTime&lt;='$($endDate.tostring("yyyy-MM-dd"))T22:59:59.999Z']]]</Select>
    </Query>
    </QueryList>"

    # Get the list of tartget servers
    [string[]]$Computers = Get-ADComputer -Filter {Enabled -eq $True -and OperatingSystem -like "*Server*"} -SearchBase "OU=Servers,DC=Domain,DC=com" |
    Select-Object -ExpandProperty Name |
    Where-Object {Test-Connection $_.Name -Count 1 -Quiet}
    # Foreach target server
    ForEach ($computer in $Computers) {
    # Get the reboot info from event log on current target server
    $RebootEvents = Get-WinEvent -FilterXML $XMLFilter -computername $Computer | Sort-Object -Descending -property TimeCreated
    $down = $null
    $up = $null
    [timespan]$TotalDownTime = New-TimeSpan
    Foreach($rebootEvent in $RebootEvents) {
    if ($rebootEvent.ID -eq 6006) {
    $down = $rebootEvent.TimeCreated
    } #end if eventID
    Else {
    $up = $rebootEvent.TimeCreated
    } #end else
    if($down -AND $up) {
    if($down -ge $up) {
    Write-Host -foregroundColor Red "*** Invalid data. Ignoring $($up)"
    $up = $down
    }
    #end if down is greater than up
    [timespan]$CurrentDownTime = New-TimeSpan -start $down -end $up
    $TotalDownTime += $currentDownTime
    $down = $null
    $up = $null
    } #end if down and up
    } #end foreach

    $periodDifference = $endDate - $startdate
    $minutesInMonth = $diff.mins
    $minutesInDay = 24*60
    $percentUpTime = (100 - ($TotalDownTime.TotalMinutes/$periodDifference.TotalMinutes)*100)
    "This computes to $($percentUptime.ToString("###.###")) percent uptime on $computer for the previous month period"
    }

Reply Children
No Data