2

I am creating a script to check details of Remote desktop connection.

$ts = get-WMIObject Win32_TerminalServiceSetting  -computername $s -Namespace ROOT\CIMV2\TerminalServices

With above script i can check RDP is enaled or not. But i need to check port number to be used in RDP.

I am aware 3389 is getting used but in my environment some servers RDP configured on other ports.

I need to fetch the port number. I checked in ROOT\CIMV2\TerminalServices WMI Class but port number detail is not there.

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp

On this location i can see there is a dword (PortNumber) that contain the port number details.

Can we check some other way because by checking registry we need to start remote registry service is running and i am afraid sometimes they are stopped and we can't run it.

2

I'm not sure about remote registry, its usually enough to have WinRM started. you can check the port number by using a combination of Invoke-Command and Get-itemProperty.

Invoke-Command -HideComputerName computer_machine { Get-ItemProperty  -Path HKLM:\SYSTEM\CurrentControlSet\Control\Termin*Server\WinStations\RDP*CP\ -Name PortNumber | select PortNumber}
  • If you want ONLY the port number, tack this on the end: | Select-Object PortNumber -ExpandProperty PortNumber – Cory Knutson Dec 19 '17 at 16:45
  • 2
    i wonder what genius decided to change the port number RDP listens on. now you have to (know and) include the port number when connecting. if its for security purposes, you're doing it wrong! – Sum1sAdmin Dec 19 '17 at 16:50
  • Invoke-Command is not working unfortunately. Is there other ways? – Calculating Machine Dec 20 '17 at 7:20
  • This seems like a remote management question rather than an 'how to get' question. I would recommend using DSC to setup RDP consistently and a administrative GPO to enable and configure WinRM – Sum1sAdmin Dec 20 '17 at 10:18
0

have fun with psremoting module http://psremoteregistry.codeplex.com/

Import-Module PSRemoteRegistry
$computers="server2","server3"
Foreach($computer in $computers){
get-regdword -Computer $computer -Hive LocalMachine -Key 'SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp' -Value PortNumber |select computername,data
}
0

Best way without anytool

    $computers="computer1","Computer2"
$result=Foreach ($Computer in $Computers) 
{
$RegBase = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine,$Computer)
$Reg=$RegBase.OpenSubKey(‘SYSTEM\CurrentControlSet\Control\Terminal Server\WinStations\RDP-Tcp’).GetValue(‘PortNumber’)
New-Object -TypeName PSCustomObject -Property @{
ComputerName = $Computer
Port=$reg
}
}

$result|select computername,port

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.