Introduction

Consider a scenario where you need to add users to the user policy for multiple web applications in multiple Farms. PowerShell is useful for that. In this article I will outline how to add users or groups to a SharePoint web application user's policy.

Preparation

Before executing the script you must first identify the users and groups that need to be added to multiple web applications in the SharePoint Farm. Group the web application list into a text file (WebapplicationList.txt). The script parses through this input file and adds the users or groups to the user policy of each web application in the list.

Functionality

The script provides options to do the following tasks:

  1. Grant FULL CONTROL access
  2. Grant FULL READ access
  3. DENY WRITE
  4. DENY ALL

Function 1

The following piece of code helps you to provide “FULL CONTROL” access:

  1. Function FullControl()
  2. {
  3. $UserOrGroup = read-host "Enter the user or group for which you want to apply FULL CONTROL (e.g domain\user) "
  4. write-host "Place the WebapplicationList.txt file under the folder where the script exists" -fore Magenta
  5. $Didyouplacethefile = read-host "Did you place the WebapplicationList.txt file under the folder where the script exists (y/n)?"
  6. if($Didyouplacethefile -eq 'y')
  7. {
  8. $testpath = Test-path -path $scriptbase\WebapplicationList.txt
  9. if($testpath)
  10. {
  11. foreach($webapplication in get-content "$scriptbase\WebapplicationList.txt")
  12. {
  13. $webapp = get-spwebapplication $webapplication
  14. write-host "Adding user or group " $userorgroup " to the webapplication " $webapplication "user policy and providing FULL CONTROL access" -fore yellow
  15. $policy = $webApp.Policies.Add($userOrGroup, $userOrGroup)
  16. $policyRole = $webApp.PolicyRoles.GetSpecialRole([Microsoft.SharePoint.Administration.SPPolicyRoleType]::FullControl)
  17. $policy.PolicyRoleBindings.Add($policyRole)
  18. $webApp.Update()
  19. write-host "User or group " $userorgroup " added to the webapplication " $webapplication -fore green
  20. }
  21. }
  22. else
  23. {
  24. write-host "The file is not placed or its incorrectly spelled" -fore cyan
  25. }
  26. }
  27. else
  28. {
  29. write-host "The user choose to exit.... Please try again after placing the file" -fore cyan
  30. }
  31. }

Function 2

The following piece of code helps you to provide “FULL READ” access:

  1. Function FullRead()
  2. {
  3. $UserOrGroup = read-host "Enter the user or group for which you want to apply FULL READ (e.g domain\user) "
  4. write-host "Place the WebapplicationList.txt file under the folder where the script exists" -fore Magenta
  5. $Didyouplacethefile = read-host "Did you place the WebapplicationList.txt file under the folder where the script exists (y/n)?"
  6. if($Didyouplacethefile -eq 'y')
  7. {
  8. $testpath = Test-path -path $scriptbase\WebapplicationList.txt
  9. if($testpath)
  10. {
  11. foreach($webapplication in get-content "$scriptbase\WebapplicationList.txt")
  12. {
  13. $webapp = get-spwebapplication $webapplication
  14. write-host "Adding user or group " $userorgroup " to the webapplication " $webapplication "user policy and providing FULL READ access" -fore yellow
  15. $policy = $webApp.Policies.Add($userOrGroup, $userOrGroup)
  16. $policyRole = $webApp.PolicyRoles.GetSpecialRole([Microsoft.SharePoint.Administration.SPPolicyRoleType]::FullRead)
  17. $policy.PolicyRoleBindings.Add($policyRole)
  18. $webApp.Update()
  19. write-host "User or group " $userorgroup " added to the webapplication " $webapplication -fore green
  20. }
  21. }
  22. else
  23. {
  24. write-host "The file is not placed or its incorrectly spelled" -fore cyan
  25. }
  26. }
  27. else
  28. {
  29. write-host "The user choose to exit.... Please try again after placing the file" -fore cyan
  30. }
  31. }
