Skip to content

Instantly share code, notes, and snippets.

@mistificator
Last active February 20, 2018 14:41
Show Gist options
  • Save mistificator/352ba16e75fc579339e40fe16b2355f3 to your computer and use it in GitHub Desktop.
Save mistificator/352ba16e75fc579339e40fe16b2355f3 to your computer and use it in GitHub Desktop.
Calculates the worktime since month beginning
#*=============================================================================
#* Script Name: getWorkTime
#* Based on True Percent Uptime, https://gallery.technet.microsoft.com/scriptcenter/True-Uptime-Calculator-5903cf22
#* Created: 20/02/2018
#* Authors: James Keeler, Mist Poryvaev
#*
#* Params: [int]$Holidays - number of holidays, default is 0.
#*
#*=============================================================================
#* Purpose: Calculates the worktime since month beginning.
#*
#*
#*=============================================================================
#*
#* Note: call "Set-ExecutionPolicy Unrestricted -Scope CurrentUser" before first run
#*
#*=============================================================================
Param(
[int] $Holidays = 0
)
Process {
$ComputerName = $env:computername
[timespan]$uptime = 0
$startingDate = Get-Date ((("01/" + (Get-Date).Month).ToString() + "/" + ((Get-Date).Year).ToString() + " 00:00:00"))
Write-Host -NoNewline "Retrieving shutdown and startup events from "
Write-Host "$ComputerName from the month beginning..."
$events = Invoke-Command -ScriptBlock {`
param($date)
Get-EventLog `
-After ($date) `
-Log System `
| Where-Object {
# kernel start
($_.EventID -eq 12 -AND $_.Source -eq "Microsoft-Windows-Kernel-General")`
-OR `
($_.EventID -eq 1 -AND $_.Source -eq "Microsoft-Windows-Power-Troubleshooter")`
-OR `
# kernel finish
($_.EventID -eq 13 -AND $_.Source -eq "Microsoft-Windows-Kernel-General")`
-OR `
($_.EventID -eq 42 -AND $_.Source -eq "Microsoft-Windows-Kernel-Power") `
-OR `
($_.EventID -eq 41 -AND $_.Source -eq "Microsoft-Windows-Kernel-Power")
}
} -ArgumentList $startingDate -ErrorAction Stop
$sortedList = New-object system.collections.sortedlist
ForEach($event in $events)
{
$sortedList.Add( $event.TimeGenerated, $event.EventID )
}
$sortedList.Add($(Get-Date((Get-Date).toString("dd/MM/yy HH:mm:ss"))), 0 )
For($i = 1; $i -lt $sortedList.Count; $i = $i + 2 )
{
$day = $sortedList.Keys[$i].Day
$duration = ($sortedList.Keys[$i] - $sortedList.Keys[$i-1])
$uptime += $duration
Write-Output "$day `t$duration"
} #end for item
$NumberOfDays = 0
For($i = 0; $i -lt (Get-Date).Day; $i++ )
{
if (0,6 -notcontains (Get-Date($startingDate)).AddDays($i).DayOfWeek)
{
$NumberOfDays++
}
}
$NumberOfDays -= $Holidays
# Create a custom object to hold the results
$results = "" | Select-Object `
Name, `
Days, `
Hours_Total, `
Hours_Per_Day
$results.Name = $ComputerName
$results.Days = $NumberOfDays
$results.Hours_Total = "{0:n0}" -f ($uptime.TotalMinutes/60)
$results.Hours_Per_Day = (Get-Date("00:00:00")).AddMinutes($uptime.TotalMinutes/$NumberOfDays).toString("HH:mm")
Write-Output $results | Format-List
Write-Output "Press any key to continue..."
[void][System.Console]::ReadKey($true)
} # end Process
#*=============================================================================
#* END OF SCRIPT
#*=============================================================================
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment