I have a script that reads a file that contains user log in and log
out data. I have a PS script that reads this on the fly for the
current day/time that it is run and in the script it then creates an
html file that is read from our intranet.
The problem I have is that I need to be able to sort the data by the
users first name to make it a little nicer to read.
The hashtable has the following:
tim parker, login, 6:00 am, logout, 5:00 pm
I take the data and then split it on the comma to come up with the
data that I need to create the line in the html table.
Here is the current script, hopefully the formatting won't be too
hacked up....How can I sort my output within this script.......
#Log In/Out Web Page Board v4
#Read the log file that is being created and create a web page that
reads through the log entries and
#creates an HTML page that can be viewed to tell who is "in the
building and who isn't"
#Added for script reference when run from the scheduler
Add-PSSnapin Quest.ActiveRoles.ADManagement
#File that we are going to create.
$file = New-Item "\\server\soc\AllStaff\LogIn\Loginout2.htm" -type
File -force
$filepath = "\\server\misdata\LOGONS.csv"
$date = Get-Date -format MM/dd/yyyy
$entries = (gc $filepath) -match $date
$datetime = Get-Date
$intime = ""
$outtime = ""
$fullname = ""
function getProperName(){
$proper = Get-QADUser -Enabled $key | select firstname, lastname
return $full = $proper.firstname + " " + $proper.lastname
}
#Create Users HashTable to store info in
$users = New-Object system.Collections.Hashtable
#Step through the entries in the log for today
foreach ($entry in $entries){
$test = $entry.Split(",")
#first check to make sure this isn't the admin account. Skipping
it.
if($test[1].trim() -ne "administrator" -or $test[1].trim() -ne
"delegate" -or $test[1].trim() -ne "score" -or $test[1].trim() -ne
"Administrator" -or $test[1].trim() -ne "Delegate"){
#User Doesn't exist in Hashtable so add them in.
if($users.contains($test[1].trim()) -eq $FALSE){
if($test[4].trim() -eq "Login"){
if($test[4].trim() -eq "Login"){
$intime = $test[3]
}
elseif($test[4].trim() -eq "LogOut"){
$outtime = $test[3]
}
$value = "Login," + $intime.trim() +",Logout," + $outtime.Trim()
$users.add($test[1], $value)
}
#First entry in Hashtable and the value is logout so we are
skipping, most likely from Previous day (Hedges/Craft/Parker)
elseif($test[4].trim() -eq "Logout"){
}
}
#User has an entry already in the table, now to check if they
are leaving or have hit
#another machine. This gets complicated.
if($users.contains($test[1].trim()) -eq $TRUE){
$tempval = $users.get_item($test[1]).split(",")
#User has logged into a second machine or a second time for the
day.
if($test[4].trim() -eq "Login"){
#User had logged out/left at some point and now is logging back
in so we will update the logout time to blank
#since they are back.
if($tempval[3].length -gt 0){
$value = "Login," + $tempval[1] +",Logout," + ""
$users[$test[1]] = $value
}
}
#User Apparently has left for the day or the time being.
elseif($test[4].trim() -eq "Logout"){
$tempval = $users.get_item($test[1]).split(",")
$value = "Login," + $tempval[1] +",Logout," + $test[3]
$users[$test[1]] = $value
}
}
}
$intime = ""
$outtime = ""
$fullname = ""
}
$out = "<html><head><title>MOPS - Log In/Out Data</title></
head><body><center><table border=""1"" width=""40%"">"
$out = $out + "<tr><td colspan=""3"" align=""center"">Employee Status
based on Login/Logout on Computers</td></tr><tr><td colspan=""3""
align=""center"">Last Updated: $datetime</td></tr>"
$out = $out + "<tr><td>Employee</td><td>Time In</td><td>Time Out</td></
tr>"
Add-Content $file $out
foreach ($key in $users.Keys){
$times = $users.get_item($key).split(",")
if($times[3].length -eq 0){
$times[3] = " "
}
if($times[1].length -eq 0){
$times[1] = " "
}
$full = getProperName $key
#$full = $key
$out="<tr><td>" + $full + "</td><td>" + $times[1] + "</td><td>" +
$times[3] + "</td></tr>"
Add-Content $file $out
}
$out = "</table></center></body></html>"
Add-Content $file $out


