0

I have found this nice PS1 that clone permissions from a SPUser To Other.
When give inputs Many things goes wrong but I have non idea what.

Has Anybody any idea about this issue?

    PS C:\Users\Administrator> $SourceUserID="i:0#.w|camp-appl\mossadm"
    PS C:\Users\Administrator> $TargetUserID="i:0#.w|camp-corp\00001969"
    PS C:\Users\Administrator> $WebAppURL="http://grispqm1:25093/"


   PS C:\Users\mossadm> C:\Users\mossadm\Desktop\CloneUser.PS1
Scanning Farm Administrators Group...
Scanning Web Application Policies...
Get-SPWebApplication : Cannot find an SPWebApplication object with Name, Id, or Url: http://sharepoint.crescent.com.
At C:\Users\mossadm\Desktop\CloneUser.PS1:109 char:13
+   $WebApp = Get-SPWebApplication $WebAppURL
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (Microsoft.Share...tWebApplication:SpCmdletGetWebApplication) [Get-SPWebApp
   lication], SPCmdletPipeBindException
    + FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SpCmdletGetWebApplication

Get-SPSite : Cannot find an SPWebApplication object with Name, Id, or Url: http://sharepoint.crescent.com.
At C:\Users\mossadm\Desktop\CloneUser.PS1:138 char:21
+  $SiteCollections = Get-SPSite -WebApplication $WebAppURL -Limit All
+                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (Microsoft.Share...SPCmdletGetSite:SPCmdletGetSite) [Get-SPSite], SPCmdletP
   ipeBindException
    + FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SPCmdletGetSite

Get-SPWebApplication : Cannot find an SPWebApplication object with Name, Id, or Url: http://sharepoint.crescent.com.
At C:\Users\mossadm\Desktop\CloneUser.PS1:141 char:10
+     if( (Get-SPWebApplication $WebAppURL).UseClaimsAuthentication)
+          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (Microsoft.Share...tWebApplication:SpCmdletGetWebApplication) [Get-SPWebApp
   lication], SPCmdletPipeBindException
    + FullyQualifiedErrorId : Microsoft.SharePoint.PowerShell.SpCmdletGetWebApplication

