Posted On October 6, 2022

PowerShell: Add Local Group Members Onto a Server List

kimconnect 0 comments
blog.KimConnect.com >> Codes , Windows >> PowerShell: Add Local Group Members Onto a Server List

Sometimes, the GUI method of accomplishing tasks is too arduous and error prone. Thus, these quick cmdlets would get the job done in matter of seconds, assuming that WinRM and firewall allows the jump host to reach its targets.

# addLocalGroupMemberOnServerList.ps1

$computernames=@(
	'testserver1',
    'testserver2'
)

$localAdmins=@(
    'domain\backupadmin'
)

$remoteDesktopUsers=@(
    'domain\testuser'
)

invoke-command -computername $computernames -scriptblock{
		param($localAdmins,$remoteDesktopUsers)
		$localAdmins=$localAdmins.ToArray() # converting ArrayList datatype to Array [of objects]
		$remoteDesktopUsers=$remoteDesktopUsers.ToArray()
		try{
			$remoteDesktopUsers|%{add-localgroupmember -group 'Remote Desktop Users' -Member $_}
			$localAdmins|%{add-localgroupmember -group 'Administrators' -Member $_}
			return $true
		}catch{
			write-warning $_
			return $false
		}
	} -Args (,$localAdmins),(,$remoteDesktopUsers)

# Use legacy cmdlets to preemt this error
# Failed to compare two elements in the array.
#     + CategoryInfo          : NotSpecified: (:) [Get-LocalGroupMember], InvalidOperationException
#     + FullyQualifiedErrorId : An unspecified error occurred.,Microsoft.PowerShell.Commands.GetLocalGroupMemberCommand
#     + PSComputerName        : testserver.kimconnect.com
invoke-command -computername $computernames {net localgroup 'Administrators'; net localgroup 'Remote Desktop Users'}

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell: Perform DISM Restore Health on Remote Servers

# This script will use a Windows ISO image to Repair Remote Computers$isoPath="\\FILESHERVER007\F$\Windows_Server_2016_Datacenter_EVAL_en-us_14393_refresh.ISO"$remoteComputers="TESTKOMPUTER","TESTKOMPUTER2"function restoreServerHealth($iso){ #…

Fix Fast Logon Issue Causing H:\ Drive Not Being Mapped

a. Click Start, click Run, type REGEDIT, and then click OK. b. Locate and click…

Increase Windows Management Instrumentation Service Handle Quota Limit

WMI.ps1----------------------------------------------$config = gwmi -Class "__ProviderHostQuotaConfiguration" -Namespace root$config | select -Property * -ExcludeProperty __* | ft…