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.
- Gets the local administrators of the machine.
- 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).
- 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.
- Function GetServerAdministrators([Microsoft.SharePoint.Administration.SPFarm]$farm)
- {
- write-host ""
- write-host "Preparing to collect SP server administrator details" -fore magenta
- $output = $scriptbase + "\" + "ServerAdminDetails.csv"
- "ServerName" + "," + "AdminMember" | Out-File -Encoding Default -FilePath $Output;
- foreach($server in $farm.Servers)
- {
- foreach($instance in $server.ServiceInstances)
- {
- if($instance.TypeName -eq $timerServiceInstanceName)
- {
- [string]$serverName = $server.Name
- write-host "Collecting administrator details for the server " $servername -fore yellow
- $admins = invoke-command {net localgroup administrators | where {$_ -AND $_ -notmatch "command completed successfully"} | select -skip 4} -computer $serverName
- foreach($admin in $admins)
- {
- write-host $admin " is member of administrator group in server " $serverName -fore cyan
- $serverName + "," + $admin | Out-File -Encoding Default -Append -FilePath $Output;
- }
- write-host "Administrator details for the server " $serverName " has been collected" -fore green
- }
- }
- }
- Write-host "Administrator details collected for all the SP servers in the farm" -fore green
- }
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.
- Function AddUserToServerAdminGroup([String]$AdminMember, [String]$ServerName)
- {
- $ans = read-host "Do you want to add user $AdminMember to server $ServerName (y/n)? "
- if($ans -eq 'y')
- {
- write-host "Adding user " $AdminMember " to administrator group on server " $ServerName -fore yellow
- $AdminMember1 = $AdminMember.split("\")
- $AdminMember2 = $AdminMember1[0] + "/" + $AdminMember1[1]
- $GroupObj = [ADSI]"WinNT://$ServerName/Administrators"
- $GroupObj.Add("WinNT://$AdminMember2")
- write-host $AdminMember " added to the local administrator group on the server " $ServerName -fore green
- }
- else
- {
- write-host "User choose not to add user " $AdminMember " to the server " $ServerName " administrator group" -fore cyan
- }
- }
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.
- Function RemoveUserFromServerAdminGroup([String]$AdminMember, [String]$ServerName)
- {
- $ans = read-host "Do you want to remove user $AdminMember from server $ServerName (y/n)? "
- if($ans -eq 'y')
- {
- write-host "Removing user " $AdminMember " from administrator group on server " $ServerName - fore yellow
- $AdminMember1 = $AdminMember.split("\")
- $AdminMember2 = $AdminMember1[0] + "/" + $AdminMember1[1]
- $GroupObj = [ADSI]"WinNT://$ServerName/Administrators"
- $GroupObj.Remove("WinNT://$AdminMember2")
- write-host $AdminMember " removed from the local administrator group on the server " $ServerName -fore green
- }
- else
- {
- write-host "User choose not to remove user " $AdminMember " from the server " $ServerName " administrator group" -fore cyan
- }
- }
Complete Code
Execution Procedure
- Download and copy the script folder to the SharePoint server.
- Launch the SharePoint management shell.
- 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.