-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
148 lines (136 loc) · 6.46 KB
/
Copy pathProgram.cs
File metadata and controls
148 lines (136 loc) · 6.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using System;
using System.Configuration.Install;
using System.Reflection;
using System.Security.Principal;
using System.ServiceProcess;
using Microsoft.ApplicationInsights.Extensibility;
namespace ApplicationInsightsProbeForWindows
{
static class Program
{
public static bool IsAdministrator()
{
WindowsIdentity identity = WindowsIdentity.GetCurrent();
WindowsPrincipal principal = new WindowsPrincipal(identity);
return principal.IsInRole(WindowsBuiltInRole.Administrator); // Doens't solve or detect the UAC issue in fact :-)
}
/// <summary>
/// The main entry point for the application.
/// </summary>
static int Main(string[] args)
{
if (Environment.UserInteractive)
{
TelemetryConfiguration.Active.DisableTelemetry = true;
if (args.Length == 1 && !string.IsNullOrWhiteSpace(args[0]))
switch (args[0].ToLowerInvariant())
{
case "i":
case "/i":
case "-i":
case "install":
case "/install":
case "-install":
if (IsAdministrator())
{
if (!string.IsNullOrWhiteSpace(TelemetryConfiguration.Active.InstrumentationKey) && TelemetryConfiguration.Active.InstrumentationKey != "Your Key")
{
bool notExist;
try
{
ServiceController sc = new ServiceController(AIProbeService.NtName);
notExist = sc.ServiceName == null; // dummy for crash if not exist
}
catch
{
notExist = true;
}
if (notExist)
{
Console.WriteLine("Installing " + AIProbeService.NtName);
ManagedInstallerClass.InstallHelper(new string[] { Assembly.GetExecutingAssembly().Location });
return 0;
}
else
{
Console.WriteLine("ERROR : Uninstall first.");
return 1;
}
}
else
{
Console.WriteLine("ERROR : Fill the <InstrumentationKey> of ApplicationInsights.config with your Azure/AI/Properties/INSTRUMENTATION KEY.");
return 1;
}
}
else
{
Console.WriteLine("ERROR : Open console with admin right.");
return 1;
}
case "u":
case "/u":
case "-u":
case "uninstall":
case "/uninstall":
case "-uninstall":
if (IsAdministrator())
{
bool exist;
try
{
ServiceController sc = new ServiceController(AIProbeService.NtName);
exist = sc.ServiceName != null; // dummy for crash if not exist
}
catch
{
exist = false;
}
if (exist)
{
Console.WriteLine("Uninstalling " + AIProbeService.NtName);
ManagedInstallerClass.InstallHelper(new string[] { "/u", Assembly.GetExecutingAssembly().Location });
return 0;
}
else
{
Console.WriteLine("ERROR : install first.");
return 1;
}
}
else
{
Console.WriteLine("ERROR : Open console with admin right.");
return 1;
}
case "s":
case "/s":
case "-s":
case "standalone":
case "/standalone":
case "-standalone":
Console.WriteLine("Running " + AIProbeService.NtName);
if (!string.IsNullOrWhiteSpace(TelemetryConfiguration.Active.InstrumentationKey) && TelemetryConfiguration.Active.InstrumentationKey != "Your Key")
{
TelemetryConfiguration.Active.DisableTelemetry = false;
Console.WriteLine("Press ENTER to exit");
while (Console.ReadKey(true).Key != ConsoleKey.Enter) ; // loop
return 0;
}
else
{
Console.WriteLine("ERROR : Fill the <InstrumentationKey> of ApplicationInsights.config with your Azure/AI/Properties/INSTRUMENTATION KEY.");
return 1;
}
}
Console.WriteLine("USAGE as administrator : /install /uninstall /standalone");
return 1;
}
else // mode service
{
ServiceBase.Run(new ServiceBase[] { new AIProbeService() });
return 0;
}
}
}
}