-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathAddToRemoteDesktopUsers.ps1
More file actions
50 lines (41 loc) · 1.64 KB
/
AddToRemoteDesktopUsers.ps1
File metadata and controls
50 lines (41 loc) · 1.64 KB
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# Script to add the user who enrolled the computer in Microsoft Entra to the remote desktop users group
# Written by Jörgen Nilsson
# ccmexec.com
$LocalGroup = Get-LocalGroup -SID "S-1-5-32-555"
$Localgroupname = $LocalGroup.name
function Get-MembersOfGroup {
Param(
[Parameter(Mandatory = $True, Position = 1)]
[string]$GroupName,
[string]$Computer = $env:COMPUTERNAME
)
$membersOfGroup = @()
$ADSIComputer = [ADSI]("WinNT://$Computer,computer")
$group = $ADSIComputer.psbase.children.find("$GroupName", 'Group')
$group.psbase.invoke("members") | ForEach-Object {
$membersOfGroup += $_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)
}
$membersOfGroup
}
# Get the UPN of the user that enrolled the computer to AAD
$AADInfo = Get-Item "HKLM:/SYSTEM/CurrentControlSet/Control/CloudDomainJoin/JoinInfo"
$RDPUsers = Get-MembersOfGroup $Localgroupname
$guids = $AADInfo.GetSubKeyNames()
foreach ($guid in $guids) {
$guidSubKey = $AADinfo.OpenSubKey($guid);
$UPN = $guidSubKey.GetValue("UserEmail");
}
$Username = $UPN -split ("@")
$Username = $Username[0]
if ($UPN) {
if (!($RDPUsers -contains $Username)) {
Add-LocalGroupMember -Group $Localgroupname -Member "Azuread\$UPN"
"Added AzureAD\$UPN as a member of the Remote Desktop Users." | Out-File -FilePath $env:TEMP\RDPUsers.log
}
else {
"AzureAD\$UPN is already a member of the Remote Desktop Users Group." | Out-File -FilePath $env:TEMP\RDPUsers.log
}
}
else {
"Failed to find an RDPUsername in registry." | Out-File -FilePath $env:TEMP\RDPUsers.log
}