1
1
// Copyright (c) Microsoft Corporation.
2
2
// Licensed under the MIT License.
3
-
4
3
#nullable enable
5
4
6
5
using System ;
@@ -195,6 +194,57 @@ internal static int MaxNameLength()
195
194
"workingdirectory"
196
195
} ;
197
196
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
+
198
248
[ Conditional ( "DEBUG" ) ]
199
249
private void AssertArgumentsParsed ( )
200
250
{
@@ -641,6 +691,53 @@ internal static string NormalizeFilePath(string path)
641
691
return Path . GetFullPath ( path ) ;
642
692
}
643
693
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
+
644
741
private static bool MatchSwitch ( string switchKey , string match , string smallestUnambiguousMatch )
645
742
{
646
743
Dbg . Assert ( ! string . IsNullOrEmpty ( match ) , "need a value" ) ;
@@ -755,6 +852,7 @@ private void ParseHelper(string[] args)
755
852
_noInteractive = true ;
756
853
_skipUserInit = true ;
757
854
_noExit = false ;
855
+ ParametersUsed |= ParameterBitmap . Version ;
758
856
break ;
759
857
}
760
858
@@ -763,48 +861,59 @@ private void ParseHelper(string[] args)
763
861
_showHelp = true ;
764
862
_showExtendedHelp = true ;
765
863
_abortStartup = true ;
864
+ ParametersUsed |= ParameterBitmap . Help ;
766
865
}
767
866
else if ( MatchSwitch ( switchKey , "login" , "l" ) )
768
867
{
769
868
// On Windows, '-Login' does nothing.
770
869
// On *nix, '-Login' is already handled much earlier to improve startup performance, so we do nothing here.
870
+ ParametersUsed |= ParameterBitmap . Login ;
771
871
}
772
872
else if ( MatchSwitch ( switchKey , "noexit" , "noe" ) )
773
873
{
774
874
_noExit = true ;
775
875
noexitSeen = true ;
876
+ ParametersUsed |= ParameterBitmap . NoExit ;
776
877
}
777
878
else if ( MatchSwitch ( switchKey , "noprofile" , "nop" ) )
778
879
{
779
880
_skipUserInit = true ;
881
+ ParametersUsed |= ParameterBitmap . NoProfile ;
780
882
}
781
883
else if ( MatchSwitch ( switchKey , "nologo" , "nol" ) )
782
884
{
783
885
_showBanner = false ;
886
+ ParametersUsed |= ParameterBitmap . NoLogo ;
784
887
}
785
888
else if ( MatchSwitch ( switchKey , "noninteractive" , "noni" ) )
786
889
{
787
890
_noInteractive = true ;
891
+ ParametersUsed |= ParameterBitmap . NonInteractive ;
788
892
}
789
893
else if ( MatchSwitch ( switchKey , "socketservermode" , "so" ) )
790
894
{
791
895
_socketServerMode = true ;
896
+ ParametersUsed |= ParameterBitmap . SocketServerMode ;
792
897
}
793
898
else if ( MatchSwitch ( switchKey , "servermode" , "s" ) )
794
899
{
795
900
_serverMode = true ;
901
+ ParametersUsed |= ParameterBitmap . ServerMode ;
796
902
}
797
903
else if ( MatchSwitch ( switchKey , "namedpipeservermode" , "nam" ) )
798
904
{
799
905
_namedPipeServerMode = true ;
906
+ ParametersUsed |= ParameterBitmap . NamedPipeServerMode ;
800
907
}
801
908
else if ( MatchSwitch ( switchKey , "sshservermode" , "sshs" ) )
802
909
{
803
910
_sshServerMode = true ;
911
+ ParametersUsed |= ParameterBitmap . SSHServerMode ;
804
912
}
805
913
else if ( MatchSwitch ( switchKey , "interactive" , "i" ) )
806
914
{
807
915
_noInteractive = false ;
916
+ ParametersUsed |= ParameterBitmap . Interactive ;
808
917
}
809
918
else if ( MatchSwitch ( switchKey , "configurationname" , "config" ) )
810
919
{
@@ -817,6 +926,7 @@ private void ParseHelper(string[] args)
817
926
}
818
927
819
928
_configurationName = args [ i ] ;
929
+ ParametersUsed |= ParameterBitmap . ConfigurationName ;
820
930
}
821
931
else if ( MatchSwitch ( switchKey , "custompipename" , "cus" ) )
822
932
{
@@ -841,14 +951,18 @@ private void ParseHelper(string[] args)
841
951
break ;
842
952
}
843
953
#endif
954
+
844
955
_customPipeName = args [ i ] ;
956
+ ParametersUsed |= ParameterBitmap . CustomPipeName ;
845
957
}
846
958
else if ( MatchSwitch ( switchKey , "command" , "c" ) )
847
959
{
848
960
if ( ! ParseCommand ( args , ref i , noexitSeen , false ) )
849
961
{
850
962
break ;
851
963
}
964
+
965
+ ParametersUsed |= ParameterBitmap . Command ;
852
966
}
853
967
else if ( MatchSwitch ( switchKey , "windowstyle" , "w" ) )
854
968
{
@@ -875,6 +989,8 @@ private void ParseHelper(string[] args)
875
989
string . Format ( CultureInfo . CurrentCulture , CommandLineParameterParserStrings . InvalidWindowStyleArgument , args [ i ] , e . Message ) ) ;
876
990
break ;
877
991
}
992
+
993
+ ParametersUsed |= ParameterBitmap . WindowStyle ;
878
994
#endif
879
995
}
880
996
else if ( MatchSwitch ( switchKey , "file" , "f" ) )
@@ -883,6 +999,8 @@ private void ParseHelper(string[] args)
883
999
{
884
1000
break ;
885
1001
}
1002
+
1003
+ ParametersUsed |= ParameterBitmap . File ;
886
1004
}
887
1005
#if DEBUG
888
1006
else if ( MatchSwitch ( switchKey , "isswait" , "isswait" ) )
@@ -894,14 +1012,18 @@ private void ParseHelper(string[] args)
894
1012
{
895
1013
ParseFormat ( args , ref i , ref _outFormat , CommandLineParameterParserStrings . MissingOutputFormatParameter ) ;
896
1014
_outputFormatSpecified = true ;
1015
+ ParametersUsed |= ParameterBitmap . OutputFormat ;
897
1016
}
898
1017
else if ( MatchSwitch ( switchKey , "inputformat" , "inp" ) || MatchSwitch ( switchKey , "if" , "if" ) )
899
1018
{
900
1019
ParseFormat ( args , ref i , ref _inFormat , CommandLineParameterParserStrings . MissingInputFormatParameter ) ;
1020
+ ParametersUsed |= ParameterBitmap . InputFormat ;
901
1021
}
902
1022
else if ( MatchSwitch ( switchKey , "executionpolicy ", "ex ") || MatchSwitch(switchKey, " ep", "ep"))
903
1023
{
904
1024
ParseExecutionPolicy ( args , ref i , ref _executionPolicy , CommandLineParameterParserStrings . MissingExecutionPolicyParameter ) ;
1025
+ ParametersUsed |= ParameterBitmap . ExecutionPolicy ;
1026
+ ParametersUsed |= GetExecutionPolicy ( _executionPolicy ) ;
905
1027
}
906
1028
else if ( MatchSwitch ( switchKey , "encodedcommand" , "e" ) || MatchSwitch ( switchKey , "ec" , "e" ) )
907
1029
{
@@ -910,13 +1032,17 @@ private void ParseHelper(string[] args)
910
1032
{
911
1033
break ;
912
1034
}
1035
+
1036
+ ParametersUsed |= ParameterBitmap . EncodedCommand ;
913
1037
}
914
1038
else if ( MatchSwitch ( switchKey , "encodedarguments" , "encodeda" ) || MatchSwitch ( switchKey , "ea" , "ea" ) )
915
1039
{
916
1040
if ( ! CollectArgs ( args , ref i ) )
917
1041
{
918
1042
break ;
919
1043
}
1044
+
1045
+ ParametersUsed |= ParameterBitmap . EncodedArgument ;
920
1046
}
921
1047
else if ( MatchSwitch ( switchKey , "settingsfile" , "settings" ) )
922
1048
{
@@ -925,6 +1051,8 @@ private void ParseHelper(string[] args)
925
1051
{
926
1052
break ;
927
1053
}
1054
+
1055
+ ParametersUsed |= ParameterBitmap . SettingsFile ;
928
1056
}
929
1057
else if ( MatchSwitch ( switchKey , "sta" , "sta" ) )
930
1058
{
@@ -944,6 +1072,7 @@ private void ParseHelper(string[] args)
944
1072
}
945
1073
946
1074
_staMode = true ;
1075
+ ParametersUsed |= ParameterBitmap . STA ;
947
1076
}
948
1077
else if ( MatchSwitch ( switchKey , "mta" , "mta" ) )
949
1078
{
@@ -963,6 +1092,7 @@ private void ParseHelper(string[] args)
963
1092
}
964
1093
965
1094
_staMode = false ;
1095
+ ParametersUsed |= ParameterBitmap . MTA ;
966
1096
}
967
1097
else if ( MatchSwitch ( switchKey , "workingdirectory" , "wo" ) || MatchSwitch ( switchKey , "wd" , "wd" ) )
968
1098
{
@@ -975,6 +1105,7 @@ private void ParseHelper(string[] args)
975
1105
}
976
1106
977
1107
_workingDirectory = args [ i ] ;
1108
+ ParametersUsed |= ParameterBitmap . WorkingDirectory ;
978
1109
}
979
1110
#if ! UNIX
980
1111
else if ( MatchSwitch ( switchKey , "removeworkingdirectorytrailingcharacter" , "removeworkingdirectorytrailingcharacter" ) )
@@ -990,6 +1121,9 @@ private void ParseHelper(string[] args)
990
1121
{
991
1122
break ;
992
1123
}
1124
+
1125
+ // default to filename being the next argument.
1126
+ ParametersUsed |= ParameterBitmap . File ;
993
1127
}
994
1128
}
995
1129
0 commit comments