Permission are copied successfully!
Read : The term 'Read' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At C:\Users\mossadm\Desktop\CloneUser.PS1:223 char:1
+ Read more: http://www.sharepointdiary.com/2015/01/clone-sharepoint-user-permissi ...
+ ~~~~
    + CategoryInfo          : ObjectNotFound: (Read:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

THIS IS THE SP1 SOURCE

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Function to copy user permissions
Function Copy-UserPermissions($SourceUserID, $TargetUserID, [Microsoft.SharePoint.SPSecurableObject]$Object)
{
 #Determine the given Object type and Get URL of it
    Switch($Object.GetType().FullName)
 {
  "Microsoft.SharePoint.SPWeb"  { $ObjectType = "Site" ; $ObjectURL = $Object.URL; $web = $Object }
  "Microsoft.SharePoint.SPListItem"
  {
   if($Object.Folder -ne $null)
   {
     $ObjectType = "Folder" ; $ObjectURL = "$($Object.Web.Url)/$($Object.Url)"; $web = $Object.Web
   }
   else
   {
    $ObjectType = "List Item"; $ObjectURL = "$($Object.Web.Url)/$($Object.Url)" ; $web = $Object.Web
   }
  }
  #Microsoft.SharePoint.SPList, Microsoft.SharePoint.SPDocumentLibrary, Microsoft.SharePoint.SPPictureLibrary,etc
  default { $ObjectType = "List/Library"; $ObjectURL = "$($Object.ParentWeb.Url)/$($Object.RootFolder.URL)"; $web = $Object.ParentWeb }
 }

 #Get Source and Target Users
 $SourceUser = $Web.EnsureUser($SourceUserID)
 $TargetUser = $Web.EnsureUser($TargetUserID)

 #Get Permissions of the Source user on given object - Such as: Web, List, Folder, ListItem
 $SourcePermissions = $Object.GetUserEffectivePermissionInfo($SourceUser)

 #Iterate through each permission and get the details
 foreach($SourceRoleAssignment in $SourcePermissions.RoleAssignments)
 {
  #Get all permission levels assigned to User account directly or via SharePOint Group
  $SourceUserPermissions=@()
        foreach ($SourceRoleDefinition in $SourceRoleAssignment.RoleDefinitionBindings)
        {
   #Exclude "Limited Accesses"
   if($SourceRoleDefinition.Name -ne "Limited Access")
   {
          $SourceUserPermissions += $SourceRoleDefinition.Name
   }
        }

  #Check Source Permissions granted directly or through SharePoint Group
  if($SourceUserPermissions)
  {
   if($SourceRoleAssignment.Member -is [Microsoft.SharePoint.SPGroup])  
   {
    $SourcePermissionType = "'Member of SharePoint Group - " + $SourceRoleAssignment.Member.Name +"'"

    #Add Target User to the Source User's Group
    #Get the Group
    $Group = [Microsoft.SharePoint.SPGroup]$SourceRoleAssignment.Member

    #Check if user is already member of the group - If not, Add to group
    if( ($Group.Users | where {$_.UserLogin -eq $TargetUserID}) -eq $null )
    {
      #Add User to Group
      $Group.AddUser($TargetUser)
      #Write-Host Added to Group: $Group.Name
    }    
   }
   else
   {
    $SourcePermissionType = "Direct Permission"

    #Add Each Direct permission (such as "Full Control", "Contribute") to Target User
    foreach($NewRoleDefinition in $SourceUserPermissions)
    {   
      #Role assignment is a linkage between User object and Role Definition
      $NewRoleAssignment = New-Object Microsoft.SharePoint.SPRoleAssignment($TargetUser)
      $NewRoleAssignment.RoleDefinitionBindings.Add($web.RoleDefinitions[$NewRoleDefinition])

      $object.RoleAssignments.Add($NewRoleAssignment)
      $object.Update()    
    }     
   }
   $SourceUserPermissions = $SourceUserPermissions -join ";" 
   Write-Host "***$($ObjectType) Permissions Copied: $($SourceUserPermissions) at $($ObjectURL) via $($SourcePermissionType)***"
  }  
 }
}

Function Clone-SPUser($SourceUserID, $TargetUserID, $WebAppURL)
{
 ###Check Whether the Source Users is a Farm Administrator ###
 Write-host "Scanning Farm Administrators Group..."
   #Get the SharePoint Central Administration site
   $AdminWebApp = Get-SPwebapplication -includecentraladministration | where {$_.IsAdministrationWebApplication}
    $AdminSite = Get-SPWeb $AdminWebApp.Url
    $AdminGroupName = $AdminSite.AssociatedOwnerGroup
    $FarmAdminGroup = $AdminSite.SiteGroups[$AdminGroupName]

 #enumerate in farm adminidtrators groups
    foreach ($user in $FarmAdminGroup.users)
    {
     if($User.LoginName.Endswith($SourceUserID,1)) #1 to Ignore Case
     {
       #Add the target user to Farm Administrator Group
    $FarmAdminGroup.AddUser($TargetUserID,"",$TargetUserID , "")
    Write-Host "***Added to Farm Administrators Group!***"
     }    
    }

 ### Check Web Application User Policies ###
 Write-host "Scanning Web Application Policies..."
  $WebApp = Get-SPWebApplication $WebAppURL  

  foreach ($Policy in $WebApp.Policies)
  {
      #Check if the search users is member of the group
     if($Policy.UserName.EndsWith($SourceUserID,1))
       {
       #Write-Host $Policy.UserName
    $PolicyRoles=@()
       foreach($Role in $Policy.PolicyRoleBindings)
       {
        $PolicyRoles+= $Role
       }
   }
  }
  #Add Each Policy found
  if($PolicyRoles)
  {
   $WebAppPolicy = $WebApp.Policies.Add($TargetUserID, $TargetUserID)
   foreach($Policy in $PolicyRoles)
   {
    $WebAppPolicy.PolicyRoleBindings.Add($Policy)
   }
   $WebApp.Update()
   Write-host "***Added to Web application Policies!***"
  }

 ### Drill down to Site Collections, Webs, Lists & Libraries, Folders and List items ###
 #Get all Site collections of given web app
 $SiteCollections = Get-SPSite -WebApplication $WebAppURL -Limit All

 #Convert UserID Into Claims format - If WebApp is claims based! Domain\User to i:0#.w|Domain\User
    if( (Get-SPWebApplication $WebAppURL).UseClaimsAuthentication)
    {
        $SourceUserID = (New-SPClaimsPrincipal -identity $SourceUserID -identitytype 1).ToEncodedString()
  $TargetUserID = (New-SPClaimsPrincipal -identity $TargetUserID -identitytype 1).ToEncodedString()
    }

 #Loop through all site collections
    foreach($Site in $SiteCollections)
    {
  #Prepare the Target user
  $TargetUser = $Site.RootWeb.EnsureUser($TargetUserID)

     Write-host "Scanning Site Collection Administrators Group for:" $site.Url
  ###Check Whether the User is a Site Collection Administrator
     foreach($SiteCollAdmin in $Site.RootWeb.SiteAdministrators)
        {
      if($SiteCollAdmin.LoginName.EndsWith($SourceUserID,1))
      {
          #Make the user as Site collection Admin
           $TargetUser.IsSiteAdmin = $true
           $TargetUser.Update()
       Write-host "***Added to Site Collection Admin Group***"
      }    
    }

  #Get all webs
  $WebsCollection = $Site.AllWebs
  #Loop throuh each Site (web)
  foreach($Web in $WebsCollection)
  {
       if($Web.HasUniqueRoleAssignments -eq $True)
             {
     Write-host "Scanning Site:" $Web.Url

     #Call the function to Copy Permissions to TargetUser
     Copy-UserPermissions $SourceUserID $TargetUserID $Web   
    }

    #Check Lists with Unique Permissions
    Write-host "Scanning Lists on $($web.url)..."
    foreach($List in $web.Lists)
    {
              if($List.HasUniqueRoleAssignments -eq $True -and ($List.Hidden -eq $false))
                 {
      #Call the function to Copy Permissions to TargetUser
      Copy-UserPermissions $SourceUserID $TargetUserID $List
     }

     #Check Folders with Unique Permissions
     $UniqueFolders = $List.Folders | where { $_.HasUniqueRoleAssignments -eq $True }                   
                    #Get Folder permissions
                    foreach($folder in $UniqueFolders)
        {
      #Call the function to Copy Permissions to TargetUser
                                                Copy-UserPermissions $SourceUserID $TargetUserID $folder     
                    }

     #Check List Items with Unique Permissions
     $UniqueItems = $List.Items | where { $_.HasUniqueRoleAssignments -eq $True }
                    #Get Item level permissions
                    foreach($item in $UniqueItems)
        {
      #Call the function to Copy Permissions to TargetUser
      Copy-UserPermissions $SourceUserID $TargetUserID $Item 
                    }
    }
  }
 }
 Write-Host "Permission are copied successfully!"

}
#Define variables for processing
$WebAppURL = "http://sharepoint.crescent.com"

#Provide input for source and Target user Ids
$SourceUser ="Crescent\TonyW"
$TargetUser ="Crescent\Salaudeen"

#Call the function to clone user access rights
Clone-SPUser $SourceUser $TargetUser $WebAppURL


Read more: http://www.sharepointdiary.com/2015/01/clone-sharepoint-user-permissions-using-powershell.html#ixzz3U0AnNwAz
  • Error says that the web application "sharepoint.crescent.com" could not be reached. Are you sure URL "sharepoint.crescent.com" exists? Is any alternate access mapping defined? – 404 Mar 10 '15 at 20:29
  • DoI understad right or should I input the Centraladmin webapp URL? – hello B Mar 10 '15 at 20:34
  • No you do not need to put centraladmin webapp url. "sharepoint.crescent.com" is the URL you need for cloning ? or "grispqm1:25093" is the URL? – 404 Mar 10 '15 at 20:35
  • Right, grispqm1:25093/. Reading the code I thought that lines "$AdminWebApp = Get-SPwebapplication -includecentraladministration | where {$_.IsAdministrationWebApplication} $AdminSite = Get-SPWeb $AdminWebApp.Url $AdminGroupName = $AdminSite.AssociatedOwnerGroup $FarmAdminGroup = $AdminSite.SiteGroups[$AdminGroupName]", need for the url of Centraladmin . Isn't corret? – hello B Mar 10 '15 at 20:39
  • No. URL of centraladmin is not required. as this task will be automatically done my "IncludeCentralAdministration " with filter criteria you provided. "grispqm1:25093" is the URL of central admin? or the web application on which you want to perform clone? – 404 Mar 10 '15 at 20:53
0

Please verify that the web application you are looking for is "http://sharepoint.crescent.com". If not, update the script with correct web application URL at following line:

#Define variables for processing
$WebAppURL = "http://sharepoint.crescent.com"
0

I believe you need to modify the variables inside the .ps1 file.

Change the following lines at the bottom of your code instead of pre-declaring the variables. Here's what it's most likely supposed to be:

#Define variables for processing
$WebAppURL = "http://grispqm1:25093/"

#Provide input for source and Target user Ids
$SourceUser ="camp-appl\mossadm"
$TargetUser ="camp-corp\00001969"

#Call the function to clone user access rights
Clone-SPUser $SourceUser $TargetUser $WebAppURL

Once you change those lines to the ones I put above, save the file again and just run C:\Users\mossadm\Desktop\CloneUser.PS1 again, without trying to declare and variables beforehand.

Also: Please remove the Read more: http[...] at the end of the .ps1 file. This is automated text from when you copied the script from (most likely) http://www.sharepointdiary.com.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.