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: Find Hyper-V Host by Guest VM Name

# findVmHostByGuestName.ps1 $vmName='TESTVM' function findVmHostByGuestName($vmName){ try{ Import-Module Hyper-V Import-Module FailoverClusters $allHyperVHosts={(Get-ClusterNode | Where { $_.State…

Shoretel: Why do we need an inventory of MAC addresses?

Answer: systems such are Shoretel's licensing are dependent on the MAC address of the original…

PowerShell: Quick 1-Liner to Locate Path to Executable

PS C:\Users\tester> (get-command robocopy.exe) | select DefinitionDefinition----------C:\Program Files (x86)\Windows Resource Kits\Tools\robocopy.exeC:\Windows\system32\Robocopy.exe