December 27, 2012

Powershell - Remove User account permissions from all sites

Use Cases:
When you create a site with unique permissions and break role inheritance at list or list item level, sharepoint will automatically add SHAREPOINT\System or site collection admin directly to Site Permissions. Here is the script that you can use to remove those users.
How​
 function RemoveAccountFromAllSites ($siteURL, $accountName, [switch]$skipRootSite)
 {
    #Get Site Collection
    $site = Get-SPSite $siteURL
 
    #Check if the accountName variable contains a slash - if so, it is an AD account
    #If not, it is a SharePoint Group
    $rootWeb = $site.RootWeb
    if ($accountName.Contains("\")) { $account = $rootWeb.EnsureUser($accountName) }
    else { $account = $rootWeb.SiteGroups[$accountName] }
 
    $rootWeb.Dispose()
    
    #Step through each site in the site collection
    $site | Get-SPWeb -limit all | ForEach-Object {
        
        #Check if the user has chosen to skip the root site - if so, do not change permissions on it
 
        if (($skipRootSite) -and ($site.Url -eq $_.Url)) { write-host "
Root site" $_.Url "will be bypassed" }
        else {
            #Check if the current site is inheriting permissions from its parent
            #If not, remove permissions on current site
            if ($_.HasUniqueRoleAssignments) {
                #write-host "
Removing account" $accountName "from site" $_.Url
                $_.RoleAssignments.Remove($account)
            }
            else {
                write-host "
Site" $_.Url "will not be modified as it inherits permissions from a parent site."
            }
        }
    }
    #Display completion message and dispose of site object
    #write-host "
Operation Complete."
    $site.Dispose()
}
Conclusion:
Script will check if site has unique permissions or not. Skips and don't do anything if site is not having unique permissions. Does what is expected if site is having unique permissions.
If you have any questions you can reach out our SharePoint Consulting team here.

Powershell - Creating log files for installation package

Use case:
It is general practice that we deliver wsp and powershell script to install it.
Why:
it is good practice to log whatever we show in screen to file so that it will be easy for sharepoint admin to send the result of installation.
How:

 # Set Execution Policy 
Set-ExecutionPolicy unrestricted
 
$date     = Get-Date
#read parameters from command line
$CurrentDir= Split-Path -parent $MyInvocation.MyCommand.Definition 
 
$timeStamp = (Get-Date).ToString("yyyyMMddhhmmss") 
$logFile = $CurrentDir+ "\install_" + $timeStamp + ".log"
start-transcript -path $logFile -noclobber 
 
#add this at end of script
Stop-Transcript 
#this is to show stop-transcript 
Remove-PsSnapin Microsoft.SharePoint.PowerShell    
Conclusion:​
$CurrentDir will get installation folder. Start-Transcript is powershell command to copy whatever message printed to console to file provided in parameter.
You have to place your installation script between start and stop transcript commands and everytime you execute the script, it will generate a separate log file that will have all details.

If you have any questions you can reach out our SharePoint Consulting team here.

Powershell - Add newly added field to all views


#Add-PSSnapin Microsoft.SharePoint.PowerShell
cls
$web = Get-SPWeb -Identity "url of web"
#list of views which needs to be changed
$views = @("All Documents","View1","View2")

$list = $web.GetList("ListUrl")
foreach($vwName in $views)
{
    $vw = $list.Views[$vwName]
    #Add new field
    $vw.ViewFields.Add("fieldName")
        #position is 0 based
    $vw.ViewFields.MoveFieldTo("fieldName",2)
    
    $vw.Update()
    Write-Host $vwName, "Updated"
}
 
If you have any questions you can reach out our SharePoint Consulting team here.