Skip to content

Add users to a RDP group based on hostname

Featured Replies

Hi Guys,

 

I would like to add domain users to be able to log on via RDP to their desktop via script or GPO. Basically, all our desktops are named after the user who uses it.

 

Dersktop=sjobs-xp1, sjobs-ws1 and Username=sjobs

 

Can you guys advise me how can I achieve this? Any scripting ideas on windows? We also just started using SCCM, so anything there can also work.

  • Author

Ok. so I wrote a powershell script myself and here it is if someone comes looking for similar request.

 

# Script to add a domain user to a Remote Desktop users group on the local PC.

$computerName = gc env:computername
$userName = $computerName.split("-")[0]
$Domain = gc env:USERDNSDOMAIN
$localGroupName = "Remote Desktop Users"

if ([adsi]::Exists("LDAP://CN=$userName,OU=PEOPLE,OU=Accounts,DC=laurion,DC=corp"))
{
 $adsi = [ADSI]"WinNT://$ComputerName/$localGroupName,group"
 $memberList = $adsi.psbase.invoke("Members") | %{$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}

 foreach($member in $memberList)
 { Write-Host "Member is $member"
if(!$member -eq $userName)
{  
   	$adsi.add("WinNT://$Domain/$userName,group")
   	Write-Host "User $Domain\$userName is now member of local group $localGroupName on $computerName."
} else
{
   	Write-Host "User already exists in the group."
}
 }
} else
{
 Write-Host "User does not exist in AD domain."
}

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.