Search This Blog

Wednesday, 22 April 2015

Powershell - Create UserGroups from csv

<#
.SYNOPSIS
   <A brief description of the script>
.DESCRIPTION
   <A detailed description of the script>
.PARAMETER <paramName>
   <Description of script parameter>
.EXAMPLE
   <An example of using the script>
#>

$UrlSite = Read-Host "Enter destination web app url"
$global:strGroupListFileName = Read-Host "Enter the location of Groups.csv"
$LogPath = Read-Host "Enter a location for log file  :"
$LogFileName = Read-Host "Enter name for log file  :"
$FilePath = $LogPath + "\" + $LogFileName

# =========================== LOADING SHAREPOITN POWERSHELL SNAPIN FILE DETAILS =====================================
#Loading Snap in for Microsoft.Sharepoint.Powershell

$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}

if ($snapin -eq $null)
{
Write-Host "Loading SharePoint Powershell Snapin" -ForegroundColor Green
$snapin = Add-PSSnapin "Microsoft.Sharepoint.Powershell" -ErrorAction SilentlyContinue

Write-Host $snapin
}
# =============================================================================

#=========================== SharePoint DLL ==========================
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Publishing")



function write-log([string]$label, [string]$logMsg)
{
    if($logFileCreated -eq $False)
    {
        write-host "Creating log file..."
        if((Test-Path -path $LogPath) -ne $True)
        {
            write-host "Provide proper values to LogPath folder" -ForegroundColor Red
        }
        else
        {
            Add-Content -Path $FilePath -Value $logHeader
            $script:logFileCreated  = $True
            write-host "Log file created..."
        }
    }
    else
    {
        [string]$info = [System.String]::Format("[$Date] {0}: {1}",$label, $logMsg)
        Add-Content -Path $FilePath -Value $info
    }
}

function AddGroup
{
$web = get-SPWeb  $global:SiteName
$GroupName =$global:Group
if ($web.SiteGroups[$GroupName] -ne $null){

Write-Host "Group already exists!"
Break
}
else
{
    $web.SiteGroups.Add($GroupName, $web.Site.Owner, $web.Site.Owner, "Use this group to grant people read permissions to the $web site")
    $group = $web.SiteGroups[$GroupName]
    $roleAssignment = new-object Microsoft.SharePoint.SPRoleAssignment($group)
    $roleDefinition = $web.Site.RootWeb.RoleDefinitions["Read"]
    $roleAssignment.RoleDefinitionBindings.Add($roleDefinition)
    $web.RoleAssignments.Add($roleAssignment)
    write-log "Group Added"
    write-host "Group Added"

}

}

[Microsoft.SharePoint.SPSecurity]::RunWithElevatedPrivileges(
{
                Write-Host "Addition Process Started...Time - $(get-date -format HH:mm:ss)" -Foregroundcolor Green
                write-log  "Addition Process Started...Time -" "$(get-date -format HH:mm:ss)"
                $StartTime=Get-Date
                    $Site = Get-SPSite $UrlSite
            $siteUrl = $Site.Url;
                   
            $web=$site.RootWeb
                    $webUrl = $web.Url
        try
        {
                       
        Import-CSV -Path $global:strGroupListFileName   | ForEach-Object {
                       
                                        if($_.UserGroup -ne $null -and $_.UserGroup -ne "")
                                        {
                                            $global:SiteName = $webUrl
                                            $global:Group=$_.UserGroup
                                            write-log "Site Group"
                                            write-log $global:Group  
                                            write-host "Site Group"
                                            write-host $global:Group                                          
                                            AddGroup
                                           
                                        }
                                   }
                                   $web.update();
        }
        finally
        {
        #$web.Dispose();
        }
           
})

No comments:

Post a Comment