Created
March 3, 2017 11:23
-
-
Save jebbster88/c5505c7899a4d1ae51c5e379e96faf6b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add-pssnapin microsoft.sharepoint.powershell | |
$MySiteUrl = "https://mysiteurl.domain.com" | |
$DomainNamePrefix = "MYDOMAIN" | |
$strImageFolder = "\\UNC\PATH\TO\IMAGES" | |
$imageDepth = 1 #Images are in subfolders off of the specified folder, but we dont want sub-sub folders | |
$logFile = "C:\Scripts\UploadProfilePhotos.log" | |
$tempImageFolder = "C:\Scripts\Temp" | |
[int32]$height = 500 #Height in pixels to resize the image | |
$displayLogOnScreen = $true | |
function Main | |
{ | |
writeLog "Getting All Images in $strImageFolder recursively with a depth of $imageDepth" | |
$images = Get-ChildItemToDepth $strImageFolder -Filter *.jpg -ToDepth 1 | |
#Get site, web and profile manager objects | |
writeLog "Retrieving Mysite Information" | |
$mySiteHostSite = Get-SPSite $MySiteUrl | |
$mySiteHostWeb = $mySiteHostSite.OpenWeb() | |
$context = Get-SPServiceContext $mySiteHostSite | |
$spPhotosFolder = $mySiteHostWeb.GetFolder("User Photos") | |
writeLog "Connecting to User Profile Service" | |
$profileManager = New-Object Microsoft.Office.Server.UserProfiles.UserProfileManager($context) | |
$count = 0 | |
foreach ($image in $images) | |
{ | |
writeLog "Processing Image $($image.FullName.Replace($strImageFolder, ''))" | |
$username = $image.Name.Split(".")[0] | |
$adAccount = $DomainNamePrefix + "\" + $username | |
#Does the user exist | |
if ($profileManager.UserExists($adAccount)) | |
{ | |
writeLog "Found User $username in sharepoint" | |
$newImagePath = $tempImageFolder + "\" + $image.Name | |
writeLog "Scaling Down Image to $newImagePath" | |
Resize-Image $image.FullName $newImagePath $height | |
$newImage = get-item $newImagePath | |
$spFullPath = $spPhotosFolder.Url + "/" + $image.Name | |
if ($mySiteHostWeb.GetFile($spFullPath).Exists) | |
{ #Image exists, check hashes | |
writeLog "Found existing image in Sharepoint. Comparing Hashes" | |
$hash1 = (Get-FileHash -Algorithm MD5 $newImagePath).Hash | |
$hash2 = (Get-SPFileHash ($mySiteHostWeb.Url + "/" + $spFullPath)).Replace("-", "") | |
if($hash1 -eq $hash2) | |
{ #Hashes match, no need to update | |
writeLog "Hashes match, no need to upload" | |
writeLog "Deleting temp image $newImagePath" | |
remove-item $newImagePath | |
continue | |
} | |
} | |
writeLog "Uploading Image to SharePoint" | |
$newImageReader = $newImage.OpenRead() | |
$spFile = $spPhotosFolder.Files.Add($spFullPath, $newImageReader, $true) | |
$newImageReader.Dispose() | |
$spImagePath = $mySiteHostWeb.Url + "/" + $spFile.Url | |
#Update the user profile | |
writeLog "Setting users profile picture path" | |
$up = $profileManager.GetUserProfile($adAccount) | |
$up["PictureURL"].Value = $spImagePath | |
$up.Commit() | |
writeLog "Deleting temp image $newImagePath" | |
remove-item $newImagePath | |
} | |
else | |
{ | |
writeLog "User $adAccount not found in sharepoint, skipping." | |
} | |
} | |
Update-SPProfilePhotoStore -MySiteHostLocation $MySiteUrl | |
} | |
Function Get-ChildItemToDepth { | |
Param( | |
[String]$Path = $PWD, | |
[String]$Filter = "*", | |
[Byte]$ToDepth = 255, | |
[Byte]$CurrentDepth = 0, | |
[Switch]$DebugMode | |
) | |
$CurrentDepth++ | |
If ($DebugMode) { | |
$DebugPreference = "Continue" | |
} | |
Get-ChildItem $Path | %{ | |
$_ | ?{ $_.Name -Like $Filter } | |
If ($_.PsIsContainer) { | |
If ($CurrentDepth -le $ToDepth) { | |
# Callback to this function | |
Get-ChildItemToDepth -Path $_.FullName -Filter $Filter ` | |
-ToDepth $ToDepth -CurrentDepth $CurrentDepth | |
} | |
Else { | |
Write-Debug $("Skipping GCI for Folder: $($_.FullName) " + ` | |
"(Why: Current depth $CurrentDepth vs limit depth $ToDepth)") | |
} | |
} | |
} | |
} | |
function Get-SPFileHash($fileUrl) | |
{ | |
$site = New-Object Microsoft.SharePoint.SPSite $fileUrl | |
$web = $site.OpenWeb() | |
$file = $web.GetFile($fileUrl) | |
$bytes = $file.OpenBinary() | |
$md5 = New-Object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider | |
$hash = [System.BitConverter]::ToString($md5.ComputeHash($bytes)) | |
return $hash | |
} | |
function writeLog($strText) { | |
$strDate = get-date -Format s | |
$strLog = $strDate + ": " + $strText | |
if ($displayLogOnScreen) {$strLog | write-host} | |
$strLog | out-file $logFile -Append | |
} | |
Function Resize-Image($source, $dest, [int32]$newHeight) | |
{ | |
#[reflection.assembly]::LoadWithPartialName("System.Drawing") | |
Add-Type -AssemblyName "System.Drawing" | |
$OldBitmap = new-object System.Drawing.Bitmap $source | |
$ratio = $OldBitmap.Height / $OldBitmap.Width | |
[int32]$newWidth = $newHeight / $ratio | |
$NewBitmap = new-object System.Drawing.Bitmap $newWidth,$newHeight # create new bitmap | |
$g=[System.Drawing.Graphics]::FromImage($NewBitmap) | |
$g.PixelOffsetMode = [System.Drawing.Drawing2D.PixelOffsetMode]::HighQuality | |
$g.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic # use high quality resize algorythm | |
$g.DrawImage($OldBitmap, 0, 0, $newWidth, $newHeight) | |
$g.Dispose() | |
$NewBitmap.Save($dest, [System.Drawing.Imaging.ImageFormat]::Jpeg) | |
$NewBitmap.Dispose() | |
$OldBitmap.Dispose() | |
} | |
Main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment