Skip to content

Commit 65d8f11

Browse files
authored
Update telemetry collecting with a few changes (PowerShell#17304)
1 parent c40066d commit 65d8f11

File tree

6 files changed

+233
-76
lines changed

6 files changed

+233
-76
lines changed

src/Microsoft.PowerShell.ConsoleHost/host/msh/CommandLineParameterParser.cs

Lines changed: 135 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
// Copyright (c) Microsoft Corporation.
22
// Licensed under the MIT License.
3-
43
#nullable enable
54

65
using System;
@@ -195,6 +194,57 @@ internal static int MaxNameLength()
195194
"workingdirectory"
196195
};
197196

197+
/// <summary>
198+
/// These represent the parameters that are used when starting pwsh.
199+
/// We can query in our telemetry to determine how pwsh was invoked.
200+
/// </summary>
201+
[Flags]
202+
internal enum ParameterBitmap : long
203+
{
204+
Command = 0x00000001, // -Command | -c
205+
ConfigurationName = 0x00000002, // -ConfigurationName | -config
206+
CustomPipeName = 0x00000004, // -CustomPipeName
207+
EncodedCommand = 0x00000008, // -EncodedCommand | -e | -ec
208+
EncodedArgument = 0x00000010, // -EncodedArgument
209+
ExecutionPolicy = 0x00000020, // -ExecutionPolicy | -ex | -ep
210+
File = 0x00000040, // -File | -f
211+
Help = 0x00000080, // -Help, -?, /?
212+
InputFormat = 0x00000100, // -InputFormat | -inp | -if
213+
Interactive = 0x00000200, // -Interactive | -i
214+
Login = 0x00000400, // -Login | -l
215+
MTA = 0x00000800, // -MTA
216+
NoExit = 0x00001000, // -NoExit | -noe
217+
NoLogo = 0x00002000, // -NoLogo | -nol
218+
NonInteractive = 0x00004000, // -NonInteractive | -noni
219+
NoProfile = 0x00008000, // -NoProfile | -nop
220+
OutputFormat = 0x00010000, // -OutputFormat | -o | -of
221+
SettingsFile = 0x00020000, // -SettingsFile | -settings
222+
SSHServerMode = 0x00040000, // -SSHServerMode | -sshs
223+
SocketServerMode = 0x00080000, // -SocketServerMode | -sockets
224+
ServerMode = 0x00100000, // -ServerMode | -server
225+
NamedPipeServerMode = 0x00200000, // -NamedPipeServerMode | -namedpipes
226+
STA = 0x00400000, // -STA
227+
Version = 0x00800000, // -Version | -v
228+
WindowStyle = 0x01000000, // -WindowStyle | -w
229+
WorkingDirectory = 0x02000000, // -WorkingDirectory | -wd
230+
// Enum values for specified ExecutionPolicy
231+
EPUnrestricted = 0x0000000100000000, // ExecutionPolicy unrestricted
232+
EPRemoteSigned = 0x0000000200000000, // ExecutionPolicy remote signed
233+
EPAllSigned = 0x0000000400000000, // ExecutionPolicy all signed
234+
EPRestricted = 0x0000000800000000, // ExecutionPolicy restricted
235+
EPDefault = 0x0000001000000000, // ExecutionPolicy default
236+
EPBypass = 0x0000002000000000, // ExecutionPolicy bypass
237+
EPUndefined = 0x0000004000000000, // ExecutionPolicy undefined
238+
EPIncorrect = 0x0000008000000000, // ExecutionPolicy incorrect
239+
}
240+
241+
internal ParameterBitmap ParametersUsed = 0;
242+
243+
internal double ParametersUsedAsDouble
244+
{
245+
get { return (double)ParametersUsed; }
246+
}
247+
198248
[Conditional("DEBUG")]
199249
private void AssertArgumentsParsed()
200250
{
@@ -641,6 +691,53 @@ internal static string NormalizeFilePath(string path)
641691
return Path.GetFullPath(path);
642692
}
643693

