// Returns the session which matches as closely as possible the requirements private UserSession GetUserSessionState(int sessionId) { UserInfo userInfo = new UserInfo(); List <WTS_SESSION_INFO> sessions = UserSessionHelper.GetUserSessions(); // Loop through the current user sessions to find the one with matching sessionId foreach (WTS_SESSION_INFO session in sessions) { if (session.SessionId == sessionId) { UserSession sessionStateValue = null; if (session.State == WTS_CONNECTSTATE_CLASS.WTSActive) { // A locked session also has its state as WTSActive. // Check specifically if it is locked. if (UserSessionHelper.IsLoginSession(sessionId)) { sessionStateValue = new UserSession(UserSessionType.Locked); } else { sessionStateValue = new UserSession(UserSessionType.Active); } } if (session.State == WTS_CONNECTSTATE_CLASS.WTSDisconnected) { sessionStateValue = new UserSession(UserSessionType.Disconnected); } // Grab the user info (username, domain, password) if (UserSessionHelper.GetUserSessionUserInfo(sessionId, out userInfo)) { sessionStateValue.UserLoginInfo = userInfo; return(sessionStateValue); } else { // For machines without auto logon, password retrieval fails. We would like to // throw an exception here to fail the test case. throw new InvalidOperationException("Auto logon is required to be enabled for this task to succeed"); } } } return(null); }