我以前从未使用过 Powershell 或 Sharepoint,试图帮助朋友,所以我的错误可能非常明显,我提前道歉。
所以我在这里要做的是: - 遍历所有用户配置文件和... - 在嵌套的 foreach 循环中遍历我已经存在的列表 - 将当前用户的 DisplayName 与当前列表项的“Employee”字段进行比较。如果有匹配项,则更新该列表项的每个字段。- 如果我们遍历整个列表并且没有匹配项,则添加一个新的列表项。
这是我发现并帮助我很多的:
http://mysharepointwork.blogspot.no/2010/09/addupdatedelete-list-items-using.html
这是我的代码:
#Add SharePoint PowerShell SnapIn if not already added
if ((Get-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null) {
Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
$site = new-object Microsoft.SharePoint.SPSite("http://sp2016:85/");
$ServiceContext = [Microsoft.SharePoint.SPServiceContext]::GetContext($site);
#Get UserProfileManager from the My Site Host Site context
$ProfileManager = new-object Microsoft.Office.Server.UserProfiles.UserProfileManager($ServiceContext)
$AllProfiles = $ProfileManager.GetEnumerator()
#Open SharePoint List
$SPServer="http://sp2016:85/"
$SPAppList="/Lists/UPList2/"
$spWeb = Get-SPWeb $SPServer
$spData = $spWeb.GetList($SPAppList)
foreach($profile in $AllProfiles)
{
#Add properties to this list item
$DisplayName = $profile.DisplayName
$Office = $profile[[Microsoft.Office.Server.UserProfiles.PropertyConstants]::Office].Value
$WorkPhone = $profile[[Microsoft.Office.Server.UserProfiles.PropertyConstants]::WorkPhone].Value
foreach($Items in $spData)
{
$SPItem = $spData.Items
if ($DisplayName -match $SPItem["Employee"] )
{
$SPItem["Office"] = $Office
$SPItem["Extension"] = $WorkPhone
$SPItem.Update()
{break}
}
}
#Create a new item
$newItem = $spData.Items.Add()
#Fill
$newItem["Employee"] = $DisplayName
$newItem["Office"] = $Office
$newItem["Extension"] = $WorkPhone
write-host "Profile for account ", $DisplayName
$newItem.Update()
}
write-host "Finished."
$site.Dispose()
- 添加新项目部分似乎有效,但不是更新部分。我认为,我的 if 语句或我的第二个 for 循环都是问题所在。
感谢每一个帮助和/或关于如何更有效地发布问题的反馈。Stack Exchange 菜鸟警报。谢谢你们!