694+
/// <summary>
695+
/// Determine the execution policy based on the supplied string.
696+
/// If the string doesn't match to any known execution policy, set it to incorrect.
697+
/// </summary>
698+
/// <param name="_executionPolicy">The value provided on the command line.</param>
699+
/// <returns>The execution policy.</returns>
700+
private static ParameterBitmap GetExecutionPolicy(string? _executionPolicy)
701+
{
702+
if (_executionPolicy is null)
703+
{
704+
return ParameterBitmap.EPUndefined;
705+
}
706+
707+
ParameterBitmap executionPolicySetting = ParameterBitmap.EPIncorrect;
708+
709+
if (string.Equals(_executionPolicy, "default", StringComparison.OrdinalIgnoreCase))
710+
{
711+
executionPolicySetting = ParameterBitmap.EPDefault;
712+
}
713+
else if (string.Equals(_executionPolicy, "remotesigned", StringComparison.OrdinalIgnoreCase))
714+
{
715+
executionPolicySetting = ParameterBitmap.EPRemoteSigned;
716+
}
717+
else if (string.Equals(_executionPolicy, "bypass", StringComparison.OrdinalIgnoreCase))
718+
{
719+
executionPolicySetting = ParameterBitmap.EPBypass;
720+
}
721+
else if (string.Equals(_executionPolicy, "allsigned", StringComparison.OrdinalIgnoreCase))
722+
{
723+
executionPolicySetting = ParameterBitmap.EPAllSigned;
724+
}
725+
else if (string.Equals(_executionPolicy, "restricted", StringComparison.OrdinalIgnoreCase))
726+
{
727+
executionPolicySetting = ParameterBitmap.EPRestricted;
728+
}
729+
else if (string.Equals(_executionPolicy, "unrestricted", StringComparison.OrdinalIgnoreCase))
730+
{
731+
executionPolicySetting = ParameterBitmap.EPUnrestricted;
732+
}
733+
else if (string.Equals(_executionPolicy, "undefined", StringComparison.OrdinalIgnoreCase))
734+
{
735+
executionPolicySetting = ParameterBitmap.EPUndefined;
736+
}
737+
738+
return executionPolicySetting;
739+
}
740+
644741
private static bool MatchSwitch(string switchKey, string match, string smallestUnambiguousMatch)
645742
{
646743
Dbg.Assert(!string.IsNullOrEmpty(match), "need a value");
@@ -755,6 +852,7 @@ private void ParseHelper(string[] args)
755852
_noInteractive = true;
756853
_skipUserInit = true;
757854
_noExit = false;
855+
ParametersUsed |= ParameterBitmap.Version;
758856
break;
759857
}
760858

@@ -763,48 +861,59 @@ private void ParseHelper(string[] args)
763861
_showHelp = true;
764862
_showExtendedHelp = true;
765863
_abortStartup = true;
864+
ParametersUsed |= ParameterBitmap.Help;
766865
}
767866
else if (MatchSwitch(switchKey, "login", "l"))
768867
{
769868
// On Windows, '-Login' does nothing.
770869
// On *nix, '-Login' is already handled much earlier to improve startup performance, so we do nothing here.
870+
ParametersUsed |= ParameterBitmap.Login;
771871
}
772872
else if (MatchSwitch(switchKey, "noexit", "noe"))
773873
{
774874
_noExit = true;
775875
noexitSeen = true;
876+
ParametersUsed |= ParameterBitmap.NoExit;
776877
}
777878
else if (MatchSwitch(switchKey, "noprofile", "nop"))
778879
{
779880
_skipUserInit = true;
881+
ParametersUsed |= ParameterBitmap.NoProfile;
780882
}
781883
else if (MatchSwitch(switchKey, "nologo", "nol"))
782884
{
783885
_showBanner = false;
886+
ParametersUsed |= ParameterBitmap.NoLogo;
784887
}
785888
else if (MatchSwitch(switchKey, "noninteractive", "noni"))
786889
{
787890
_noInteractive = true;
891+
ParametersUsed |= ParameterBitmap.NonInteractive;
788892
}
789893
else if (MatchSwitch(switchKey, "socketservermode", "so"))
790894
{
791895
_socketServerMode = true;
896+
ParametersUsed |= ParameterBitmap.SocketServerMode;
792897
}
793898
else if (MatchSwitch(switchKey, "servermode", "s"))
794899
{
795900
_serverMode = true;
901+
ParametersUsed |= ParameterBitmap.ServerMode;
796902
}
797903
else if (MatchSwitch(switchKey, "namedpipeservermode", "nam"))
798904
{
799905
_namedPipeServerMode = true;
906+
ParametersUsed |= ParameterBitmap.NamedPipeServerMode;
800907
}
801908
else if (MatchSwitch(switchKey, "sshservermode", "sshs"))
802909
{
803910
_sshServerMode = true;
911+
ParametersUsed |= ParameterBitmap.SSHServerMode;
804912
}
805913
else if (MatchSwitch(switchKey, "interactive", "i"))
806914
{
807915
_noInteractive = false;
916+
ParametersUsed |= ParameterBitmap.Interactive;
808917
}
809918
else if (MatchSwitch(switchKey, "configurationname", "config"))
810919
{
@@ -817,6 +926,7 @@ private void ParseHelper(string[] args)
817926
}
818927

819928
_configurationName = args[i];
929+
ParametersUsed |= ParameterBitmap.ConfigurationName;
820930
}
821931
else if (MatchSwitch(switchKey, "custompipename", "cus"))
822932
{
@@ -841,14 +951,18 @@ private void ParseHelper(string[] args)
841951
break;
842952
}
843953
#endif
954+
844955
_customPipeName = args[i];
956+
ParametersUsed |= ParameterBitmap.CustomPipeName;
845957
}
846958
else if (MatchSwitch(switchKey, "command", "c"))
847959
{
848960
if (!ParseCommand(args, ref i, noexitSeen, false))
849961
{
850962
break;
851963
}
964+
965+
ParametersUsed |= ParameterBitmap.Command;
852966
}
853967
else if (MatchSwitch(switchKey, "windowstyle", "w"))
854968
{
@@ -875,6 +989,8 @@ private void ParseHelper(string[] args)
875989
string.Format(CultureInfo.CurrentCulture, CommandLineParameterParserStrings.InvalidWindowStyleArgument, args[i], e.Message));
876990
break;
877991
}
992+
993+
ParametersUsed |= ParameterBitmap.WindowStyle;
878994
#endif
879995
}
880996
else if (MatchSwitch(switchKey, "file", "f"))
@@ -883,6 +999,8 @@ private void ParseHelper(string[] args)
883999
{
8841000
break;
8851001
}
1002+
1003+
ParametersUsed |= ParameterBitmap.File;
8861004
}
8871005
#if DEBUG
8881006
else if (MatchSwitch(switchKey, "isswait", "isswait"))
@@ -894,14 +1012,18 @@ private void ParseHelper(string[] args)
8941012
{
8951013
ParseFormat(args, ref i, ref _outFormat, CommandLineParameterParserStrings.MissingOutputFormatParameter);
8961014
_outputFormatSpecified = true;
1015+
ParametersUsed |= ParameterBitmap.OutputFormat;
8971016
}
8981017
else if (MatchSwitch(switchKey, "inputformat", "inp") || MatchSwitch(switchKey, "if", "if"))
8991018
{
9001019
ParseFormat(args, ref i, ref _inFormat, CommandLineParameterParserStrings.MissingInputFormatParameter);
1020+
ParametersUsed |= ParameterBitmap.InputFormat;
9011021
}
9021022
else if (MatchSwitch(switchKey, "executionpolicy", "ex") || MatchSwitch(switchKey, "ep", "ep"))
9031023
{
9041024
ParseExecutionPolicy(args, ref i, ref _executionPolicy, CommandLineParameterParserStrings.MissingExecutionPolicyParameter);
1025+
ParametersUsed |= ParameterBitmap.ExecutionPolicy;
1026+
ParametersUsed |= GetExecutionPolicy(_executionPolicy);
9051027
}
9061028
else if (MatchSwitch(switchKey, "encodedcommand", "e") || MatchSwitch(switchKey, "ec", "e"))
9071029
{
@@ -910,13 +1032,17 @@ private void ParseHelper(string[] args)
9101032
{
9111033
break;
9121034
}
1035+
1036+
ParametersUsed |= ParameterBitmap.EncodedCommand;
9131037
}
9141038
else if (MatchSwitch(switchKey, "encodedarguments", "encodeda") || MatchSwitch(switchKey, "ea", "ea"))
9151039
{
9161040
if (!CollectArgs(args, ref i))
9171041
{
9181042
break;
9191043
}
1044+
1045+
ParametersUsed |= ParameterBitmap.EncodedArgument;
9201046
}
9211047
else if (MatchSwitch(switchKey, "settingsfile", "settings"))
9221048
{
@@ -925,6 +1051,8 @@ private void ParseHelper(string[] args)
9251051
{
9261052
break;
9271053
}
1054+
1055+
ParametersUsed |= ParameterBitmap.SettingsFile;
9281056
}
9291057
else if (MatchSwitch(switchKey, "sta", "sta"))
9301058
{
@@ -944,6 +1072,7 @@ private void ParseHelper(string[] args)
9441072
}
9451073

