Wednesday, January 30, 2013

Add FBA users to the SharePoint groups reading from .CSV file



When you run this script it will ask you to provide .csv file location. I reads one by one user it will be added fba account to the SharePoint group.


=======.csv file sample data====================


winUserName,       Email,   FBAloweredusername, Groups
domain1\surya1 , surya1@xxx.com ,surya1, group1, group2, group3, group4;
domain1\surya2, surya2@xxx.com ,surya2, group1;group4;
domain1\surya3, surya3@xxx.com ,surya3, group1; group2; group3; group4;

param([string]$URL = "siteurl", [string]$InputFile=$(Read-Host -prompt "Path to CSV File containing users and groups:"))


#add-pssnapin Microsoft.SharePoint.Powershell

$site = New-Object Microsoft.SharePoint.SPSite($URL)
$web = $site.openweb("")

$file = ipcsv $InputFile

$i = 1

#logging
$timestamp = get-date -format "yyyyMMdd_hhmmtt"
$filenameStart = "MigrationLogADDUsers"
$logfile = ("{0}{1}.csv" -f $filenamestart, $timestamp)

$header = "Message,Error"
$header | out-file -FilePath $logfile

$j=1

#variables
      $winusername = $null
      $winlogin = $null
      $fbaUserName= $null
      $fbaformattedlogin = $null
      $action= $null
    $groups = $null
      $groupname = $null
     
foreach ($line in $file)
{
      #get excel sheet cell values 
    $winusername = $line.winUserName
    $action= $line.Action
      $fbaUserName = $line.FBAloweredusername
      $groups =$line.Groups
     
      #formatting fba login with membership. Replace with your provider name

    $fbaformattedlogin = "i:0#.f|dmidmemberprovider|" + $fbaUserName
   
       
       Write-Host "================= $i FBA Login "  $fbaformattedlogin "==========================================="
      $msg = ("{0},{1}" -f "==============================  $i FBA UserName: ", "$fbaformattedlogin   =================================")
      $msg | out-file -FilePath $logfile  -append
     
      $msg = ("{0},{1}" -f "User added to below groups successfully :" , "")
      $msg | out-file -FilePath $logfile  -append
                 
      Write-Host "User added to below groups successfully"
      try
            {     #differentiating group names with ; in the excel sheet (groups col)
                  $groups.Split(";") | ForEach {

# looping through groups and adding the users
                  $MyGrp = $web.SiteGroups[$_]
                 
                 
                        if ($MyGrp -ne $null)
                        {
                              Write-Host "GroupName:" $MyGrp
                              $MyGrp.AddUser($fbaformattedlogin,"","","")
                              $msg = ("{0},{1}" -f "$MyGrp" , "")
                              $msg | out-file -FilePath $logfile  -append
                             
                        }
                  }
            }
     
      Catch {

                        $msg = ("{0},{1}" -f "Error:", $_)
                        $msg | out-file -FilePath $logfile  -append
              }

      $i++
     
#     if($i -eq 3)
 #   {
#           break;
#     }

}          
           



$web.Dispose()
$site.Dispose()

3 comments:

  1. Hi, does this script actually create the FBA users too and add them in to sharepoint groups?

    ReplyDelete
    Replies
    1. follow this post to create users and groups report

      http://suryapulipati.blogspot.com/2012/11/get-all-sharepoint-users-and-groups.html

      Delete
  2. hi,this script reads users from csv file and add them to the sharepoint groups. for that these users should be in the fba database.

    as per my requirement, I have to replace all windows accounts with FBA accounts. for that I have prepared a excel report with winacct, fba account. groups.. etc.

    ReplyDelete

Followers