Importer profilbilleder til SharePoint med PowerShell
To PowerShell-scripts, der lægger billeder fra en lokal mappe i My Site-hostens billedbibliotek og sætter dem som brugernes profilbilleder i SharePoint 2010 og 2013.
Bedste løsning til at lave automatisk UserProfile-import til SharePoint
Opret mappen “ImportProfileImages”
Opret undermappen “ProfileImages”
Opret “c:\SPSolutions\ImportProfileImages\UpdateUserProfiles.ps1” med koden fra SharePoint Use Cases
1cls 2if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) { 3 Add-PSSnapin Microsoft.SharePoint.PowerShell; 4} 5 6#--------------------------------------------------------------------------------- 7# Default Values 8#--------------------------------------------------------------------------------- 9 10$spNotFoundMsg = "Unable to connect to SharePoint. Please verify that the site '$siteUrl' is hosted on the local machine."; 11 12#----------------------------------------------------- 13# Load Assemblies 14#----------------------------------------------------- 15 16if ([Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") -eq $null) { throw $spNotFoundMsg; } 17if ([Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server") -eq $null) { throw $spNotFoundMsg; } 18 19#----------------------------------------------------- 20# Functions 21#----------------------------------------------------- 22 23function ToSimpleString([string]$value, [bool]$trim = $true, [bool]$removeSpaces = $true, [bool]$toLower = $true) 24{ 25 if ($value -eq $null) { return [System.String]::Empty; } 26 27 if ($trim) 28 { 29 $value = $value.Trim(); 30 } 31 32 if ($removeSpaces) 33 { 34 $value = $value.Replace(" ", ""); 35 } 36 37 if ($toLower) 38 { 39 $value = $value.ToLower(); 40 } 41 42 return $value; 43} 44 45function GetSPSite($url) 46{ 47 [Microsoft.SharePoint.SPSite]$site = New-Object "Microsoft.SharePoint.SPSite" -ArgumentList $url 48 return $site; 49} 50 51function GetSPWeb($url) 52{ 53 [Microsoft.SharePoint.SPSite]$site = GetSPSite -url $url; 54 [Microsoft.SharePoint.SPWeb]$web = $site.OpenWeb(); 55 return $web 56} 57 58function GetSPList($url, $listName) 59{ 60 $listName = (ToSimpleString -value $listName); 61 62 [Microsoft.SharePoint.SPWeb]$web = GetSPWeb -url $url; 63 foreach($list in $web.Lists) 64 { 65 $title = (ToSimpleString -value $list.Title); 66 if ($listName -eq $title) 67 { 68 return $list; 69 } 70 } 71 return $null; 72} 73 74function GetSPDocumentLibrary($url, $libraryName) 75{ 76 [Microsoft.SharePoint.SPDocumentLibrary]$lib = [Microsoft.SharePoint.SPDocumentLibrary](GetSPList -url $url -listName $libraryName); 77 return $lib; 78} 79 80function GetSPFile($libraryInstance, $fileName) 81{ 82 $fileName = (ToSimpleString -value $fileName -removeSpaces $false); 83 84 foreach($file in $libraryInstance.RootFolder.Files) 85 { 86 $itemName = (ToSimpleString -value $file.Name -removeSpaces $false); 87 if ($fileName -eq $itemName) 88 { 89 return $file; 90 } 91 } 92 return $null; 93} 94 95function UploadSPFile([string]$url, [string]$libraryName, [string]$filePath, [System.Text.StringBuilder]$verbose = $null) 96{ 97 try 98 { 99 [Microsoft.SharePoint.SPDocumentLibrary]$lib = (GetSPDocumentLibrary -url $url -libraryName $libraryName); 100 if ($lib -eq $null) 101 { 102 throw (([string]'Cannot find document library "') + ([string]$libraryName) + ([string]'" at url "') + ([string]$url) + ([string]'"!')); 103 } 104 105 $bytes = [System.IO.File]::ReadAllBytes($filePath); 106 $fileName = [System.IO.Path]::GetFileName($filePath); 107 108 [Microsoft.SharePoint.SPFile]$file = GetSPFile -libraryInstance $lib -fileName $fileName; 109 110 if ($file -eq $null) 111 { 112 if ($verbose -ne $null) 113 { 114 [void]$verbose.AppendLine("Uploading File..."); 115 } 116 $file = $lib.RootFolder.Files.Add($fileName, $bytes); 117 } 118 else 119 { 120 if ($verbose -ne $null) 121 { 122 [void]$verbose.AppendLine("File Exists, overwriting..."); 123 } 124 $file.SaveBinary($bytes); 125 } 126 127 if ($verbose -ne $null) 128 { 129 [void]$verbose.AppendLine(($bytes.Length.ToString()) + ([string]" bytes written!")); 130 } 131 132 return $file; 133 } 134 catch 135 { 136 if ($verbose -ne $null) 137 { 138 [void]$verbose.AppendLine(([string]'Error: Upload to document library "') + ([string]$libraryName) + ([string]'" at "') + ([string]$url) + ([string]'" or file "') + ([string]$filePath) + ([string]'" failed!')); 139 [void]$verbose.AppendLine([string]'Error: ' + [string]$error[1]); 140 } 141 } 142 143 return $null; 144} 145 146function GetSpContext($url) 147{ 148 [Microsoft.SharePoint.SPSite]$site = GetSPSite -url $url 149 return [Microsoft.Office.Server.ServerContext]::GetContext($site); 150} 151 152function GetProfileManager($url) 153{ 154 [Microsoft.Office.Server.ServerContext]$ctx = GetSpContext -url $url 155 [Microsoft.Office.Server.UserProfiles.UserProfileManager]$upm = New-Object "Microsoft.Office.Server.UserProfiles.UserProfileManager" -ArgumentList $ctx 156 157 return $upm; 158} 159 160function GetSPUser($url, $loginName) 161{ 162 [Microsoft.SharePoint.SPWeb]$web = GetSPWeb -url $url 163 [Microsoft.SharePoint.SPUser]$user = $web.AllUsers[$loginName] 164 return $user; 165} 166 167function GetProfilePropertyName($userProfileManager, $propertyName) 168{ 169 $propertyName = (ToSimpleString -value $propertyName); 170 $propertyName = $propertyName.Replace("sps-", ""); 171 172 foreach($prop in $userProfileManager.Properties) 173 { 174 [string]$n = (ToSimpleString -value $prop.DisplayName); 175 $n = $n.Replace("sps-", ""); 176 if ($propertyName -eq $n) { return $prop.Name.ToString(); } 177 178 $n = (ToSimpleString -value $prop.Name); 179 $n = $n.Replace("sps-", ""); 180 if ($propertyName -eq $n) { return $prop.Name.ToString(); } 181 } 182 183 return $null; 184} 185 186#This function is VERY different from [System.IO.Path]::Combine 187function CombineUrls([string]$baseUrl, [string]$relUrl) 188{ 189 [System.Uri]$base = New-Object System.Uri($baseUrl, [System.UriKind]::Absolute); 190 [System.Uri]$rel = New-Object System.Uri($relUrl, [System.UriKind]::Relative); 191 192 return (New-Object System.Uri($base, $rel)).ToString(); 193} 194 195function Update-SPProfilePictures([string]$webUrl, [string]$picLibraryName, [string]$localFolderPath, [string]$domain) 196{ 197 #Get web and picture library folder that will store the pictures 198 $web = Get-SPWeb $webUrl 199 $picFolder = $web.Folders[$picLibraryName] 200 if(!$picFolder) 201 { 202 Write-Host "Picture Library Folder not found" 203 return 204 } 205 206 #Attach to local folder and enumerate through all files 207 $files = ([System.IO.DirectoryInfo] (Get-Item $localFolderPath)).GetFiles() | ForEach-Object { 208 209 $username = [IO.Path]::GetFileNameWithoutExtension($_.FullName); 210 211 #Create file stream object from file 212 $fileStream = ([System.IO.FileInfo] (Get-Item $_.FullName)).OpenRead() 213 $contents = new-object byte[] $fileStream.Length 214 $fileStream.Read($contents, 0, [int]$fileStream.Length); 215 $fileStream.Close(); 216 217 write-host "Copying" $_.Name "to" $picLibraryName "in" $web.Title "..." 218 219 #Add file 220 $spFile = $picFolder.Files.Add($picFolder.Url + "/" + $_.Name, $contents, $true) 221 $spItem = $spFile.Item 222 223 $upm = GetProfileManager -url $webUrl 224 $up = $null; 225 $up = $upm.GetUserProfile("$domain\$username"); 226 227 $picturePropertyName = GetProfilePropertyName -UserProfileManager $upm -PropertyName "PictureUrl"; 228 229 if($up -ne $null) 230 { 231 if (-not [System.String]::IsNullOrEmpty($picturePropertyName)) 232 { 233 $PortraitUrl = CombineUrls -baseUrl $spFile.Web.Url -relUrl $spFile.ServerRelativeUrl; 234 Write-Host $PortraitUrl 235 $up.get_Item($picturePropertyName).Value = $PortraitUrl; 236 $up.Commit(); 237 } 238 } 239 240 } 241 242 Write-Host "Updating User Profile Photo Store..." -foregroundcolor yellow 243 Update-SPProfilePhotoStore –MySiteHostLocation $webUrl 244 Write-Host "Done" -foregroundcolor green 245}Opret “c:\SPSolutions\ImportProfileImages\RunUpdateUserProfiles.ps1” med denne kode:
1cd C:\SPSolutions\ImportProfilePhotos 2. .\UpdateProfiles.ps1 3Update-SPProfilePictures "http://my-sites-host-url" "User Photos" "c:\SPSolutions\ImportProfileImages\ProfileImages" "Your_Domain"Gem filerne, og kopiér billederne ind i mappen “ProfileImages” med brugerens alias som filnavn.
Højreklik på “RunUpdateUserProfiles.ps1”, og vælg “Run with PowerShell”
Kilde: SharePoint Use Cases
Sidst opdateret