How can you get the User Logon modes for Terminal Server.
Navigate to Server Manager -> Roles -> Remote Desktop Services -> RD Session Host Configuration and look at the value for User Logon mode as shown in the image below.
How to obtain the user logon mode settings using powershell:
param($ComputerName, $username ,$password)
$namespace = "root\CIMV2\TerminalServices"
$secstr = ConvertTo-SecureString $password -asplaintext -force
$Creds = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
$CurrentMachine = gc env:computername
if($ComputerName -eq $CurrentMachine)
{
$Obj = Get-WmiObject -class Win32_TerminalServiceSetting -computername $computer -Authentication PacketPrivacy -Impersonation Impersonate -namespace namespace
}
else
{
$Obj = Get-WmiObject -class Win32_TerminalServiceSetting -computername $computer -Authentication PacketPrivacy -Impersonation Impersonate -namespace $namespace -Credential $Creds
}
$Obj
$SessionBrokerDrainMode = $Obj.SessionBrokerDrainMode
$Logons = $Obj.Logons
$SessionBrokerDrainMode
$Logons
Now if you get the variables values as either 1 or 0 for logons
0 : New sessions are allowed.
1 : New sessions are not allowed.
For SessionBrokerDrainMode could have three values:
0 : Allow all connections.
1 : Allow reconnections, but prevent new logons until the server is restarted.
2 : Allow reconnections, but prevent new logons.
Getting/Setting the User logon mode settings using registry keys:
public void GetUserLogonMode(string strMachineName, string logFile, string userLogonMode)
{
RegistryKey rkey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, strMachineName, RegistryView.Registry64);
RegistryKey rkeySessionBrokerDrainMode = rkey.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Terminal Server");
string SessionBrokerDrainMode = rkeySessionBrokerDrainMode.GetValue("TSServerDrainMode").ToString();
RegistryKey rkeyWinLogOn = rkey.OpenSubKey(@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon",true);
string strLogons = rkeyWinLogOn.GetValue("WinStationsDisabled").ToString();
}
The variables SessionBrokerDrainMode and strLogons will hold the values for the SessionBrokerDrainMode and logons as in the above case.
I have noticed some issues with powershell while extracting the values for Logons, so I think working through the registry key is a better way to do it.
For editing the values for User Logon mode you can change the value in the registry keys.
rkeySessionBrokerDrainMode.SetValue("TSServerDrainMode", 0);
rkeyWinLogOn.SetValue("WinStationsDisabled", 0);
Getting/Setting the User logon mode settings using command prompt: