![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Sorting a Hash Table before outputting it to html 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 |
My System Specs![]() |
| | #2 (permalink) |
| | RE: Sorting a Hash Table before outputting it to html Use the getenumerator() method of the hash table. For example PS> $x = @{"one"="first";"two"="second";"three"="Third";"four"="fourth"} PS> $x Name Value ---- ----- four fourth two second three Third one first PS> $x.GetEnumerator() | Sort Name Name Value ---- ----- four fourth one first three Third two second PS> $x.GetEnumerator() | Sort Value Name Value ---- ----- one first four fourth two second three Third -- Richard Siddaway All scripts are supplied "as is" and with no warranty PowerShell MVP Blog: http://richardsiddaway.spaces.live.com/ PowerShell User Group: http://www.get-psuguk.org.uk "TimParker" wrote: Quote: > 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 > |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Sorting a Hash Table before outputting it to html On Jan 2, 10:11*am, TimParker <tim...@xxxxxx> wrote: Quote: > 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 Hashtableand 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 thetable, 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 haslogged 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 foreach ($key in $users.Keys){ to foreach ($key in ($users.Keys | sort)){ |
My System Specs![]() |
| | #4 (permalink) |
| | Re: Sorting a Hash Table before outputting it to html Thanks. I had tried sorting before the foreach statement. Didn't think about adding it there. DUH. Seems to be working fine. Will have to see Monday when all the users are logging in. On Jan 2, 11:05*pm, tojo2000 <tojo2...@xxxxxx> wrote: Quote: > On Jan 2, 10:11*am, TimParker <tim...@xxxxxx> wrote: > > > > > Quote: > > 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. Quote: > > 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. Quote: > > The hashtable has the following: Quote: > > tim parker, login, 6:00 am, logout, 5:00 pm Quote: > > 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. Quote: > > Here is the current script, hopefully the formatting won't be too > > hacked up....How can I sort my output within this script....... Quote: > > #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" Quote: > > #Added for script reference when run from the scheduler > > Add-PSSnapin Quest.ActiveRoles.ADManagement Quote: > > #File that we are going to create. > > $file = New-Item "\\server\soc\AllStaff\LogIn\Loginout2.htm" -type > > File -force Quote: > > $filepath = "\\server\misdata\LOGONS.csv" > > $date = Get-Date -format MM/dd/yyyy > > $entries = (gc $filepath) -match $date > > $datetime = Get-Date Quote: > > $intime = "" > > $outtime = "" > > $fullname = "" Quote: > > function getProperName(){ > > * * * * $proper = Get-QADUser -Enabled $key | select firstname, lastname > > * * * * return $full = $proper.firstname + " " + $proper.lastname Quote: > > } Quote: > > #Create Users HashTable to store info in > > $users = New-Object system.Collections.Hashtable Quote: > > #Step through the entries in the log for today > > foreach ($entry in $entries){ > > * * * * * * * * $test = $entry.Split(",") Quote: > > * * * * * * * * #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"){ Quote: > > * * * * * * * * * * * * #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"){ > > * * * * * * * * * * * * } > > * * * * * * * * * * * * } Quote: > > * * * * * * * * * * #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 > > * * * * * * * * * * * * * * * * * ** * } Quote: > > * * * * * * * * * * * * * * * * } > > * * * * * * * * * * * * * * #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 > > * * * * * * * * * * * * * * * * } > > * * * * * * * * * * * * } > > * * * * * * * * } Quote: > > * * * * * * * * $intime = "" > > * * * * * * * * $outtime = "" > > * * * * * * * * $fullname = "" Quote: > > } Quote: > > $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 Quote: > > 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 Quote: > > } Quote: > > $out = "</table></center></body></html>" > > Add-Content $file $out > Change the line > > * foreach ($key in $users.Keys){ > > to > > * foreach ($key in ($users.Keys | sort)){- Hide quoted text - > > - Show quoted text - |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| How to create a hash table from an array | PowerShell | |||
| Adding data to a hash table | PowerShell | |||
| Passing hash table by reference | PowerShell | |||
| Variable as hash table issue | PowerShell | |||
| How do I read a XML file into a hash table? | PowerShell | |||