locked
SharePoint PowerShell - need to adapt a script so that it runs on every web in every site for a specified web application RRS feed

  • General discussion

  • Please help - this script runs on a single site collection and checks that the default groups are present and creates them if not - but we have hundreds in sites in our extranet web app - How can I adapt it so that it goes - for this web app - for every Site collection - for every web in every site collection - do the stuff

    asnp *sharepoint*
    
    #Get a site collection:
    $s1 = Get-SPSite http://sp16/sites/eWiki
    
    #To list all webs and their associated groups run:
    foreach ($w1 in $s1.AllWebs)
    {
        $w1.Url #Just for sake of knowing where we are
        $w1.AssociatedMemberGroup | select Name
        $w1.AssociatedVisitorGroup | select Name
        $w1.AssociatedOwnerGroup | select Name
    }
    
    #To list all webs with at least one associated group empty reference run:
    foreach ($w1 in $s1.AllWebs)
    {
    if ($w1.AssociatedMemberGroup -eq $null -or $w1.AssociatedVisitorGroup -eq $null -or $w1.AssociatedOwnerGroup -eq $null)
        {
        $w1.Url #Just for sake of knowing where we are
        $w1.AssociatedMemberGroup
        $w1.AssociatedVisitorGroup
        $w1.AssociatedOwnerGroup
        }
    }
    
    #To add association groups to webs missing them run:
    foreach ($w1 in $s1.AllWebs)
    {
    if ($w1.AssociatedMemberGroup -eq $null -or $w1.AssociatedVisitorGroup -eq $null -or $w1.AssociatedOwnerGroup -eq $null)
        {
        $w1.Url #Just for sake of knowing where we are
        
        $w1.AssociatedMemberGroup
        $w1.AssociatedVisitorGroup
        $w1.AssociatedOwnerGroup
        [string]$User1 = "spdev\Administrator" #Group Owner, mandatory
        [string]$User2 = "" #Group Contact
        [string]$Name = "" #Group Name if not default
    
        $w1.CreateDefaultAssociatedGroups($User1,$User1,$Name) #This creates 3 new groups under the form of “[Site title] Members”, “[Site title] Visitors”, “[Site title] Owners” and associates them as default groups
        
        #Alternatively assign existing groups manually, choose from $w1.AssociatedGroups
    
        }  
    }
    
    

    Thanks!

    Thursday, November 28, 2019 12:35 PM

All replies

  • Are you asking someone to rewrite a script you have found on the Internet. This forum does not write scripts on request.

    Please carefully review the following links to set your expectation for posting in technical forums.


    \_(ツ)_/

    Thursday, November 28, 2019 6:31 PM
  • No I'm not asking for a rewrite - the script works as is - but it works at a site collection level - I need to work out how to scope it to a web application level so that it runs on every web in every site in the specified Web application
    Friday, November 29, 2019 6:58 AM
  • Get the web and enumerate the sites. If you wrote the script then you already know how to do that.

    In all cases we cannot customize scripts you have found on the Internet. 


    \_(ツ)_/


    • Edited by jrv Friday, November 29, 2019 7:07 AM
    Friday, November 29, 2019 7:06 AM
  • OK - I think this will work - but I currently don't have a DEV environment to test on- hence the question.

    asnp *sharepoint*
    
    $wburl = "http://extranet"
    $webApp = Get-SPWebApplication $wburl
    
    {
        # Get All  Site Collections in the Web Application
    
        foreach ($spSite in $webApp.Sites) {
    
            # Get the SubSites/Webs in the Site Collection
            foreach($spWeb in $SPsite.AllWebs) 
    
    {
        $w1.Url 
        $w1.AssociatedMemberGroup | select Name
        $w1.AssociatedVisitorGroup | select Name
        $w1.AssociatedOwnerGroup | select Name
    }
    
    }
    
    }
    

    Friday, November 29, 2019 7:28 AM
  • It is a guess based on no understanding of PowerShell and it is a bad guess.  Start here to learn basic PowerShell.

    https://www.sapien.com/books_training/Windows-PowerShell-4

    The PowerShell Best Practices and Style Guide

    The basics will help you start and build a scrpt and it will help you to see how the syntax works.  YOu cannot guess at this.


    \_(ツ)_/


    • Edited by jrv Friday, November 29, 2019 7:35 AM
    Friday, November 29, 2019 7:34 AM
  • I also recommend that you learn SharePoint along with PowerShell. Before you can solve your problem you need to answer other questions about you SharePoint sites and servers.

    The script that you posted does exactly what you asked and some other things too.  Your guess is just a bad edit of the original post.

    Start here to learn SharePoint commands:

    https://docs.microsoft.com/en-us/powershell/module/sharepoint-server/?view=sharepoint-ps

    To write scripts you almost always have to know both PowerShell and the technology you want to work with.  The path is to take a task.  Understand how we use PowerShell for any task and look up the docs for any modules you need to use.  All of us do that.  That is what a technician does.  After you have done that then ask any questions you need to understand issues with implementation.

    For SharePoint issues you would post in the SharePoint forums as they are the owners of the SP module and snapin.

    Since you do not know much about this I recommend working with your sites and the CmdLets and learn how they work and if you have enough information and permissions to access your SP.

    You have many questions to discover and answer before even trying to write a scrip.


    \_(ツ)_/

    Friday, November 29, 2019 7:49 AM
  • Hi jonjames.

    If you want to loop through all the site collections (and through all the webs within a site collection) of a specific web application you have to write a PowerShell snippet like the one below

    Add-PSSnapIn Microsoft.SharePoint.PowerShell
    
    # Get all the available site collections in the farm
    $sites = Get-SPWebApplication http://sharepointweb | Get-SPSite -Limit ALL
    
    # Loop through all the site collections
    ForEach ($site In $sites)
    {
      # Do some stuff with the site collection
      ...
    
      # Loop through all the webs within the site collection
      ForEach ($web In $site.AllWebs)
      {
        # Do some stuff with the web
        ...
        $web.Dispose()
      }
    
      $site.Dispose()
    }

    Bye.


    Luigi Bruno
    MCP, MOS, MTA, MCTS, MCSA, MCSE



    Friday, November 29, 2019 8:33 AM