2

I have never worked with Powershell or Sharepoint before, trying to help a friend, so my mistake might be really obvious, I apologise in advance.

So what I'm trying to do here is: -loop through all user profiles and... -loop through my already existing list in a nested foreach loop -compare DisplayName of current user to current list item's "Employee" field. If there's a match, update every field of that list item. -if we looped through the whole list and there was no match, add a new list item.

This is what I found and helped me a lot:

http://mysharepointwork.blogspot.no/2010/09/addupdatedelete-list-items-using.html

Here is my code:

#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()

-The adding new item part seems to work, but not the update one. I assume,either my if statement or my second for loop is the problem.

Every help is appreciated and/or feedback on how to post questions more efficiently. Stack Exchange rookie alert. Thank you all!

TietjeDK
  • 1,167
  • 2
  • 15
  • 43
Nora
  • 21
  • 5
  • 3
    Have you debugged it in Powershell ISE? It's easy - just set a breakpoint (F9 key at a code line) and run the script, then inspect variables after each step (F10 key). It's probable that `$SPItem["Employee"]` is not a string but a compound object with properties, in which case you'll need to use one of them. Also there's no need to surround `break` with braces. – wOxxOm Aug 26 '16 at 14:06
  • @wOxxOm I will only be able to work on this again later with my friend, but looked into your suggestion re. SPItem[Employee]. The more I look at this code, the more confused I get, now I think there's some logical error also, so I edited and clarified what exactly I was aiming to do here. Either way, I will work on this more and get back here. Thanks for taking the time! – Nora Aug 26 '16 at 18:49
  • You will create a new item no matter what, because your new-item function is triggered for each user-profile, even if there is a match. Your are only breaking the inner for-loop, not the outer loop. I would suggest using Powershell ISE and `write-host` to see the output in the console to see whats going on. – TietjeDK May 29 '17 at 11:25

0 Answers0