SCRIPT: UploadPhoto.ps1

Script Name: UploadPhoto.ps1

Script Author: Anderson Patricio, MVP

Original Post: http://msmvps.com/blogs/andersonpatricio/pages/script-uploadphoto-ps1.aspx

Log:
v1.0 – Initial script. Check jpg file size and path. Uses a standard path as jpg repository and allows to add pictures to a single user or multiple users (-all switch)

Code:


#
# Script created by Anderson Patricio (<a href="http://msmvps.org/blogs/AndersonPatricio">http://msmvps.org/blogs/AndersonPatricio</a>)
#
# Syntax:
# uploadphoto.ps1 <mailbox-name>
# uploadphoto.ps1 -all (It will load all jpg files from the default folder and upload to the users in AD
#
param([Switch]$all, [String]$UserName)

#Default Values. Change them based on your environment.
$DefaultPhotoPath = 'C:\Photos'

Function CheckPhoto(){
 Write-Warning "Validating file(s).."
 Write-Host "File exists... " -nonewline
 If (Test-Path $PhotoPath)
  {
  Write-Host "[OK]" -ForeGroundColor Green
  Write-host "Photo size... "  -nonewline
  $PhotoSize = Get-ChildItem $PhotoPath | select Length
  If ($PhotoSize.Length -le 10000) { Write-Host "[OK]" -ForeGroundColor Green } Else { Write-Host "[Fail]" -ForeGroundColor Red; exit }
  }
 Else
  {
  Write-Host "[Fail]" -ForeGroundColor Red
  Exit
  }
}

Function UploadAll(){
 ForEach ($TempFile in Get-ChildItem $DefaultPhotoPath | Where-Object { $_.Extension -eq ".jpg" } )
  {
   $TempUserName = $TempFile.Name.substring(0, $TempFile.Name.Length - 4)
   Write-Host $TempUserName -ForeGroundColor Yellow -NoNewLine
   Import-RecipientDataProperty -Identity $TempUserName -Picture -FileData ([Byte[]]$(Get-Content -path $TempFile.Fullname -Encoding Byte -ReadCount 0))
   Write-Host "[Done]" -ForeGroundColor Green
  }
}

If ( $all -eq $true)
 {
 Write-Warning " ## This action will upload all pictures of C:\Photos to the AD users."
 Write-Warning " ## All pictures must have the same name of the usernames"
 Write-Warning "Are you sure that you want upload all pictures to the users (Y/N)?"
 $Opt = Read-Host
 If ( $Opt -eq 'y' ) { UploadAll; } Else { Write-Host "No changes were made."; Exit }
 }
Else
 {
 $PhotoPath = $DefaultPhotoPaty + $UserName + '.jpg'
 CheckPhoto;
 If ( $AbortMission -eq '$true' ) { Write-Error "Please, review the errors and try again." } Else { Import-RecipientDataProperty -Identity $UserName -Picture -FileData ([Byte[]]$(Get-Content -path $PhotoPath -Encoding Byte -ReadCount 0)) } 
 }

One thought on “SCRIPT: UploadPhoto.ps1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s