Function 3

The following piece of code helps you to provide “DENY WRITE” access:

  1. Function DenyWrite()
  2. {
  3. $UserOrGroup = read-host "Enter the user or group for which you want to apply DENY WRITE (e.g domain\user) "
  4. write-host "Place the WebapplicationList.txt file under the folder where the script exists" -fore Magenta
  5. $Didyouplacethefile = read-host "Did you place the WebapplicationList.txt file under the folder where the script exists (y/n)?"
  6. if($Didyouplacethefile -eq 'y')
  7. {
  8. $testpath = Test-path -path $scriptbase\WebapplicationList.txt
  9. if($testpath)
  10. {
  11. foreach($webapplication in get-content "$scriptbase\WebapplicationList.txt")
  12. {
  13. $webapp = get-spwebapplication $webapplication
  14. write-host "Adding user or group " $userorgroup " to the webapplication " $webapplication "user policy and providing DENY WRITE access" -fore yellow
  15. $policy = $webApp.Policies.Add($userOrGroup, $userOrGroup)
  16. $policyRole = $webApp.PolicyRoles.GetSpecialRole([Microsoft.SharePoint.Administration.SPPolicyRoleType]::DenyWrite)
  17. $policy.PolicyRoleBindings.Add($policyRole)
  18. $webApp.Update()
  19. write-host "User or group " $userorgroup " added to the webapplication " $webapplication -fore green
  20. }
  21. }
  22. else
  23. {
  24. write-host "The file is not placed or its incorrectly spelled" -fore cyan
  25. }
  26. }
  27. else
  28. {
  29. write-host "The user choose to exit.... Please try again after placing the file" -fore cyan
  30. }
  31. }

Function 4

The following piece of code helps you to provide “DENY ALL” access:

  1. Function DenyAll()
  2. {
  3. $UserOrGroup = read-host "Enter the user or group for which you want to apply DENY ALL (e.g domain\user) "
  4. write-host "Place the WebapplicationList.txt file under the folder where the script exists" -fore Magenta
  5. $Didyouplacethefile = read-host "Did you place the WebapplicationList.txt file under the folder where the script exists (y/n)?"
  6. if($Didyouplacethefile -eq 'y')
  7. {
  8. $testpath = Test-path -path $scriptbase\WebapplicationList.txt
  9. if($testpath)
  10. {
  11. foreach($webapplication in get-content "$scriptbase\WebapplicationList.txt")
  12. {
  13. $webapp = get-spwebapplication $webapplication
  14. write-host "Adding user or group " $userorgroup " to the webapplication " $webapplication "user policy and providing DENY ALL access" -fore yellow
  15. $policy = $webApp.Policies.Add($userOrGroup, $userOrGroup)
  16. $policyRole = $webApp.PolicyRoles.GetSpecialRole([Microsoft.SharePoint.Administration.SPPolicyRoleType]::DenyAll)
  17. $policy.PolicyRoleBindings.Add($policyRole)
  18. $webApp.Update()
  19. write-host "User or group " $userorgroup " added to the webapplication " $webapplication -fore green
  20. }
  21. }
  22. else
  23. {
  24. write-host "The file is not placed or its incorrectly spelled" -fore cyan
  25. }
  26. }
  27. else
  28. {
  29. write-host "The user choose to exit.... Please try again after placing the file" -fore cyan
  30. }
  31. }

