@@ -262,6 +262,9 @@ internal void AddSessionStateCmdletEntryToCache(SessionStateCmdletEntry entry, b
262
262
/// False if not. Null if command discovery should default to something reasonable
263
263
/// for the command discovered.
264
264
/// </param>
265
+ /// <param name="forCompletion">
266
+ /// True if this for parameter completion and script requirements should be ignored.
267
+ /// </param>
265
268
/// <returns>
266
269
/// </returns>
267
270
/// <exception cref="CommandNotFoundException">
@@ -271,22 +274,23 @@ internal void AddSessionStateCmdletEntryToCache(SessionStateCmdletEntry entry, b
271
274
/// If the security manager is preventing the command from running.
272
275
/// </exception>
273
276
internal CommandProcessorBase LookupCommandProcessor ( string commandName ,
274
- CommandOrigin commandOrigin , bool ? useLocalScope )
277
+ CommandOrigin commandOrigin , bool ? useLocalScope , bool forCompletion = false )
275
278
{
276
279
CommandProcessorBase processor = null ;
277
280
CommandInfo commandInfo = LookupCommandInfo ( commandName , commandOrigin ) ;
278
281
279
282
if ( commandInfo != null )
280
283
{
281
- processor = LookupCommandProcessor ( commandInfo , commandOrigin , useLocalScope , null ) ;
284
+ processor = LookupCommandProcessor ( commandInfo , commandOrigin , useLocalScope , null , forCompletion ) ;
285
+
282
286
// commandInfo.Name might be different than commandName - restore the original invocation name
283
287
processor . Command . MyInvocation . InvocationName = commandName ;
284
288
}
285
289
286
290
return processor ;
287
291
}
288
292
289
- internal static void VerifyRequiredModules ( ExternalScriptInfo scriptInfo , ExecutionContext context )
293
+ internal static void VerifyRequiredModules ( ExternalScriptInfo scriptInfo , ExecutionContext context , bool forCompletion = false )
290
294
{
291
295
// Check Required Modules
292
296
if ( scriptInfo . RequiresModules != null )
@@ -301,7 +305,7 @@ internal static void VerifyRequiredModules(ExternalScriptInfo scriptInfo, Execut
301
305
moduleManifestPath : null ,
302
306
manifestProcessingFlags : ModuleCmdletBase . ManifestProcessingFlags . LoadElements | ModuleCmdletBase . ManifestProcessingFlags . WriteErrors ,
303
307
error : out error ) ;
304
- if ( error != null )
308
+ if ( ! forCompletion && error is not null )
305
309
{
306
310
ScriptRequiresException scriptRequiresException =
307
311
new ScriptRequiresException (
@@ -316,9 +320,9 @@ internal static void VerifyRequiredModules(ExternalScriptInfo scriptInfo, Execut
316
320
}
317
321
}
318
322
319
- private CommandProcessorBase CreateScriptProcessorForSingleShell ( ExternalScriptInfo scriptInfo , ExecutionContext context , bool useLocalScope , SessionStateInternal sessionState )
323
+ private CommandProcessorBase CreateScriptProcessorForSingleShell ( ExternalScriptInfo scriptInfo , ExecutionContext context , bool useLocalScope , SessionStateInternal sessionState , bool forCompletion = false )
320
324
{
321
- VerifyScriptRequirements ( scriptInfo , Context ) ;
325
+ VerifyScriptRequirements ( scriptInfo , Context , forCompletion ) ;
322
326
323
327
if ( ! string . IsNullOrEmpty ( scriptInfo . RequiresApplicationID ) )
324
328
{
@@ -340,12 +344,18 @@ private CommandProcessorBase CreateScriptProcessorForSingleShell(ExternalScriptI
340
344
// #Requires -PSVersion
341
345
// #Requires -PSEdition
342
346
// #Requires -Module
343
- internal static void VerifyScriptRequirements ( ExternalScriptInfo scriptInfo , ExecutionContext context )
347
+ internal static void VerifyScriptRequirements ( ExternalScriptInfo scriptInfo , ExecutionContext context , bool forCompletion = false )
344
348
{
345
- VerifyElevatedPrivileges ( scriptInfo ) ;
346
- VerifyPSVersion ( scriptInfo ) ;
347
- VerifyPSEdition ( scriptInfo ) ;
348
- VerifyRequiredModules ( scriptInfo , context ) ;
349
+ // When completing script parameters we don't care if these requirements are met.
350
+ // VerifyRequiredModules will attempt to load the required modules which is useful for completion (so the correct types are loaded).
351
+ if ( ! forCompletion )
352
+ {
353
+ VerifyElevatedPrivileges ( scriptInfo ) ;
354
+ VerifyPSVersion ( scriptInfo ) ;
355
+ VerifyPSEdition ( scriptInfo ) ;
356
+ }
357
+
358
+ VerifyRequiredModules ( scriptInfo , context , forCompletion ) ;
349
359
}
350
360
351
361
internal static void VerifyPSVersion ( ExternalScriptInfo scriptInfo )
@@ -426,6 +436,9 @@ internal static void VerifyElevatedPrivileges(ExternalScriptInfo scriptInfo)
426
436
/// False if not. Null if command discovery should default to something reasonable
427
437
/// for the command discovered.
428
438
/// </param>
439
+ /// <param name="forCompletion">
440
+ /// True if this for parameter completion and script requirements should be ignored.
441
+ /// </param>
429
442
/// <param name="sessionState">The session state the commandInfo should be run in.</param>
430
443
/// <returns>
431
444
/// </returns>
@@ -436,7 +449,7 @@ internal static void VerifyElevatedPrivileges(ExternalScriptInfo scriptInfo)
436
449
/// If the security manager is preventing the command from running.
437
450
/// </exception>
438
451
internal CommandProcessorBase LookupCommandProcessor ( CommandInfo commandInfo ,
439
- CommandOrigin commandOrigin , bool ? useLocalScope , SessionStateInternal sessionState )
452
+ CommandOrigin commandOrigin , bool ? useLocalScope , SessionStateInternal sessionState , bool forCompletion = false )
440
453
{
441
454
CommandProcessorBase processor = null ;
442
455
@@ -482,7 +495,7 @@ internal CommandProcessorBase LookupCommandProcessor(CommandInfo commandInfo,
482
495
scriptInfo . SignatureChecked = true ;
483
496
try
484
497
{
485
- processor = CreateScriptProcessorForSingleShell ( scriptInfo , Context , useLocalScope ?? true , sessionState ) ;
498
+ processor = CreateScriptProcessorForSingleShell ( scriptInfo , Context , useLocalScope ?? true , sessionState , forCompletion ) ;
486
499
}
487
500
catch ( ScriptRequiresSyntaxException reqSyntaxException )
488
501
{
0 commit comments