locked
Error when running CreateDefaultAssociatedGroups in powershell RRS feed

  • Question

  • I created a webapp and sitecollection from scratch using powershell.  Since the default groups are missing, I am trying to add them but I am getting an error.

    PS C:\> $sp_web.CreateDefaultAssociatedGroups("A1\admin_ck","","")
    Exception calling "CreateDefaultAssociatedGroups" with "3" argument(s): "User cannot be found."
    At line:1 char:38
    + $sp_web.CreateDefaultAssociatedGroups <<<< ("A1\admin_ck","","")
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : DotNetMethodException

    Didn't find too much info on google, so any help is welcome


    • Edited by c_kubie Monday, February 27, 2012 6:07 PM
    Monday, February 27, 2012 5:16 PM

Answers

  • If the problem really is that the user doesn't exist then this should work:

    $sp_web.EnsureUser("A1\admin_ck")
    $sp_web.CreateDefaultAssociatedGroups("A1\admin_ck","","")
     
    It will work because EnsureUser adds the user to the site when it executes.

    If this post was helpful please mark it as helpful, if it solved your problem please mark it as answered.
    Visit my Blog: http://matthewchurilla.blogspot.com/

    Monday, February 27, 2012 9:13 PM

All replies

  • I think that the default associated groups have to be sharepoint groups - your code above infers domain groups?
    Monday, February 27, 2012 8:31 PM
  • You need to specifiy all 3 parameters.  The 2nd one is the secondary contact for the group this needs to be another user, the 3rd is the prefix to the group name.  So if you specified "Blah" you'd get "Blah Visitors".

    If this post was helpful please mark it as helpful, if it solved your problem please mark it as answered.
    Visit my Blog: http://matthewchurilla.blogspot.com/


    Monday, February 27, 2012 9:05 PM
  • The problem was that the account doesnt exist in sharepoint yet.  I would then get account is in use when running it from my farm admin account.

    My workaround is to add a secondary admin account to the 'new-spsite' string.

    # Create a new Sharepoint Site Collection
    New-SPSite -URL $SiteCollectionURL -OwnerAlias $SiteCollectionOwner -SecondaryOwnerAlias $SecOwnerAlias -Language $SiteCollectionLanguage -Template $SiteCollectionTemplate -Name $SiteCollectionName

    $sp_web = Get-SPWeb $SiteCollectionURL
    $sp_web.CreateDefaultAssociatedGroups($SecOwnerAlias ,"", "")

    Monday, February 27, 2012 9:10 PM
  • If the problem really is that the user doesn't exist then this should work:

    $sp_web.EnsureUser("A1\admin_ck")
    $sp_web.CreateDefaultAssociatedGroups("A1\admin_ck","","")
     
    It will work because EnsureUser adds the user to the site when it executes.

    If this post was helpful please mark it as helpful, if it solved your problem please mark it as answered.
    Visit my Blog: http://matthewchurilla.blogspot.com/

    Monday, February 27, 2012 9:13 PM
  • Hi guys,

    I had the same issue (still not "really" resolved though), and found out something interesting.

    I am new to PowerShell scripting, so what I did is, creating each piece of code (Service Applications, Web applications, Site collections, My Sites and so on...), tested them of course, and then, put all together with minor modifications.

    When I created a site collection by PowerShell, noticed the Default Groups were not there. Then I looked on the net and found a script for adding Default SP Groups (.CreateDefaultAssociatedGroups). Fine.

    However, when I put all together, I have noticed that I always have error:

    Exception calling "CreateDefaultAssociatedGroups" with "3" argument(s): "User cannot be 
    found."
    At line:21 char:1
    + $web.CreateDefaultAssociatedGroups($Login1,$Login2,"")
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : SPException

    Even tho $Login1 and $Login2 are nothing more than SPSetup or SPFarm... And of course already in SharePoint as Managed Accounts (which were used anyway earlier in my full script)

    So following the thread here, I added $web.EnsureUser(Domain\name), still got the same error BUT I have noticed that the LoginName & UserLogin properties are: i:0#.w|mydomain\spsetup

    I then changed $Login1 = i:0#.w|mydomain\spsetup AND BOOM!! Was working...

    I am not sure why this is still showing i:0#.w| though... 

    Well, hope that helps someone in the futur.

    • Proposed as answer by Olaf V Wednesday, June 11, 2014 11:43 AM
    Tuesday, May 20, 2014 6:27 PM
  • Hi guys,

    I had the same issue (still not "really" resolved though), and found out something interesting.

    I am new to PowerShell scripting, so what I did is, creating each piece of code (Service Applications, Web applications, Site collections, My Sites and so on...), tested them of course, and then, put all together with minor modifications.

    When I created a site collection by PowerShell, noticed the Default Groups were not there. Then I looked on the net and found a script for adding Default SP Groups (.CreateDefaultAssociatedGroups). Fine.

    However, when I put all together, I have noticed that I always have error:

    Exception calling "CreateDefaultAssociatedGroups" with "3" argument(s): "User cannot be 
    found."
    At line:21 char:1
    + $web.CreateDefaultAssociatedGroups($Login1,$Login2,"")
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : SPException

    Even tho $Login1 and $Login2 are nothing more than SPSetup or SPFarm... And of course already in SharePoint as Managed Accounts (which were used anyway earlier in my full script)

    So following the thread here, I added $web.EnsureUser(Domain\name), still got the same error BUT I have noticed that the LoginName & UserLogin properties are: i:0#.w|mydomain\spsetup

    I then changed $Login1 = i:0#.w|mydomain\spsetup AND BOOM!! Was working...

    I am not sure why this is still showing i:0#.w| though... 

    Well, hope that helps someone in the futur.


    Tuesday, May 20, 2014 6:50 PM
  • In addition to my old post, if you type Get-SPUser -Web http://<your_site>

    You will see that the Farm Account is also referred as the "System Account":

    UserLogin                        DisplayName         
    ---------                            -----------         
    c:0(.s|true                       Everyone            
    i:0#.w|exams\spfarm    SPFarm              
    SHAREPOINT\system     System Account

    So if you plan on using the Farm Acct, you can directly set your variable like:
    $SPFarm = "SHAREPOINT\system"

    And it will work just fine.


    FrenchSpeaker - MCTS

    Sunday, January 11, 2015 7:46 PM
  • Maybe you should use the following instead:

    $user=$sp_web.EnsureUser("A1\admin_ck")

    $sp_web.CreateDefaultAssociatedGroups($user,"","")


    Friday, February 5, 2016 2:20 PM
  • No it was giving error even after adding EnsureUser. But works with logon user.

    sanmm

    Friday, July 29, 2016 9:37 PM