9461074
_staMode = true;
1075+
ParametersUsed |= ParameterBitmap.STA;
9471076
}
9481077
else if (MatchSwitch(switchKey, "mta", "mta"))
9491078
{
@@ -963,6 +1092,7 @@ private void ParseHelper(string[] args)
9631092
}
9641093

9651094
_staMode = false;
1095+
ParametersUsed |= ParameterBitmap.MTA;
9661096
}
9671097
else if (MatchSwitch(switchKey, "workingdirectory", "wo") || MatchSwitch(switchKey, "wd", "wd"))
9681098
{
@@ -975,6 +1105,7 @@ private void ParseHelper(string[] args)
9751105
}
9761106

9771107
_workingDirectory = args[i];
1108+
ParametersUsed |= ParameterBitmap.WorkingDirectory;
9781109
}
9791110
#if !UNIX
9801111
else if (MatchSwitch(switchKey, "removeworkingdirectorytrailingcharacter", "removeworkingdirectorytrailingcharacter"))
@@ -990,6 +1121,9 @@ private void ParseHelper(string[] args)
9901121
{
9911122
break;
9921123
}
1124+
1125+
// default to filename being the next argument.
1126+
ParametersUsed |= ParameterBitmap.File;
9931127
}
9941128
}
9951129

src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ internal static int Start(string bannerText, string helpText)
192192
// First check for and handle PowerShell running in a server mode.
193193
if (s_cpp.ServerMode)
194194
{
195-
ApplicationInsightsTelemetry.SendPSCoreStartupTelemetry("ServerMode");
195+
ApplicationInsightsTelemetry.SendPSCoreStartupTelemetry("ServerMode", s_cpp.ParametersUsedAsDouble);
196196
ProfileOptimization.StartProfile("StartupProfileData-ServerMode");
197197
StdIOProcessMediator.Run(
198198
initialCommand: s_cpp.InitialCommand,
@@ -202,7 +202,7 @@ internal static int Start(string bannerText, string helpText)
202202
}
203203
else if (s_cpp.SSHServerMode)
204204
{
205-
ApplicationInsightsTelemetry.SendPSCoreStartupTelemetry("SSHServer");
205+
ApplicationInsightsTelemetry.SendPSCoreStartupTelemetry("SSHServer", s_cpp.ParametersUsedAsDouble);
206206
ProfileOptimization.StartProfile("StartupProfileData-SSHServerMode");
207207
StdIOProcessMediator.Run(
208208
initialCommand: s_cpp.InitialCommand,
@@ -212,15 +212,15 @@ internal static int Start(string bannerText, string helpText)
212212
}
213213
else if (s_cpp.NamedPipeServerMode)
214214
{
215-
ApplicationInsightsTelemetry.SendPSCoreStartupTelemetry("NamedPipe");
215+
ApplicationInsightsTelemetry.SendPSCoreStartupTelemetry("NamedPipe", s_cpp.ParametersUsedAsDouble);
216216
ProfileOptimization.StartProfile("StartupProfileData-NamedPipeServerMode");
217217
RemoteSessionNamedPipeServer.RunServerMode(
218218
configurationName: s_cpp.ConfigurationName);
219219
exitCode = 0;
220220
}
221221
else if (s_cpp.SocketServerMode)
222222
{
223-
ApplicationInsightsTelemetry.SendPSCoreStartupTelemetry("SocketServerMode");
223+
ApplicationInsightsTelemetry.SendPSCoreStartupTelemetry("SocketServerMode", s_cpp.ParametersUsedAsDouble);
224224
ProfileOptimization.StartProfile("StartupProfileData-SocketServerMode");
225225
HyperVSocketMediator.Run(
226226
initialCommand: s_cpp.InitialCommand,
@@ -255,7 +255,7 @@ internal static int Start(string bannerText, string helpText)
255255
PSHost.IsStdOutputRedirected = Console.IsOutputRedirected;
256256

257257
// Send startup telemetry for ConsoleHost startup
258-
ApplicationInsightsTelemetry.SendPSCoreStartupTelemetry("Normal");
258+
ApplicationInsightsTelemetry.SendPSCoreStartupTelemetry("Normal", s_cpp.ParametersUsedAsDouble);
259259

260260
exitCode = s_theConsoleHost.Run(s_cpp, false);
261261
}

0 commit comments

Comments
 (0)