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

Basic HTML and HTML5: Create a Form Element

<h2>CatPhotoApp</h2><main><p>Click here to view more <a href="#">cat photos</a>.</p><a href="#"><img src="https://bit.ly/fcc-relaxing-cat" alt="A cute orange cat lying…

PowerShell: Manually Create Failover Cluster SMB Shares

# Set three variables$shareName="someShare"$smbServer="someClusteredVServer"$sharePath="S:\Shares\someShare"# Generate Domain Admins label$subdomain=(net config workstation) -match 'Workstation domain\s+\S+$' -replace '.+?(\S+)$','$1'$domainadmins="$subdomain`\Domain…

Updating SSL Certificates on Active Directory Federation Services (ADFS) Server

Scripted Version Notes: Part 1: Adding Cert to ADFS Server # Import Cert and get…