#URL to get the laptops data
$url = "$URL/status.xml"
#store the laptop xml file
$filename = "$PATH\status.xml"
$webClient = new-object System.Net.WebClient
$webClient.DownloadFile( $url, $filename )
#load the xml file we downloaded
[xml]$xml = get-content "status.xml"
#here is where we sort the xml file
$xml.notices.laptopCirc | ForEach-Object {
#Conversions for date and time for creating tasks
$TimeDueTS = [datetime]::ParseExact($_.timeDue.substring(0, 19), 'yyyy-MM-ddTHH:mm:ss', $null)
#Timestamps
#$CurrentTimeStamp
#add time to time due if required
$DueTime = $Due_Conversion.AddSeconds(+0).ToString('MM/dd/yyyy HH:mm:ss')
#get current time
$CurrentTime = (Get-Date).ToString('MM/dd/yyyy HH:mm:ss')
$CurrentTimeStamp = Get-Date -Format yyyy-MM-ddTHH:mm:ss
$removeTask = $_.laptopName+"_Due"
$isDue = $DueTime.substring(11, 5)
#Send an email if the laptop is due
if ($CurrentTime -match $DueTime){
#send an email if the current time matches the time due. Remove the task once the email is sent
#Write-output $_.laptopName $_.emailAddress
$emailBody = "Hello "+$_.displayName+",
Your laptop is now overdue and needs to be returned to the locker. If you need help with saving your document or the return process, please ask Staff.
Laptops are loaned for a maximum of 4 hours"
$emailSubject = "Please return "+$_.laptopName+" to the locker"
$emailSubject
Send-MailMessage -Encoding UTF8 -From '$FROM ADDRESS' -To $_.emailAddress -Subject $emailSubject -Body $emailBody -smtpserver '$SMTPSERVER'
Unregister-ScheduledTask -TaskName $removeTask -Confirm:$false
}
[PSCustomObject]@{
'displayName' = $_.displayName
'barcode' = $_.barcode
'emailAddress' = $_.emailAddress
'checkOutTime' = $_.checkOutTime
'timeDue' = $_.timeDue
'laptopName' = $_.laptopName
}
}
I have a scheduled task that runs every 5 minutes checking to see if a laptop is checked out; if it is it creates the notices; if not then it exits the script. If there is a notice already made it skips creating it again. It works really well.