public Response Get()
        {
            var response = new Response();

            try
            {
                using (var server = new TerminalServicesManager().GetLocalServer())
                {
                    server.Open();
                    var sessions = server.GetSessions()
                                   .Where(s => !string.IsNullOrEmpty(s.UserName))
                                   .Select(s => new
                    {
                        Id        = s.SessionId,
                        User      = s.DomainName + @"\" + s.UserName,
                        LoginDate = s.LoginTime?.ToString("g"),
                        State     = s.ConnectionState.ToString()
                    });
                    response.Data = sessions;
                    return(response);
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
                response.AddErrorNotification(ex.Message, ex.StackTrace);
                return(response);
            }
        }
Esempio n. 2
0
        public static void LogSessionData(Guid currentServiceRunGuid, int sessionId, SessionTrackingEvents sessionTrackingEvent, string reason, bool logAllExistingSessions = false)
        {
            using (ITerminalServer server = new TerminalServicesManager().GetLocalServer())
            {
                SessionTracking sessionTracking = null;
                if (!File.Exists(GlobalSettings.DataFilePath))
                {
                    sessionTracking = new SessionTracking();
                    sessionTracking.SessionTrackingParamsList = new List <SessionTrackingParams>();
                    File.Create(GlobalSettings.DataFilePath).Close();
                    Utility.SerializeObjectToFile <SessionTracking>(sessionTracking, GlobalSettings.DataFilePath);
                }

                server.Open();
                foreach (ITerminalServicesSession session in server.GetSessions().Where(o => o.LoginTime.HasValue))
                {
                    if (logAllExistingSessions || session.SessionId == sessionId)
                    {
                        SessionTrackingParams sessionTrackingParams = new SessionTrackingParams()
                        {
                            ServiceRunGuid = currentServiceRunGuid,
                            SessionId      = session.SessionId,
                            LogonDT        = session.LoginTime.HasValue ? session.LoginTime.Value : DateTime.MinValue,
                            EventDT        = DateTime.Now,
                            UserAccount    = (session.UserAccount != null && session.UserAccount.Value != null ? session.UserAccount.Value : String.Empty),
                            Event          = Enum.GetName(typeof(SessionTrackingEvents), sessionTrackingEvent),
                            Reason         = reason
                        };
                        string sessionTrackingParamsSerialized = Utility.SerializeObject <SessionTrackingParams>(sessionTrackingParams);

                        // Faster and not-so-clean way to write XML node to the end of the serialized List<SessionTrackingParams>,
                        // cleaner way would be to deserialize, add node and serialize whole list which might contain hundreds of records, but it would
                        // possibly be much slower in case of large file.
                        string text = File.ReadAllText(GlobalSettings.DataFilePath);
                        if (text.Contains("</SessionTrackingParamsList>"))
                        {
                            text = text.Replace(
                                "</SessionTrackingParamsList>",
                                sessionTrackingParamsSerialized + Environment.NewLine + "</SessionTrackingParamsList>");
                        }
                        else
                        {
                            text = text.Replace(
                                "<SessionTrackingParamsList />",
                                "<SessionTrackingParamsList>" + Environment.NewLine + sessionTrackingParamsSerialized + Environment.NewLine + "</SessionTrackingParamsList>");
                        }
                        File.WriteAllText(GlobalSettings.DataFilePath, text);

                        if (!logAllExistingSessions)
                        {
                            break;
                        }
                    }
                }
                server.Close();
            }
        }
Esempio n. 3
0
 public static void killProcess(string computerName, int PID)
 {
     if (ping(computerName))
     {
         ITerminalServer server = new TerminalServicesManager().GetRemoteServer(computerName);
         server.Open();
         server.GetProcess(PID).Kill();
     }
 }
Esempio n. 4
0
 public static void killProcess(string computerName, string processName)
 {
     try {
         if (ping(computerName))
         {
             ITerminalServer server = new TerminalServicesManager().GetRemoteServer(computerName);
             server.Open();
             server.GetProcesses().First(p => p.ProcessName == processName).Kill();
         }
     } catch { }
 }
Esempio n. 5
0
 public static List <ITerminalServicesProcess> getComputerProcess(string computerName)
 {
     if (ping(computerName))
     {
         ITerminalServer server = new TerminalServicesManager().GetRemoteServer(computerName);
         server.Open();
         List <ITerminalServicesProcess> processes = server.GetProcesses().ToList();
         return(processes);
     }
     else
     {
         return(new List <ITerminalServicesProcess>());
     }
 }
Esempio n. 6
0
        public static void killAnotherCopyOfProgram()
        {
            ITerminalServer server = new TerminalServicesManager().GetLocalServer();

            server.Open();
            var procs = server.GetProcesses().Where(
                el => el.ProcessName.Equals("rPCSMT.exe") &&
                Process.GetCurrentProcess().Id != el.ProcessId
                );

            foreach (var proc in procs)
            {
                proc.Kill();
            }
        }
Esempio n. 7
0
 public static bool checkUserLogedIn(string username, string computerName)
 {
     try {
         if (ping(computerName))
         {
             ITerminalServer server = new TerminalServicesManager().GetRemoteServer(computerName);
             server.Open();
             //ITerminalServicesSession session = server.GetSessions().First(_session => _session.UserAccount.Value.ToLower() == username.ToLower());
             foreach (ITerminalServicesSession session in server.GetSessions())
             {
                 if (session.UserAccount != null && session.UserName.ToLower() == username.ToLower()
                     & session.ConnectionState == Cassia.ConnectionState.Active)
                 {
                     return(true);
                 }
             }
         }                 //else MessageBox.Show($"{computerName} don't ping");
     } catch {
         //MessageBox.Show(e.Message);
     }
     return(false);
 }
        public Response Delete(int id)
        {
            var response = new Response();

            try
            {
                Log.Debug($"DropUserSession called for session id {id}.");

                using (var server = new TerminalServicesManager().GetLocalServer())
                {
                    server.Open();
                    var userSession = server.GetSessions()
                                      .FirstOrDefault(s => s.SessionId == id);

                    if (userSession == null)
                    {
                        response.AddErrorNotification($"User session {id} not found.");
                        return(response);
                    }

                    userSession.Logoff(true);

                    var message = $"Session closed for user {userSession.UserName}.";
                    Log.Debug(message);
                    response.AddSuccessNotification(message);
                }

                return(response);
            }
            catch (Exception ex)
            {
                Log.Error(ex.Message);
                response.AddErrorNotification(ex.Message, ex.StackTrace);
                return(response);
            }
        }