Complete Code

  1. $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm
  2. $LogFile = ".\AddUserOrGroupToWebAppPolicyPatch-$LogTime.rtf"
  3. # Add SharePoint PowerShell Snapin
  4. if ( (Get-PSSnapin -Name Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue) -eq $null ) {
  5. Add-PSSnapin Microsoft.SharePoint.Powershell
  6. }
  7. $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
  8. Set-Location $scriptBase
  9. #Deleting any .rtf files in the scriptbase location
  10. $FindRTFFile = Get-ChildItem $scriptBase\*.* -include *.rtf
  11. if($FindRTFFile)
  12. {
  13. foreach($file in $FindRTFFile)
  14. {
  15. remove-item $file
  16. }
  17. }
  18. start-transcript $logfile
  19. Function FullRead()
  20. {
  21. $UserOrGroup = read-host "Enter the user or group for which you want to apply FULL READ (e.g domain\user) "
  22. write-host "Place the WebapplicationList.txt file under the folder where the script exists" -fore Magenta
  23. $Didyouplacethefile = read-host "Did you place the WebapplicationList.txt file under the folder where the script exists (y/n)?"
  24. if($Didyouplacethefile -eq 'y')
  25. {
  26. $testpath = Test-path -path $scriptbase\WebapplicationList.txt
  27. if($testpath)
  28. {
  29. foreach($webapplication in get-content "$scriptbase\WebapplicationList.txt")
  30. {
  31. $webapp = get-spwebapplication $webapplication
  32. write-host "Adding user or group " $userorgroup " to the webapplication " $webapplication "user policy and providing FULL READ access" -fore yellow
  33. $policy = $webApp.Policies.Add($userOrGroup, $userOrGroup)
  34. $policyRole = $webApp.PolicyRoles.GetSpecialRole([Microsoft.SharePoint.Administration.SPPolicyRoleType]::FullRead)
  35. $policy.PolicyRoleBindings.Add($policyRole)
  36. $webApp.Update()
  37. write-host "User or group " $userorgroup " added to the webapplication " $webapplication -fore green
  38. }
  39. }
  40. else
  41. {
  42. write-host "The file is not placed or its incorrectly spelled" -fore cyan
  43. }
  44. }
  45. else
  46. {
  47. write-host "The user choose to exit.... Please try again after placing the file" -fore cyan
  48. }
  49. }
  50. Function FullControl()
  51. {
  52. $UserOrGroup = read-host "Enter the user or group for which you want to apply FULL CONTROL (e.g domain\user) "
  53. write-host "Place the WebapplicationList.txt file under the folder where the script exists" -fore Magenta
  54. $Didyouplacethefile = read-host "Did you place the WebapplicationList.txt file under the folder where the script exists (y/n)?"
  55. if($Didyouplacethefile -eq 'y')
  56. {
  57. $testpath = Test-path -path $scriptbase\WebapplicationList.txt
  58. if($testpath)
  59. {
  60. foreach($webapplication in get-content "$scriptbase\WebapplicationList.txt")
  61. {
  62. $webapp = get-spwebapplication $webapplication
  63. write-host "Adding user or group " $userorgroup " to the webapplication " $webapplication "user policy and providing FULL CONTROL access" -fore yellow
  64. $policy = $webApp.Policies.Add($userOrGroup, $userOrGroup)
  65. $policyRole = $webApp.PolicyRoles.GetSpecialRole([Microsoft.SharePoint.Administration.SPPolicyRoleType]::FullControl)
  66. $policy.PolicyRoleBindings.Add($policyRole)
  67. $webApp.Update()
  68. write-host "User or group " $userorgroup " added to the webapplication " $webapplication -fore green
  69. }
  70. }
  71. else
  72. {
  73. write-host "The file is not placed or its incorrectly spelled" -fore cyan
  74. }
  75. }
  76. else
  77. {
  78. write-host "The user choose to exit.... Please try again after placing the file" -fore cyan
  79. }
  80. }
  81. Function DenyWrite()
  82. {
  83. $UserOrGroup = read-host "Enter the user or group for which you want to apply DENY WRITE (e.g domain\user) "
  84. write-host "Place the WebapplicationList.txt file under the folder where the script exists" -fore Magenta
  85. $Didyouplacethefile = read-host "Did you place the WebapplicationList.txt file under the folder where the script exists (y/n)?"
  86. if($Didyouplacethefile -eq 'y')
  87. {
  88. $testpath = Test-path -path $scriptbase\WebapplicationList.txt
  89. if($testpath)
  90. {
  91. foreach($webapplication in get-content "$scriptbase\WebapplicationList.txt")
  92. {
  93. $webapp = get-spwebapplication $webapplication
  94. write-host "Adding user or group " $userorgroup " to the webapplication " $webapplication "user policy and providing DENY WRITE access" -fore yellow
  95. $policy = $webApp.Policies.Add($userOrGroup, $userOrGroup)
  96. $policyRole = $webApp.PolicyRoles.GetSpecialRole([Microsoft.SharePoint.Administration.SPPolicyRoleType]::DenyWrite)
  97. $policy.PolicyRoleBindings.Add($policyRole)
  98. $webApp.Update()
  99. write-host "User or group " $userorgroup " added to the webapplication " $webapplication -fore green
  100. }
  101. }
  102. else
  103. {
  104. write-host "The file is not placed or its incorrectly spelled" -fore cyan
  105. }
  106. }
  107. else
  108. {
  109. write-host "The user choose to exit.... Please try again after placing the file" -fore cyan
  110. }
  111. }
  112. Function DenyAll()
  113. {
  114. $UserOrGroup = read-host "Enter the user or group for which you want to apply DENY ALL (e.g domain\user) "
  115. write-host "Place the WebapplicationList.txt file under the folder where the script exists" -fore Magenta
  116. $Didyouplacethefile = read-host "Did you place the WebapplicationList.txt file under the folder where the script exists (y/n)?"
  117. if($Didyouplacethefile -eq 'y')
  118. {
  119. $testpath = Test-path -path $scriptbase\WebapplicationList.txt
  120. if($testpath)
  121. {
  122. foreach($webapplication in get-content "$scriptbase\WebapplicationList.txt")
  123. {
  124. $webapp = get-spwebapplication $webapplication
  125. write-host "Adding user or group " $userorgroup " to the webapplication " $webapplication "user policy and providing DENY ALL access" -fore yellow
  126. $policy = $webApp.Policies.Add($userOrGroup, $userOrGroup)
  127. $policyRole = $webApp.PolicyRoles.GetSpecialRole([Microsoft.SharePoint.Administration.SPPolicyRoleType]::DenyAll)
  128. $policy.PolicyRoleBindings.Add($policyRole)
  129. $webApp.Update()
  130. write-host "User or group " $userorgroup " added to the webapplication " $webapplication -fore green
  131. }
  132. }
  133. else
  134. {
  135. write-host "The file is not placed or its incorrectly spelled" -fore cyan
  136. }
  137. }
  138. else
  139. {
  140. write-host "The user choose to exit.... Please try again after placing the file" -fore cyan
  141. }
  142. }
  143. write-host "########################################################################################################" -fore cyan
  144. write-host "Enter 1 to provide FULL READ access" -fore green
  145. write-host "Enter 2 to provide FULL CONTROL access" -fore green
  146. write-host "Enter 3 to DENY WRITE access" -fore green
  147. write-host "Enter 4 to DENY ALL access" -fore green
  148. write-host "########################################################################################################" -fore cyan
  149. $option = read-host "Enter the option "
  150. switch($option)
  151. {
  152. 1{
  153. FullRead
  154. }
  155. 2{
  156. FullControl
  157. }
  158. 3{
  159. DenyWrite
  160. }
  161. 4{
  162. DenyAll
  163. }
  164. }
  165. write-host ""
  166. write-host "SCRIPT COMPLETED" -fore Blue
  167. stop-transcript

Execution Procedure

Step 1: Download and copy the script to your SharePoint server. Populate the input file (WebapplicationList.txt) with the web application details and place it under the same location where the script exists.

Step 2: Navigate to the script path.

Step 3: Execute the script as in the following:



Enter option 1 or 2 or 3 or 4 to get the desired output.

Conclusion

Thus this article provides an outline for how to add users or groups to a web application's user policy using a PowerShell script.