Articles about European Sharepoint Hosting Service
SharePoint 2013 Hosting UK – HostForLIFE.eu :: PowerShell to Get/Add/Remove Users of Local Administrator Group
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
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.
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.
Print article | This entry was posted by Peter on August 23, 2018 at 7:01 am, and is filed under European SharePoint 2013 Hosting. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |