static void Main() { Config.Load(); var args = Environment.GetCommandLineArgs().ToList(); // If "-interactive" switch present, run service as an interactive console app. if (args.Exists(str => str.ToLower() == "-interactive")) { AditService.Connect(); } else if (args.Exists(str => str.ToLower() == "-install")) { InstallService(args); } else if (args.Exists(str => str.ToLower() == "-uninstall")) { UninstallService(); } else { ServiceBase[] ServicesToRun; ServicesToRun = new ServiceBase[] { new WindowsService() }; ServiceBase.Run(ServicesToRun); } while (true) { System.Threading.Thread.Sleep(60000); } }
public void SendHeartbeat() { if (!AditService.HeartbeatTimer.Enabled) { AditService.HeartbeatTimer.Elapsed += (sender, args) => { SendHeartbeat(); }; AditService.HeartbeatTimer.Interval = 30000; AditService.HeartbeatTimer.Start(); } try { if (!AditService.IsConnected) { AditService.HeartbeatTimer.Stop(); AditService.WaitToRetryConnection(); return; } var uptime = new PerformanceCounter("System", "System Up Time", true); var macAddress = Utilities.GetMACAddress(); uptime.NextValue(); string currentUser; try { var mos = new ManagementObjectSearcher("Select * FROM Win32_Process WHERE ExecutablePath LIKE '%explorer.exe%'"); var col = mos.Get(); var process = col.Cast <ManagementObject>().First(); var ownerInfo = new string[2]; process.InvokeMethod("GetOwner", ownerInfo); currentUser = ownerInfo[1] + "\\" + ownerInfo[0]; } catch { currentUser = ""; } var request = new { Type = "Heartbeat", ComputerName = Environment.MachineName, CurrentUser = currentUser, LastReboot = (DateTime.Now - TimeSpan.FromSeconds(uptime.NextValue())), MACAddress = macAddress }; SendJSON(request); Utilities.CleanupFiles(); } catch (Exception ex) { Utilities.WriteToLog(ex); } }
protected override void OnStart(string[] args) { base.OnStart(args); AditService.Connect(); }