law school images

The law school wants a “face-book” with images of each student.  I lost the old script files and had some procedures changes (ie, i no longer have db access to the LCMS – i was using existing student enrollment in the course as a starting point for the faces).  So i did some rewriting.

First step is to generate an XML file for each student with their userid from the LCMS riles and name and the number of their photo image from AD.

Create an array as $name = @(), then get content from the enrollment file, select those entries with the right courses in them, split the enrollment string into three parts and save out the userid.  Next – create a new array from the old array, which is sorted and unique.  Just get each student once.

Open a connection to our LDAP server, get, for each userid, the firstname, lastname, class and finally the numeric id, which is the name of the image file.  Output that to a manually created XML file.

[array]$array= @()
$sorted_array = @()

$array+= (get-content \\server\c$\lcms\apps\snapshot\data\semesterStudents.txt|where-object {$_ -like "*_13LF*"})|foreach {$_.split("|")[1]}
# write-output ("this is the count:" + $array.count) # for testing
$sorted_array = $lawstu|sort -Unique
# write-output ("this is the count sorted:" + $sorted_array.count) # for testing

set up connection to AD

$objDomain = New-Object System.DirectoryServices.DirectoryEntry("LDAP://server.university.edu/ou=userOU, DC=university,DC=edu")
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
# header for XML file
"<?xml version='1.0'?>"|Out-File C:\users\rbeck\desktop\law\stuLawUsers.XML -Append
"<users>"|Out-File C:\users\rbeck\desktop\law\stuLawUsers.XML -Append
foreach ($user in $lawstusort){
     $strFilter = "(&(objectCategory=User)(samAccountName=" + $user + "))"
     $objSearcher.Filter = $strFilter
     $objSearcher.SearchScope = "Subtree"
     $colResults = $objSearcher.FindAll()

 foreach ($objResult in $colResults){
     $objUser = $objResult.GetDirectoryEntry()
     $fn = $objUser.FirstName
     $ln = $objUser.LastName
     $id = $objUser.samaccountname
     $classyr = $objUser.extensionattribute1
     $idnum = $objUser.employeeID
     $output = @"
        <u>
            <firstname>$fn</firstname>
            <lastname>$ln</lastname>
            <user_id>$id</user_id>
            <class>$classyr</class>
            <num>$idnum</num>
        </u>
"@
$output |Out-File C:\users\rbeck\desktop\law\stuLawUsers.XML -Append 
} # end of while for reader
} # end of XML\
"</users>"|Out-File C:\users\rbeck\desktop\law\stuLawUsers.XML -Append

OK, we have a data file.  The second script does some prep work, creates and if they exist, removes the contents of some directories, and reads the new XML file,  For each entry, checks for the existence of the image file on a remote server, outputs an error message if the image doesn’t exist, otherwise copies the image down to the local machine  into a ‘class’ directory that is picked from data in the xml file, renames the file to firstname-lastname.ext and goes to the next entry.  Zipping the directory saves about 50% of the size.  The operator needs to  have admin access to the server since we are using the admin share for the server and the files.

$path2files = "C:\local\desktop\law\lawstuimages\"
$path2images = "\\serveri\drive$\Data exchange\images\"
$folders = "Law First Year","Law Second Year","Law Third Year","Law Fourth Year","Law Graduated",  "Law Visiting","Other","Faculty","Staff","Grad First Year","Grad Second Year","Grad Third Year"
foreach ($folder in $folders){
if (test-path ("$path2files$folder")){
       Remove-Item "$path2files$folder\*"  #remove the images
} else {
        New-Item -ItemType Directory -Force -Path "$path2files$folder"  # create the folder
} # end else

Read the XML file.  Notice the path2files is not correct for the XML file, and i have to mess with it with string.replace()

[xml]$file = Get-Content ($path2files.replace("\lawstuimages","")+"stuLawUsers.XML")
foreach( $entry in $file.users.u) { 
      $num = ([int]$entry.num).toString("0000000") # pad number to 7 digits.
      $fn = $entry.firstname
      $ln = $entry.lastname
      $class = $entry.class
           if ($class -eq ""){ $class = "Other" }
# if path not found, no image - write message to console.
 # otherwise, copy the file, put the file into the folder based on class and rename it. 
 if (test-path "$path2images\$($entry.num).jpg"){
     copy-item -path "$path2images\$($entry.num).jpg" -destination $($path2files+"$class\")
     # write-output "copying $num.jpg or $($entry.num) to $class" # for testing
     $z = $($path2files+"$class\"+$entry.num + ".jpg")
     rename-item -path $z -newname "$fn-$ln.jpg"
 }  else {
     write-output ("can't find image for $fn $ln at $num")
 } # end of if/else on test-path
} # end of foreach XML

Reading the XML is really easy – i could probably used an internal library to get the XML outfile.  Notice the evaluation operator needed at lots of points to get from a variable to the value of the variables properties.

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.