Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am writing a PowerShell script to re-sync my users to the AD because of a problem I have described Here. From time to time I get a "Set-SPUser : user cannot be found." error but when I call the same command again with the same parameters no error appears and it simply works.

I have no problem just writing this line twice so if the error occurs, it'll simply work the second time it is called or even running the script twice in a row. But I prefer to understand this problem.

The code looks like this:

$allSiteCollections = Get-SPWebApplication [Web-Application-Name]

foreach ($siteCollection in $allSiteCollections.Sites)
{
    $currentWeb = Get-SPWeb $siteCollection.url

    $members = $currentWeb.SiteGroups["Members"]

    foreach ($currentUser in $members.Users)
    {
        Set-SPUser -Identity $currentUser.UserLogin -Web $currentWeb.url -SyncFromAD -Group "Members"
    }
}

This is the line that throws the error:

Set-SPUser -Identity $currentUser.UserLogin -Web $currentWeb.url -SyncFromAD -Group "Members"

If it throws the error and I re-run it, it works perfectly the second time.

Does anybody have any idea why does this happen?

share|improve this question
    
Were you able to get this working? –  Steve Lineberry May 30 '13 at 14:16
    
Nope. I used user migrate instead... –  itaywss May 31 '13 at 16:03

1 Answer 1

up vote 1 down vote accepted

I don't know why this happens but I've seen it before. So much so that when I built my code on top of this I put a try catch in and had it do the command a second time before I was convinced that I didn't work as expected.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.