Tag Archives: Command prompt

Close the application process on SUT using Eggplant

It is little tricky to talk to the file system or the task manager in the most reliable ways using eggplant.
Below is the code to close the process using command prompt with Eggplant instead of using the Task manager images.

function CloseApplicationProcess
try
TypeText windowsKey
TypeText controlKey ,"r"
TypeText "Cmd.exe"
TypeText enterKey
wait 1
TypeText "taskkill /FI " && quote &"Imagename eq test.exe" & quote & " -f"
TypeText enterKey
wait 1
TypeText "exit"
TypeText enterKey
return true
Catch anException
tracescreen on
put the exception's location into excepLoc
replace return in excepLoc with ", "
log anException & " | " & excepLoc & " | " & " Function : CloseApplication"
return false
end try
end CloseApplicationProcess

you can do a lot more by interacting with the command prompt, like dealing with the file system, running the batch files, restarting the services etc.
Whatever you could do using command prompt on a machine, can be done this way using Eggplant on the SUT directly.

User logon modes for Terminal Servers

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.

UserLogonMode

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:

CommandPrompt