Introduction

This article outlines how to get, add and remove users of a local administrator group on SharePoint servers using a PowerShell script.

Local Administrators

The script does the following functionality.

  1. Gets the local administrators of the machine.
  2. Adds a user to the local administrator of the machine (the user must enter the user details into the AddUsers.csv file and place it under the folder where the PowerShell script exists).
  3. Removes a user from the local administrator of the machine (the user must enter the user details into the RemoveUsers.csv file and place it under the folder where the PowerShell script exists).

Get the local administrators of the machine

The following piece of code gets the users under the local administrator group of the machine.

  1. Function GetServerAdministrators([Microsoft.SharePoint.Administration.SPFarm]$farm)
  2. {
  3. write-host ""
  4. write-host "Preparing to collect SP server administrator details" -fore magenta
  5. $output = $scriptbase + "\" + "ServerAdminDetails.csv"
  6. "ServerName" + "," + "AdminMember" | Out-File -Encoding Default -FilePath $Output;
  7. foreach($server in $farm.Servers)
  8. {
  9. foreach($instance in $server.ServiceInstances)
  10. {
  11. if($instance.TypeName -eq $timerServiceInstanceName)
  12. {
  13. [string]$serverName = $server.Name
  14. write-host "Collecting administrator details for the server " $servername -fore yellow
  15. $admins = invoke-command {net localgroup administrators | where {$_ -AND $_ -notmatch "command completed successfully"} | select -skip 4} -computer $serverName
  16. foreach($admin in $admins)
  17. {
  18. write-host $admin " is member of administrator group in server " $serverName -fore cyan
  19. $serverName + "," + $admin | Out-File -Encoding Default -Append -FilePath $Output;
  20. }
  21. write-host "Administrator details for the server " $serverName " has been collected" -fore green
  22. }
  23. }
  24. }
  25. Write-host "Administrator details collected for all the SP servers in the farm" -fore green
  26. }
Add users to the local administrator of the machine

The following piece of code helps to add the users to the local administrator group on the SharePoint servers.

  1. Function AddUserToServerAdminGroup([String]$AdminMember, [String]$ServerName)
  2. {
  3. $ans = read-host "Do you want to add user $AdminMember to server $ServerName (y/n)? "
  4. if($ans -eq 'y')
  5. {
  6. write-host "Adding user " $AdminMember " to administrator group on server " $ServerName -fore yellow
  7. $AdminMember1 = $AdminMember.split("\")
  8. $AdminMember2 = $AdminMember1[0] + "/" + $AdminMember1[1]
  9. $GroupObj = [ADSI]"WinNT://$ServerName/Administrators"
  10. $GroupObj.Add("WinNT://$AdminMember2")
  11. write-host $AdminMember " added to the local administrator group on the server " $ServerName -fore green
  12. }
  13. else
  14. {
  15. write-host "User choose not to add user " $AdminMember " to the server " $ServerName " administrator group" -fore cyan
  16. }
  17. }
Remove users from local administrator of the machine

The following piece of code helps to remove the users from the local administrator group on the SharePoint servers.
  1. Function RemoveUserFromServerAdminGroup([String]$AdminMember, [String]$ServerName)
  2. {
  3. $ans = read-host "Do you want to remove user $AdminMember from server $ServerName (y/n)? "
  4. if($ans -eq 'y')
  5. {
  6. write-host "Removing user " $AdminMember " from administrator group on server " $ServerName - fore yellow
  7. $AdminMember1 = $AdminMember.split("\")
  8. $AdminMember2 = $AdminMember1[0] + "/" + $AdminMember1[1]
  9. $GroupObj = [ADSI]"WinNT://$ServerName/Administrators"
  10. $GroupObj.Remove("WinNT://$AdminMember2")
  11. write-host $AdminMember " removed from the local administrator group on the server " $ServerName -fore green
  12. }
  13. else
  14. {
  15. write-host "User choose not to remove user " $AdminMember " from the server " $ServerName " administrator group" -fore cyan
  16. }
  17. }
Complete Code
  1. $LogTime = Get-Date -Format yyyy-MM-dd_hh-mm
  2. $LogFile = ".\GetServerAdministratorsPatch-$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. import-module WebAdministration
  8. $scriptBase = split-path $SCRIPT:MyInvocation.MyCommand.Path -parent
  9. Set-Location $scriptBase
  10. write-host "TESTING FOR LOG FOLDER EXISTENCE" -fore yellow
  11. $TestLogFolder = test-path -path $scriptbase\Logs
  12. if($TestLogFolder)
  13. {
  14. write-host "The log folder already exist in the script location" -fore yellow
  15. $clearlogfolder = read-host "Do you want to clear the log folder (y/n)"
  16. if($clearlogfolder -eq 'y')
  17. {
  18. write-host "The user choosen to clear the log folder" -fore yellow
  19. write-host "Clearing the log folder" -fore yellow
  20. remove-item $scriptbase\Logs\* -recurse -confirm:$false
  21. write-host "Log folder cleared" -fore yellow
  22. }
  23. else
  24. {
  25. write-host "The user choosen not to clear the log files" -fore yellow
  26. }
  27. }
  28. else
  29. {
  30. write-host "Log folder does not exist" -fore yellow
  31. write-host "Creating a log folder" -fore yellow
  32. New-Item $Scriptbase\Logs -type directory
  33. write-host "Log folder created" -fore yellow
  34. }
  35. #moving any .rtf files in the scriptbase location
  36. $FindRTFFile = Get-ChildItem $scriptBase\*.* -include *.rtf
  37. if($FindRTFFile)
  38. {
  39. write-host "Some old log files are found in the script location" -fore yellow
  40. write-host "Moving old log files into the Logs folder" -fore yellow
  41. foreach($file in $FindRTFFile)
  42. {
  43. move-item -path $file -destination $scriptbase\logs
  44. }
  45. write-host "Old log files moved successfully" -fore yellow
  46. }
  47. start-transcript $logfile
  48. $global:timerServiceName = "SharePoint 2010 Timer"
  49. $global:timerServiceInstanceName = "Microsoft SharePoint Foundation Timer"
  50. # Get the local farm instance
  51. [Microsoft.SharePoint.Administration.SPFarm]$farm = [Microsoft.SharePoint.Administration.SPFarm]::get_Local()
  52. Function GetServerAdministrators([Microsoft.SharePoint.Administration.SPFarm]$farm)
  53. {
  54. write-host ""
  55. write-host "Preparing to collect SP server administrator details" -fore magenta
  56. $output = $scriptbase + "\" + "ServerAdminDetails.csv"
  57. "ServerName" + "," + "AdminMember" | Out-File -Encoding Default -FilePath $Output;
  58. foreach($server in $farm.Servers)
  59. {
  60. foreach($instance in $server.ServiceInstances)
  61. {
  62. if($instance.TypeName -eq $timerServiceInstanceName)
  63. {
  64. [string]$serverName = $server.Name
  65. write-host "Collecting administrator details for the server " $servername -fore yellow
  66. $admins = invoke-command {net localgroup administrators | where {$_ -AND $_ -notmatch "command completed successfully"} | select -skip 4} -computer $serverName
  67. foreach($admin in $admins)
  68. {
  69. write-host $admin " is member of administrator group in server " $serverName -fore cyan
  70. $serverName + "," + $admin | Out-File -Encoding Default -Append -FilePath $Output;
  71. }
  72. write-host "Administrator details for the server " $serverName " has been collected" -fore green
  73. }
  74. }
  75. }
  76. Write-host "Administrator details collected for all the SP servers in the farm" -fore green
  77. }
  78. Function AddUserToServerAdminGroup([String]$AdminMember, [String]$ServerName)
  79. {
  80. $ans = read-host "Do you want to add user $AdminMember to server $ServerName (y/n)? "
  81. if($ans -eq 'y')
  82. {
  83. write-host "Adding user " $AdminMember " to administrator group on server " $ServerName -fore yellow
  84. $AdminMember1 = $AdminMember.split("\")
  85. $AdminMember2 = $AdminMember1[0] + "/" + $AdminMember1[1]
  86. $GroupObj = [ADSI]"WinNT://$ServerName/Administrators"
  87. $GroupObj.Add("WinNT://$AdminMember2")
  88. write-host $AdminMember " added to the local administrator group on the server " $ServerName -fore green
  89. }
  90. else
  91. {
  92. write-host "User choose not to add user " $AdminMember " to the server " $ServerName " administrator group" -fore cyan
  93. }
  94. }
  95. Function RemoveUserFromServerAdminGroup([String]$AdminMember, [String]$ServerName)
  96. {
  97. $ans = read-host "Do you want to remove user $AdminMember from server $ServerName (y/n)? "
  98. if($ans -eq 'y')
  99. {
  100. write-host "Removing user " $AdminMember " from administrator group on server " $ServerName -fore yellow
  101. $AdminMember1 = $AdminMember.split("\")
  102. $AdminMember2 = $AdminMember1[0] + "/" + $AdminMember1[1]
  103. $GroupObj = [ADSI]"WinNT://$ServerName/Administrators"
  104. $GroupObj.Remove("WinNT://$AdminMember2")
  105. write-host $AdminMember " removed from the local administrator group on the server " $ServerName -fore green
  106. }
  107. else
  108. {
  109. write-host "User choose not to remove user " $AdminMember " from the server " $ServerName " administrator group" -fore cyan
  110. }
  111. }
  112. write-host "########################################################################################################" -fore cyan
  113. write-host "Enter 1 to get the SP server administrator details" -fore green
  114. write-host "Enter 2 to add users to local administrator group" -fore green
  115. write-host "Enter 3 to remove users from local administrator group" -fore green
  116. write-host "########################################################################################################" -fore cyan
  117. $option = read-host "Enter the option "
  118. switch($option)
  119. {
  120. 1{
  121. GetServerAdministrators $farm
  122. }
  123. 2{
  124. write-host "Preparing to add users to the server administrator group" -fore magenta
  125. $csvfile = $scriptbase + "\" + "AddUsers.csv"
  126. import-csv $csvfile | where {
  127. AddUserToServerAdminGroup $_.AdminMember $_.ServerName
  128. }
  129. write-host "Users has been added to local administrators group" -fore green
  130. }
  131. 3{
  132. write-host "Preparing to remove users from the server administrator group" -fore magenta
  133. $csvfile1 = $scriptbase + "\" + "RemoveUsers.csv"
  134. import-csv $csvfile1 | where {
  135. RemoveUserFromServerAdminGroup $_.AdminMember $_.ServerName
  136. }
  137. write-host "Users has been removed from local administrators group" -fore green
  138. }
  139. }
  140. stop-transcript
Execution Procedure
  1. Download and copy the script folder to the SharePoint server.
  2. Launch the SharePoint management shell.
  3. Navigate to the script path and execute the script.

Enter the desired option.

Conclusion

Thus this article outlines how to get, add and remove users of the local administrator group on SharePoint servers using a PowerShell script.