What time did you start to work today?

Following the same idea and structure of the previous post, with this one we will get the first time in the current day our machine got unlocked and, based on a 8 hours journey with 30 minutes of lunch, how long left before you go home.

 

$journeyDuration = 8 #hours
$lunchDuration = 0.5 #hours

#Since 12am today
$date = [datetime]::Today


$totalTimeHaveToBeIn = $journeyDuration + $lunchDuration

$jd = [timespan]::fromHours($totalTimeHaveToBeIn)


# Grab the LOGON events from a DC                                                    #The workstation was unlocked
$Events = Get-WinEvent -ComputerName localhost -FilterHashtable @{Logname='Security';Id=4801; StartTime=$date}            

# Parse out the event message data            
ForEach ($Event in $Events) {  
          
    # Convert the event to XML            
    $eventXML = [xml]$Event.ToXml()
     
    # Iterate through each one of the XML message properties            
    For ($i=0; $i -lt $eventXML.Event.EventData.Data.Count; $i++) {            
        
        # Append these as object properties            
        Add-Member -InputObject $Event -MemberType NoteProperty -Force -Name  $eventXML.Event.EventData.Data[$i].name -Value $eventXML.Event.EventData.Data[$i].'#text'
        
    }            

} 

# Get the first unlock today
$TimeIn = $Events | Select-Object TimeCreated | Sort-Object -Property TimeCreated | Select-Object -First 1 
$dte = Get-Date

# difference between now and the first unlock
$ts = New-TimeSpan -Start $TimeIn.TimeCreated -End $dte


Write-Host "You probably got in at: " $TimeIn.TimeCreated -ForegroundColor Cyan
Write-Host "You have been in the office for: " $ts -ForegroundColor Cyan
Write-Host "Times your machine was unlocked: " -ForegroundColor Cyan

# Get every unlock event today
$Events | Select-Object TargetUserName, TargetDomainName, TimeCreated, Message | Sort-Object -Property TimeCreated 

If ($ts -gt $jd) {

Write-Host "TIME TO GO HOMEEEE!" -ForegroundColor RED
}
Else {

$ts2 = $totalTimeHaveToBeIn - $ts.TotalHours
$ts2 = [timespan]::fromHours($ts2)

Write-Host "Oh God, still have to work: " $ts2 -ForegroundColor YELLOW

}

 

You can also run that periodically and in case you have done your shift, could be sent an alert by email telling you to go home.

Check previous post for an example.

Any problems, please let me know 🙂