Tuesday, December 4, 2012

Why Error Checking Is The Bird, And The Bird Is The Word!



Today started as a normal day. I came into the office did my normal morning routine. Checked the many different mailboxes I’m tasked with up keeping, made sure all of our websites were up and running. Then i received an email from one of my supervisors. He was letting me know that he noticed something a little off. See recently i created a script that automatically uploads a user’s photo to their Active Directory account. Basically it puts Jim Bob’s stupid face on Jim Bob’s stupid account that he doesn’t even know how to log into. So doing some research into the errors I’m receiving ex:

Set-ADUser : A value for the attribute was not in the acceptable range of values
At C:\Scripts\PhotoUpdate\Photos.ps1:43 char:11
+ Set-ADUser <<<<  $horse.SamAccountName -Replace @{thumbnailPhoto=$photo}
    + CategoryInfo          : NotSpecified: (JoseMo:ADUser) [Set-ADUser], ADException
    + FullyQualifiedErrorId : A value for the attribute was not in the acceptable range o
   f values,Microsoft.ActiveDirectory.Management.Commands.SetADUser


I find that the photos even after resizing are upwards of 1MB. Now you might be thinking “1 MB is really small. Dude!”  WRONG! There is a 100KB Cap on what information you place in the field itself. So looking back over all of the staff pictures that were nestled softly in the directory bedding I realize there is no telling how many of these pictures never uploaded. Looking into it more than 1000 didn’t. So now I’m spending the rest of today with my old “frenemy” PhotoShop. So for today’s PowerShell tip: Implement Error Checking Dammit! You never know when your shit will break. So here it is the script that saved the day. 






#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#
################################################ Import AD snap-in ####################################################
#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@#
Import-Module ActiveDirectory


# Make a Directory where the modified pictures will go
mkdir \\file\staffidphotos$\AD

# Remove the old error file
rm C:\Scripts\PhotoUpdate\Nope.txt

# This script requires ImageMagick http://www.imagemagick.org
$tool = "C:\ImageMagick\convert.exe"

# Create New Erro File
New-Item C:\Scripts\PhotoUpdate\Nope.txt -type file

# Set the Resize Command
$cmd = "resize 9126@"
# Still setting
$test = "-"

# Get today's date
$Date=get-date
# Go Back 29 days
$Date1=$Date.AddDays(-29)


# This command will get all of the pictures that has been uploaded to your picture directory that are newer than 29 days ago
get-childitem "\\file\staffidphotos$" | where-object {$_.mode -notmatch "d"} | where-object {$_.lastwritetime -gt $date1} | foreach {$_.name} | Out-File "C:\Scripts\PhotoUpdate\NewPictures.txt"

# Simply Removes Empty Lines
(Get-Content "C:\Scripts\PhotoUpdate\NewPictures.txt") | where {$_ -ne ""} > "C:\Scripts\PhotoUpdate\NewPictures.txt"

# Get me them beautiful accountnames
$dog = Get-Content "C:\Scripts\PhotoUpdate\NewPictures.txt"

foreach ($person in $dog){
# CHecks to make sure the user has an AD account
$gator = Get-ADUser -Identity $person
If  ($gator -like $null){
# Adds the users name to here if they do not have an account
Add-Content C:\Scripts\PhotoUpdate\Nope.txt "$person"

}
else
{
echo $gator.SamAccountName
$file = "\\file\staffidphotos$\$person"
$file1 = "\\file\staffidphotos$\AD\$person"
Echo $file

# Runs the command to resize the picture
invoke-expression "$tool $file $test$cmd $file1"

# Formats the New photo to bytes
$photo = [byte[]](Get-Content $file1 -Encoding byte)

# Sets the Picture to the users account
Set-ADUser $gator.SamAccountName -Replace @{thumbnailPhoto=$photo}
}}


# Removes the old photos
rm \\file\staffidphotos$\AD -Recurse -Force
rm C:\Scripts\PhotoUpdate\NewPictures.txt


#Sends an email with who was not updated
$i = 0
$datey = Get-Date -Format %M/%d/%y
$FromAddress = "it@test.com"
$ToAddress = "it@test.com"
$paste = Get-Content C:\Scripts\PhotoUpdate\Nope.txt
$check = 1..$paste.count
while ($i -le $paste.count-1) { 
$check[$i] = $paste[$i] + "<br>" 

$i++ 
} 
$check12 = $check  -replace ".jpg",""
$MessageSubject = "Picture not updated for these users"
$MessageBody = "No Account on $datey for User:<br>$check12"
$SendingServer = "exchane.test.com"
send-mailmessage -from $FromAddress -to $ToAddress -subject $MessageSubject -body $MessageBody -smtpServer $SendingServer -BodyAsHtml

No comments:

Post a Comment