批量上传用户图片Powershell脚本-跳过现有照片. [英] Bulk upload User Pictures Powershell Script - Skip existing photos..
问题描述
你好.我有一个客户的当前工作流程.当某人加入公司时,将拍摄一张头像并将其放到安全团队的共享文件夹中.图片文件被命名为与用户登录相同的ID.
Hello. I have a current working process for a client. When someone joins the company, a headshot is taken and is placed in a shared folder by security team. The picture file is named the same ID of the user log on.
该脚本每月运行一次,但是我需要对其进行修改,以便如果头像ID与现有照片库的名称匹配,则该脚本应跳过该用户的上传.我已经尝试了很多方法来使其正常运行,但是不能,任何人都可以帮助修改 这个脚本好吗?
On a monthly basis, this script runs, but I need it modified so that IF the headshot ID matches the name of the existing photo library, then it should skip the upload for this user. I have tried many ways to get it working but cannot, can anyone help modify this script please?
cls
if((Get-PSSnapin |其中{$ _.Name -eq" Microsoft.SharePoint.PowerShell"}})-eq $ null){
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}
if((Get-PSSnapin | Where {$_.Name -eq "Microsoft.SharePoint.PowerShell"}) -eq $null) {
Add-PSSnapin Microsoft.SharePoint.PowerShell;
}
$ environment ="$ env:userdomain"
$environment ="$env:userdomain"
#---------------------------------------------- -----------------------------------
#默认值
#------------------------------------------------- --------------------------------
#---------------------------------------------------------------------------------
# Default Values
#---------------------------------------------------------------------------------
$ spNotFoundMsg =无法连接到SharePoint.请验证站点"$ siteUrl"是否托管在本地计算机上.";
$spNotFoundMsg = "Unable to connect to SharePoint. Please verify that the site '$siteUrl' is hosted on the local machine.";
#---------------------------------------------- -------
#加载装配体
#------------------------------------------------- ----
#-----------------------------------------------------
# Load Assemblies
#-----------------------------------------------------
if([Reflection.Assembly] :: LoadWithPartialName("Microsoft.SharePoint")-eq $ null) {抛出$ spNotFoundMsg; }
如果([Reflection.Assembly] :: LoadWithPartialName("Microsoft.Office.Server")-eq $ null) {抛出$ spNotFoundMsg; }
if ([Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") -eq $null) { throw $spNotFoundMsg; }
if ([Reflection.Assembly]::LoadWithPartialName("Microsoft.Office.Server") -eq $null) { throw $spNotFoundMsg; }
#---------------------------------------------- -------
#函数
#------------------------------------------------- ----
#-----------------------------------------------------
# Functions
#-----------------------------------------------------
function ToSimpleString([string] $ value,[bool] $ trim = $ true,[bool] $ removeSpaces = $ true,[bool] $ toLower = $ true)
{
if($ value -eq $ null){返回[System.String] :: Empty; }
function ToSimpleString([string]$value, [bool]$trim = $true, [bool]$removeSpaces = $true, [bool]$toLower = $true)
{
if ($value -eq $null) { return [System.String]::Empty; }
如果($ trim)
{
$ value = $ value.Trim();
}
if ($trim)
{
$value = $value.Trim();
}
如果($ removeSpaces)
{
$ value = $ value.Replace(","");
}
if ($removeSpaces)
{
$value = $value.Replace(" ", "");
}
如果($ toLower)
{
$ value = $ value.ToLower();
}
if ($toLower)
{
$value = $value.ToLower();
}
返回$ value;
}
return $value;
}
函数GetSPSite($ url)
{
[Microsoft.SharePoint.SPSite] $ site =新对象"Microsoft.SharePoint.SPSite"; -ArgumentList $ url
返回$ site;
}
function GetSPSite($url)
{
[Microsoft.SharePoint.SPSite]$site = New-Object "Microsoft.SharePoint.SPSite" -ArgumentList $url
return $site;
}
函数GetSPWeb($ url)
{
[Microsoft.SharePoint.SPSite] $ site = GetSPSite -url $ url;
[Microsoft.SharePoint.SPWeb] $ web = $ site.OpenWeb();
返回$ web
}
function GetSPWeb($url)
{
[Microsoft.SharePoint.SPSite]$site = GetSPSite -url $url;
[Microsoft.SharePoint.SPWeb]$web = $site.OpenWeb();
return $web
}
函数GetSPList($ url,$ listName)
{
$ listName =(ToSimpleString -value $ listName);
function GetSPList($url, $listName)
{
$listName = (ToSimpleString -value $listName);
[Microsoft.SharePoint.SPWeb] $ web = GetSPWeb -url $ url;
foreach($ web.Lists中的$ list)
{
$ title =(ToSimpleString -value $ list.Title);
如果($ listName -eq $ title)
{
返回$ list;
}
}
返回$ null;
}
[Microsoft.SharePoint.SPWeb]$web = GetSPWeb -url $url;
foreach($list in $web.Lists)
{
$title = (ToSimpleString -value $list.Title);
if ($listName -eq $title)
{
return $list;
}
}
return $null;
}
函数GetSPDocumentLibrary($ url,$ libraryName)
{
[Microsoft.SharePoint.SPDocumentLibrary] $ lib = [Microsoft.SharePoint.SPDocumentLibrary](GetSPList -url $ url -listName $ libraryName);
返回$ lib;
}
function GetSPDocumentLibrary($url, $libraryName)
{
[Microsoft.SharePoint.SPDocumentLibrary]$lib = [Microsoft.SharePoint.SPDocumentLibrary](GetSPList -url $url -listName $libraryName);
return $lib;
}
函数GetSPFile($ libraryInstance,$ fileName)
{
$ fileName =(ToSimpleString -value $ fileName -removeSpaces $ false);
function GetSPFile($libraryInstance, $fileName)
{
$fileName = (ToSimpleString -value $fileName -removeSpaces $false);
foreach($ libraryInstance.RootFolder.Files中的$ file)
{
$ itemName =(ToSimpleString -value $ file.Name -removeSpaces $ false);
如果($ fileName -eq $ itemName)
{
返回$ file;
}
}
返回$ null;
}
foreach($file in $libraryInstance.RootFolder.Files)
{
$itemName = (ToSimpleString -value $file.Name -removeSpaces $false);
if ($fileName -eq $itemName)
{
return $file;
}
}
return $null;
}
function UploadSPFile([string] $ url,[string] $ libraryName,[string] $ filePath,[System.Text.StringBuilder] $ verbose = $ null)
{
试试
{
[Microsoft.SharePoint.SPDocumentLibrary] $ lib =(GetSPDocumentLibrary -url $ url -libraryName $ libraryName);
如果($ lib -eq $ null)
{
抛出(([[string]'无法找到文档库''')+([string] $ libraryName)+([string]'''指向网址``')+([string] $ url)+([string] '" ;!'));
}
function UploadSPFile([string]$url, [string]$libraryName, [string]$filePath, [System.Text.StringBuilder]$verbose = $null)
{
try
{
[Microsoft.SharePoint.SPDocumentLibrary]$lib = (GetSPDocumentLibrary -url $url -libraryName $libraryName);
if ($lib -eq $null)
{
throw (([string]'Cannot find document library "') + ([string]$libraryName) + ([string]'" at url "') + ([string]$url) + ([string]'"!'));
}
$ bytes = [System.IO.File] :: ReadAllBytes($ filePath);
$ fileName = [System.IO.Path] :: GetFileName($ filePath);
$bytes = [System.IO.File]::ReadAllBytes($filePath);
$fileName = [System.IO.Path]::GetFileName($filePath);
[Microsoft.SharePoint.SPFile] $ file = GetSPFile -libraryInstance $ lib -fileName $ fileName;
[Microsoft.SharePoint.SPFile]$file = GetSPFile -libraryInstance $lib -fileName $fileName;
如果($ file -eq $ null)
{
如果($ verbose -ne $ null)
{
[void] $ verbose.AppendLine("Uploading File ...");
}
$ file = $ lib.RootFolder.Files.Add($ fileName,$ bytes);
}
其他
{
如果($ verbose -ne $ null)
{
[void] $ verbose.AppendLine(文件已存在,正在覆盖...");
}
$ file.SaveBinary($ bytes);
}
if ($file -eq $null)
{
if ($verbose -ne $null)
{
[void]$verbose.AppendLine("Uploading File...");
}
$file = $lib.RootFolder.Files.Add($fileName, $bytes);
}
else
{
if ($verbose -ne $null)
{
[void]$verbose.AppendLine("File Exists, overwriting...");
}
$file.SaveBinary($bytes);
}
如果($ verbose -ne $ null)
{
[void] $ verbose.AppendLine(($ bytes.Length.ToString())+([[string]"写入的字节!"));
}
if ($verbose -ne $null)
{
[void]$verbose.AppendLine(($bytes.Length.ToString()) + ([string]" bytes written!"));
}
返回$ file;
}
赶上
{
如果($ verbose -ne $ null)
{
[void] $ verbose.AppendLine(([[string]'Error:Upload to document library''')+([string] $ libraryName)+([string]'''in';')+([string] $ url)+([string]'''或
文件``'')+([string] $ filePath)+([string]'''失败!'));
[void] $ verbose.AppendLine([string]'Error:'+ [string] $ error [1]);
}
}
return $file;
}
catch
{
if ($verbose -ne $null)
{
[void]$verbose.AppendLine(([string]'Error: Upload to document library "') + ([string]$libraryName) + ([string]'" at "') + ([string]$url) + ([string]'" or
file "') + ([string]$filePath) + ([string]'" failed!'));
[void]$verbose.AppendLine([string]'Error: ' + [string]$error[1]);
}
}
返回$ null;
}
return $null;
}
函数GetSpContext($ url)
{
[Microsoft.SharePoint.SPSite] $ site = GetSPSite -url $ url
返回[Microsoft.Office.Server.ServerContext] :: GetContext($ site);
function GetSpContext($url)
{
[Microsoft.SharePoint.SPSite]$site = GetSPSite -url $url
return [Microsoft.Office.Server.ServerContext]::GetContext($site);
}
}
函数GetProfileManager($ url)
{
[Microsoft.Office.Server.ServerContext] $ ctx = GetSpContext -url $ url
[Microsoft.Office.Server.UserProfiles.UserProfileManager] $ upm =新对象"Microsoft.Office.Server.UserProfiles.UserProfileManager"; -ArgumentList $ ctx
function GetProfileManager($url)
{
[Microsoft.Office.Server.ServerContext]$ctx = GetSpContext -url $url
[Microsoft.Office.Server.UserProfiles.UserProfileManager]$upm = New-Object "Microsoft.Office.Server.UserProfiles.UserProfileManager" -ArgumentList $ctx
返回$ upm;
}
return $upm;
}
函数GetSPUser($ url,$ loginName)
{
[Microsoft.SharePoint.SPWeb] $ web = GetSPWeb -url $ url
[Microsoft.SharePoint.SPUser] $ user = $ web.AllUsers [$ loginName]
返回$ user;
}
function GetSPUser($url, $loginName)
{
[Microsoft.SharePoint.SPWeb]$web = GetSPWeb -url $url
[Microsoft.SharePoint.SPUser]$user = $web.AllUsers[$loginName]
return $user;
}
函数GetProfilePropertyName($ userProfileManager,$ propertyName)
{
$ propertyName =(ToSimpleString -value $ propertyName);
$ propertyName = $ propertyName.Replace("sps-","));
function GetProfilePropertyName($userProfileManager, $propertyName)
{
$propertyName = (ToSimpleString -value $propertyName);
$propertyName = $propertyName.Replace("sps-", "");
foreach($ userProfileManager.Properties中的$ prop)
{
[string] $ n =(ToSimpleString -value $ prop.DisplayName);
$ n = $ n.Replace("sps-","));
如果($ propertyName -eq $ n){返回$ prop.Name.ToString(); }
foreach($prop in $userProfileManager.Properties)
{
[string]$n = (ToSimpleString -value $prop.DisplayName);
$n = $n.Replace("sps-", "");
if ($propertyName -eq $n) { return $prop.Name.ToString(); }
$ n =(ToSimpleString -value $ prop.Name);
$ n = $ n.Replace("sps-","));
如果($ propertyName -eq $ n){返回$ prop.Name.ToString(); }
}
$n = (ToSimpleString -value $prop.Name);
$n = $n.Replace("sps-", "");
if ($propertyName -eq $n) { return $prop.Name.ToString(); }
}
返回$ null;
}
return $null;
}
#此功能与[System.IO.Path] :: Combine完全不同
function CombineUrls([string] $ baseUrl,[string] $ relUrl)
{
[System.Uri] $ base =新对象System.Uri($ baseUrl,[System.UriKind] :: Absolute);
[System.Uri] $ rel =新对象System.Uri($ relUrl,[System.UriKind] :: Relative);
#This function is VERY different from [System.IO.Path]::Combine
function CombineUrls([string]$baseUrl, [string]$relUrl)
{
[System.Uri]$base = New-Object System.Uri($baseUrl, [System.UriKind]::Absolute);
[System.Uri]$rel = New-Object System.Uri($relUrl, [System.UriKind]::Relative);
return(New-Object System.Uri($ base,$ rel)).ToString();
}
return (New-Object System.Uri($base, $rel)).ToString();
}
函数Update-SPProfilePictures([string] $ webUrl,[string] $ picLibraryName,[string] $ localFolderPath,[string] $ domain)
{
#获取用于存储图片的网络和图片库文件夹
$ web = Get-SPWeb $ webUrl
$ picFolder = $ web.Folders [$ picLibraryName]
if(!$ picFolder)
{
写主机找不到图片库文件夹"
返回
}
function Update-SPProfilePictures([string]$webUrl, [string]$picLibraryName, [string]$localFolderPath, [string]$domain)
{
#Get web and picture library folder that will store the pictures
$web = Get-SPWeb $webUrl
$picFolder = $web.Folders[$picLibraryName]
if(!$picFolder)
{
Write-Host "Picture Library Folder not found"
return
}
#附加到本地文件夹并枚举所有文件
$ files =([System.IO.DirectoryInfo](Get-Item $ localFolderPath)).GetFiles()| ForEach-Object {
#Attach to local folder and enumerate through all files
$files = ([System.IO.DirectoryInfo] (Get-Item $localFolderPath)).GetFiles() | ForEach-Object {
$ username = [IO.Path] :: GetFileNameWithoutExtension($ _.FullName);
$username = [IO.Path]::GetFileNameWithoutExtension($_.FullName);
#从文件创建文件流对象
$ fileStream =([[System.IO.FileInfo](Get-Item $ _.FullName)).OpenRead()
$ contents =新对象字节[] $ fileStream.Length
$ fileStream.Read($ contents,0,[int] $ fileStream.Length);
$ fileStream.Close();
#Create file stream object from file
$fileStream = ([System.IO.FileInfo] (Get-Item $_.FullName)).OpenRead()
$contents = new-object byte[] $fileStream.Length
$fileStream.Read($contents, 0, [int]$fileStream.Length);
$fileStream.Close();
写主机正在复制". $ _.名称至" $ picLibraryName在" $ web.Title``...''
write-host "Copying" $_.Name "to" $picLibraryName "in" $web.Title "..."
#添加文件
$ spFile = $ picFolder.Files.Add($ picFolder.Url +``/''+ $ _.Name,$ contents,$ true)
$ spItem = $ spFile.Item
#Add file
$spFile = $picFolder.Files.Add($picFolder.Url + "/" + $_.Name, $contents, $true)
$spItem = $spFile.Item
$ upm = GetProfileManager -url $ webUrl
$ up = $ null;
$ up = $ upm.GetUserProfile("$ domain \ $ username");
$upm = GetProfileManager -url $webUrl
$up = $null;
$up = $upm.GetUserProfile("$domain\$username");
$ picturePropertyName = GetProfilePropertyName -UserProfileManager $ upm -PropertyName" PictureUrl" ;;
$picturePropertyName = GetProfilePropertyName -UserProfileManager $upm -PropertyName "PictureUrl";
if($ up -ne $ null)
{
if(-不是[System.String] :: IsNullOrEmpty($ picturePropertyName))
{
nbsp; bsp $ PortraitUrl = CombineUrls -baseUrl $ spFile.Web.Url -relUrl $ spFile.ServerRelativeUrl;
nbsp; bsp 写主机$ PortraitUrl
nbsp; bsp $ up.get_Item($ picturePropertyName).Value = $ PortraitUrl;
nbsp; bsp $ up.Commit();
}
}
if($up -ne $null)
{
if (-not [System.String]::IsNullOrEmpty($picturePropertyName))
{
$PortraitUrl = CombineUrls -baseUrl $spFile.Web.Url -relUrl $spFile.ServerRelativeUrl;
Write-Host $PortraitUrl
$up.get_Item($picturePropertyName).Value = $PortraitUrl;
$up.Commit();
}
}
}
}
写主机正在更新用户个人资料照片存储..."; -前景色黄色
Update-SPProfilePhotoStore –MySiteHostLocation $ webUrl
写主机完成". -forecolorcolor绿色
}
Write-Host "Updating User Profile Photo Store..." -foregroundcolor yellow
Update-SPProfilePhotoStore –MySiteHostLocation $webUrl
Write-Host "Done" -foregroundcolor green
}
Update-SPProfilePictures" http://sharepoint.$environment.local/profiles " 用户照片" " E:\ Headshots" "$ environment"
Update-SPProfilePictures "http://sharepoint.$environment.local/profiles" "User Photos" "E:\Headshots" "$environment"
推荐答案
您好uzziwozzi,
Hi uzziwozzi,
从您发布的脚本中,我注意到有一段脚本:
From the script you post, I notice there is a piece of script:
if (
冗长-ne
null) { [void]
null) { [void]
这篇关于批量上传用户图片Powershell脚本-跳过现有照片.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!