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 using following PS script to add users from a text file. Some of the users in the text file are inactive or gone (basically can't be resolved). The error message from the catch block is executed for all the users. If invalid user then it should display $spUser does not exists. Can anyone see what am I doing incorrect here? Just wanted to mention that valid AD users are getting added. EnsureUser is throwing exception when user not found in AD.

$webUrl = "http://server"
$web = Get-SPWeb $webUrl
$fileWithPath = "N:\Staging\Users.txt"
$content = Get-Content $fileWithPath
$spGroup = "Northwest Region Sales"
$rd="Read"

foreach($user in $content)
{    
    try
    {
        $spUser = $web.EnsureUser($user)
        if($spUser -ne $null)
        {
        Write-Host $spuser
            Write-Host "Adding " $spUser
            $web.SiteGroups[$spGroup].AddUser($spUser)
            Write-Host $user  " User added to the group successfully"
        }
        else
        {
        Write-Host $spUser " does not exists"
    }
    }
    catch
    {
        Write-Host "Error occured on " $user
    } 
}
share|improve this question
    
Nothing wrong with your code - permission issue, group doesn't exist? Try to get exception message by using $_ in catch block : Trap and Try/Catch. –  Vedran Rasol Oct 4 '11 at 20:57
    
Also try adding $spGroup = $web.SiteGroups[$spGroup] and Write-Host $spGroup to see if group is found –  Vedran Rasol Oct 4 '11 at 21:01
    
Actually, the valid users are being added to the SPGroup. It's only when a user is not found (due to deletion from AD or set to Inactive). –  Mala Singha Oct 4 '11 at 21:05
1  
I added the extra $_ to the catch block. I see this error. I thought EnsureUser will return a boolen (1/0 or true/false). I see this error for the bad users. Error occured on: Exception calling "EnsureUser" with "1" argument(s): "The specified user Domain\csantana could not be found." –  Mala Singha Oct 4 '11 at 21:08
    
I don't get it. EnsureUser throws exception if user is not found. If exception is thrown then user don't exist so you write 'does not exist' and foreach loop will continue. You can remove if statement because it is useless. –  Vedran Rasol Oct 4 '11 at 21:30
show 3 more comments

1 Answer

To summarize comments.

SpWeb.EnsureUser method will throw an exception if user account doesn't exist in Active Directory or if it is disabled.

Code provided in question could look like this (in foreach loop):

try
{
    $spUser = $web.EnsureUser($user)

    Write-Host "Adding " $spUser
    $web.SiteGroups[$spGroup].AddUser($spUser)
    Write-Host $user  " User added to the group successfully"
}
catch
{
    Write-Host $spUser " does not exists"
} 

Using try catch block is not the best option but in this case it is the only option.

Some more details with C# example on how to check AD for user can be found here:

share|improve this answer
add comment

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.