File size: 38,449 Bytes
8c763fb | 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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections;
using System.Collections.Concurrent;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Management.Automation.Internal;
using Microsoft.PowerShell.Commands;
using Dbg = System.Management.Automation.Diagnostics;
namespace System.Management.Automation
{
/// <summary>
/// Class definition of CommandProcessor - This class provides interface to create
/// and execute commands written in CLS compliant languages.
/// </summary>
internal class CommandProcessor : CommandProcessorBase
{
#region ctor
static CommandProcessor()
{
s_constructInstanceCache = new ConcurrentDictionary<Type, Func<Cmdlet>>();
// Avoid jitting constructors some of the more commonly used cmdlets in SMA.dll - not meant to be
// exhaustive b/c many cmdlets aren't called at all, so we'd never even need an entry in the cache.
s_constructInstanceCache.GetOrAdd(typeof(ForEachObjectCommand), () => new ForEachObjectCommand());
s_constructInstanceCache.GetOrAdd(typeof(WhereObjectCommand), () => new WhereObjectCommand());
s_constructInstanceCache.GetOrAdd(typeof(ImportModuleCommand), () => new ImportModuleCommand());
s_constructInstanceCache.GetOrAdd(typeof(GetModuleCommand), () => new GetModuleCommand());
s_constructInstanceCache.GetOrAdd(typeof(GetHelpCommand), () => new GetHelpCommand());
s_constructInstanceCache.GetOrAdd(typeof(InvokeCommandCommand), () => new InvokeCommandCommand());
s_constructInstanceCache.GetOrAdd(typeof(GetCommandCommand), () => new GetCommandCommand());
s_constructInstanceCache.GetOrAdd(typeof(OutDefaultCommand), () => new OutDefaultCommand());
s_constructInstanceCache.GetOrAdd(typeof(OutHostCommand), () => new OutHostCommand());
s_constructInstanceCache.GetOrAdd(typeof(OutNullCommand), () => new OutNullCommand());
s_constructInstanceCache.GetOrAdd(typeof(SetStrictModeCommand), () => new SetStrictModeCommand());
s_constructInstanceCache.GetOrAdd(typeof(FormatDefaultCommand), () => new FormatDefaultCommand());
s_constructInstanceCache.GetOrAdd(typeof(OutLineOutputCommand), () => new OutLineOutputCommand());
}
/// <summary>
/// Initializes the new instance of CommandProcessor class.
/// </summary>
/// <param name="cmdletInfo">
/// The information about the cmdlet.
/// </param>
/// <param name="context">
/// PowerShell engine execution context for this command.
/// </param>
/// <exception cref="CommandNotFoundException">
/// If there was a failure creating an instance of the cmdlet type.
/// </exception>
internal CommandProcessor(CmdletInfo cmdletInfo, ExecutionContext context) : base(cmdletInfo)
{
this._context = context;
Init(cmdletInfo);
}
/// <summary>
/// This is the constructor for script as cmdlet.
/// </summary>
/// <param name="scriptCommandInfo">
/// The information about the cmdlet.
/// </param>
/// <param name="context">
/// PowerShell engine execution context for this command.
/// </param>
/// <param name="useLocalScope"></param>
/// <param name="sessionState"></param>
/// <param name="fromScriptFile">True when the script to be executed came from a file (as opposed to a function, or interactive input).</param>
internal CommandProcessor(IScriptCommandInfo scriptCommandInfo, ExecutionContext context, bool useLocalScope, bool fromScriptFile, SessionStateInternal sessionState)
: base(scriptCommandInfo as CommandInfo)
{
this._context = context;
this._useLocalScope = useLocalScope;
this._fromScriptFile = fromScriptFile;
this.CommandSessionState = sessionState;
Init(scriptCommandInfo);
}
#endregion ctor
#region internal members
/// <summary>
/// Returns a CmdletParameterBinderController for the specified command.
/// </summary>
/// <param name="command">
/// The cmdlet to bind parameters to.
/// </param>
/// <returns>
/// A new instance of a CmdletParameterBinderController.
/// </returns>
/// <exception cref="ArgumentException">
/// if <paramref name="command"/> is not a Cmdlet.
/// </exception>
internal ParameterBinderController NewParameterBinderController(InternalCommand command)
{
if (command is not Cmdlet cmdlet)
{
throw PSTraceSource.NewArgumentException(nameof(command));
}
ParameterBinderBase parameterBinder;
IScriptCommandInfo scriptCommandInfo = CommandInfo as IScriptCommandInfo;
if (scriptCommandInfo != null)
{
parameterBinder = new ScriptParameterBinder(scriptCommandInfo.ScriptBlock, cmdlet.MyInvocation, this._context, cmdlet, CommandScope);
}
else
{
parameterBinder = new ReflectionParameterBinder(cmdlet, cmdlet);
}
_cmdletParameterBinderController = new CmdletParameterBinderController(cmdlet, CommandInfo.CommandMetadata, parameterBinder);
return _cmdletParameterBinderController;
}
internal CmdletParameterBinderController CmdletParameterBinderController
{
get
{
if (_cmdletParameterBinderController == null)
{
NewParameterBinderController(this.Command);
}
return _cmdletParameterBinderController;
}
}
private CmdletParameterBinderController _cmdletParameterBinderController;
/// <summary>
/// Get the ObsoleteAttribute of the current command.
/// </summary>
internal override ObsoleteAttribute ObsoleteAttribute
{
get { return _obsoleteAttribute; }
}
private ObsoleteAttribute _obsoleteAttribute;
/// <summary>
/// Binds the specified command-line parameters to the target.
/// </summary>
/// <returns>
/// true if encode succeeds otherwise false.
/// </returns>
/// <exception cref="ParameterBindingException">
/// If any parameters fail to bind,
/// or
/// If any mandatory parameters are missing.
/// </exception>
/// <exception cref="MetadataException">
/// If there is an error generating the metadata for dynamic parameters.
/// </exception>
internal void BindCommandLineParameters()
{
using (commandRuntime.AllowThisCommandToWrite(false))
{
Diagnostics.Assert(
this.CmdletParameterBinderController != null,
"A parameter binder controller should always be available");
// Always set the hash table on MyInvocation so it's available for both interpreted cmdlets
// as well as compiled ones.
this.CmdletParameterBinderController.CommandLineParameters.UpdateInvocationInfo(this.Command.MyInvocation);
this.Command.MyInvocation.UnboundArguments = new Collections.Generic.List<object>();
this.CmdletParameterBinderController.BindCommandLineParameters(arguments);
}
}
/// <summary>
/// Prepares the command. Encodes the command-line parameters
/// JonN 2003-04-02 Split from Execute()
/// </summary>
/// <exception cref="ParameterBindingException">
/// If any parameters fail to bind,
/// or
/// If any mandatory parameters are missing.
/// </exception>
/// <exception cref="MetadataException">
/// If there is an error generating the metadata for dynamic parameters.
/// </exception>
internal override void Prepare(IDictionary psDefaultParameterValues)
{
// Note that Prepare() and DoBegin() should NOT be combined.
// Reason: Encoding of commandline parameters happen as part of
// Prepare(). If they are combined, the first command's
// DoBegin() will be called before the next command's Prepare().
// Since BeginProcessing() can write objects to the downstream
// commandlet, it will end up calling DoExecute() (from Pipe.Add())
// before Prepare.
// Steps involved:
// (1) Backup the default parameter values
// (2) Handle input objects - add them to the input pipe.
// (3) Bind the parameters to properties (encoding)
// (4) Execute the command method using DoExecute() (repeatedly)
this.CmdletParameterBinderController.DefaultParameterValues = psDefaultParameterValues;
Diagnostics.Assert(
this.Command != null,
"CommandProcessor did not initialize Command\n" + this.CommandInfo.Name);
PSLanguageMode? oldLanguageMode = null;
bool? oldLangModeTransitionStatus = null;
try
{
var scriptCmdletInfo = this.CommandInfo as IScriptCommandInfo;
if (scriptCmdletInfo != null &&
scriptCmdletInfo.ScriptBlock.LanguageMode.HasValue &&
scriptCmdletInfo.ScriptBlock.LanguageMode != Context.LanguageMode)
{
// Set the language mode before parameter binding if it's necessary for a script cmdlet, so that the language
// mode is appropriately applied for evaluating parameter defaults and argument type conversion.
oldLanguageMode = Context.LanguageMode;
Context.LanguageMode = scriptCmdletInfo.ScriptBlock.LanguageMode.Value;
// If it's from ConstrainedLanguage to FullLanguage, indicate the transition before parameter binding takes place.
// When transitioning to FullLanguage mode, we don't want any ConstrainedLanguage restrictions or incorrect Audit messages.
if (oldLanguageMode == PSLanguageMode.ConstrainedLanguage && Context.LanguageMode == PSLanguageMode.FullLanguage)
{
oldLangModeTransitionStatus = Context.LanguageModeTransitionInParameterBinding;
Context.LanguageModeTransitionInParameterBinding = true;
}
}
BindCommandLineParameters();
}
finally
{
if (oldLanguageMode.HasValue)
{
// Revert to the original language mode after doing the parameter binding
Context.LanguageMode = oldLanguageMode.Value;
}
if (oldLangModeTransitionStatus.HasValue)
{
// Revert the transition state to old value after doing the parameter binding
Context.LanguageModeTransitionInParameterBinding = oldLangModeTransitionStatus.Value;
}
}
}
protected override void OnSetCurrentScope()
{
// When dotting a script cmdlet, push the locals of automatic variables to
// the 'DottedScopes' of the current scope.
PSScriptCmdlet scriptCmdlet = this.Command as PSScriptCmdlet;
if (scriptCmdlet != null && !UseLocalScope)
{
scriptCmdlet.PushDottedScope(CommandSessionState.CurrentScope);
}
}
protected override void OnRestorePreviousScope()
{
// When dotting a script cmdlet, pop the locals of automatic variables from
// the 'DottedScopes' of the current scope.
PSScriptCmdlet scriptCmdlet = this.Command as PSScriptCmdlet;
if (scriptCmdlet != null && !UseLocalScope)
{
scriptCmdlet.PopDottedScope(CommandSessionState.CurrentScope);
}
}
/// <summary>
/// Execute BeginProcessing part of command.
/// </summary>
internal override void DoBegin()
{
if (!RanBeginAlready && CmdletParameterBinderController.ObsoleteParameterWarningList != null)
{
using (CommandRuntime.AllowThisCommandToWrite(false))
{
// Write out warning messages for the bound obsolete parameters.
// The warning message are generated during parameter binding, but we delay writing
// them out until now so that the -WarningAction will be respected as expected.
foreach (WarningRecord warningRecord in CmdletParameterBinderController.ObsoleteParameterWarningList)
{
CommandRuntime.WriteWarning(warningRecord);
}
}
// Clear up the warning message list
CmdletParameterBinderController.ObsoleteParameterWarningList.Clear();
}
base.DoBegin();
}
/// <summary>
/// This calls the command. It assumes that Prepare() has already been called.
/// JonN 2003-04-02 Split from Execute()
/// </summary>
/// <exception cref="PipelineStoppedException">
/// a terminating error occurred, or the pipeline was otherwise stopped
/// </exception>
internal override void ProcessRecord()
{
// Invoke the Command method with the request object
if (!this.RanBeginAlready)
{
RanBeginAlready = true;
try
{
using (commandRuntime.AllowThisCommandToWrite(true))
{
if (Context._debuggingMode > 0 && Command is not PSScriptCmdlet)
{
Context.Debugger.CheckCommand(this.Command.MyInvocation);
}
Command.DoBeginProcessing();
}
}
catch (Exception e)
{
// This cmdlet threw an exception, so wrap it and bubble it up.
throw ManageInvocationException(e);
}
}
Debug.Assert(this.Command.MyInvocation.PipelineIterationInfo != null); // this should have been allocated when the pipeline was started
while (Read())
{
Pipe oldErrorOutputPipe = _context.ShellFunctionErrorOutputPipe;
Exception exceptionToThrow = null;
try
{
//
// On V1 the output pipe was redirected to the command's output pipe only when it
// was already redirected. This is the original comment explaining this behaviour:
//
// NTRAID#Windows Out of Band Releases-926183-2005-12-15
// MonadTestHarness has a bad dependency on an artifact of the current implementation
// The following code only redirects the output pipe if it's already redirected
// to preserve the artifact. The test suites need to be fixed and then this
// the check can be removed and the assignment always done.
//
// However, this makes the hosting APIs behave differently than commands executed
// from the command-line host (for example, see bugs Win7:415915 and Win7:108670).
// The RedirectShellErrorOutputPipe flag is used by the V2 hosting API to force the
// redirection.
//
if (this.RedirectShellErrorOutputPipe || _context.ShellFunctionErrorOutputPipe != null)
{
_context.ShellFunctionErrorOutputPipe = this.commandRuntime.ErrorOutputPipe;
}
// NOTICE-2004/06/08-JonN 959638
using (commandRuntime.AllowThisCommandToWrite(true))
using (ParameterBinderBase.bindingTracer.TraceScope("CALLING ProcessRecord"))
{
if (CmdletParameterBinderController.ObsoleteParameterWarningList != null &&
CmdletParameterBinderController.ObsoleteParameterWarningList.Count > 0)
{
// Write out warning messages for the pipeline-value-bound obsolete parameters.
// The warning message are generated during parameter binding, but we delay writing
// them out until now so that the -WarningAction will be respected as expected.
foreach (WarningRecord warningRecord in CmdletParameterBinderController.ObsoleteParameterWarningList)
{
CommandRuntime.WriteWarning(warningRecord);
}
// Clear up the warning message list
CmdletParameterBinderController.ObsoleteParameterWarningList.Clear();
}
this.Command.MyInvocation.PipelineIterationInfo[this.Command.MyInvocation.PipelinePosition]++;
Command.DoProcessRecord();
}
}
catch (RuntimeException rte)
{
// Most exceptions get wrapped here, but an exception that originated from
// a throw statement should not get wrapped, so it is just rethrown.
if (rte.WasThrownFromThrowStatement)
{
throw;
}
exceptionToThrow = rte;
}
catch (LoopFlowException)
{
// Don't wrap LoopFlowException, we incorrectly raise a PipelineStoppedException
// which gets caught by a script try/catch if we wrap here.
throw;
}
catch (Exception e)
{
// Catch-all OK, 3rd party callout.
exceptionToThrow = e;
}
finally
{
_context.ShellFunctionErrorOutputPipe = oldErrorOutputPipe;
}
if (exceptionToThrow != null)
{
// This cmdlet threw an exception, so
// wrap it and bubble it up.
throw ManageInvocationException(exceptionToThrow);
}
}
}
#endregion public_methods
#region helper_methods
/// <summary>
/// Tells whether it is the first call to Read.
/// </summary>
private bool _firstCallToRead = true;
/// <summary>
/// Tells whether to bail out in the next call to Read.
/// </summary>
private bool _bailInNextCall;
/// <summary>
/// Populates the parameters specified from the pipeline.
/// </summary>
/// <returns>
/// A bool indicating whether read succeeded.
/// </returns>
/// <exception cref="ParameterBindingException">
/// If a parameter fails to bind.
/// or
/// If a mandatory parameter is missing.
/// </exception>
/// <exception cref="PipelineStoppedException">
/// The pipeline was already stopped.
/// </exception>
// 2003/10/07-JonN was public, now internal
internal sealed override bool Read()
{
// (1) If Read() is called for the first time and with pipe closed and
// no object in the input pipe and
// (typically for the first cmdlet in the pipe & during programmatic
// execution of a command), Read() will succeed (return true) for
// only one time (so that the
// (2) If Read() is called with some input objects in the pipeline, it
// processes the input
// object one at a time and adds parameters from the input object
// to the list of parameters. If
// added to the error pipe and Read() will continue to read the
// next object in the pipe.
// (3) Read() will return false if there are no objects in the pipe
// for processing.
// (4) Read() will return true if the parameters are encoded in the
// request - signals ready for execution.
// (5) Read() will refresh the properties that are encoded via pipeline
// parameters in the next
// call to Read() [To their default values, so that the
// next execution of the command will
// not work on previously specified parameter].
// If the flag 'bail in next call' is true, then bail out returning false.
if (_bailInNextCall)
return false;
// ProcessRecord() will loop on Command.Read(), and continue calling
// ProcessRecord() until the incoming pipe is empty. We need to
// stop this loop if a downstream cmdlet broke guidelines and
// "swallowed" a PipelineStoppedException.
Command.ThrowIfStopping();
// Prepare the default value parameter list if this is the first call to Read
if (_firstCallToRead)
{
_firstCallToRead = false;
if (!IsPipelineInputExpected())
{
// Cmdlet should operate only with command-line parameters
// Let the command Execute with the specified command line parameters
// And Read should return false in the next call.
_bailInNextCall = true;
return true;
}
}
// If this cmdlet has any members that could be bound
// from the pipeline, do that now. In fact, we always try and
// do it once anyway because this BindPipelineParameters() does
// the final binding stage in before executing the cmdlet.
bool mandatoryParametersSpecified = false;
while (!mandatoryParametersSpecified)
{
// Retrieve the object from the input pipeline
object inputObject = this.commandRuntime.InputPipe.Retrieve();
if (inputObject == AutomationNull.Value)
{
// no object in the pipeline, stop reading
Command.CurrentPipelineObject = null;
return false;
}
// If we are reading input for the first command in the pipeline increment PipelineIterationInfo[0], which is the number of items read from the input
if (this.Command.MyInvocation.PipelinePosition == 1)
{
this.Command.MyInvocation.PipelineIterationInfo[0]++;
}
try
{
// Process the input pipeline object
if (!ProcessInputPipelineObject(inputObject))
{
// The input object was not bound to any parameters of the cmdlet.
// Write a non-terminating error and continue with the next input
// object.
WriteInputObjectError(
inputObject,
ParameterBinderStrings.InputObjectNotBound,
"InputObjectNotBound");
continue;
}
}
catch (ParameterBindingException bindingError)
{
// Set the target and write the error
bindingError.ErrorRecord.SetTargetObject(inputObject);
ErrorRecord errorRecord =
new ErrorRecord(
bindingError.ErrorRecord,
bindingError);
this.commandRuntime._WriteErrorSkipAllowCheck(errorRecord);
continue;
}
Collection<MergedCompiledCommandParameter> missingMandatoryParameters;
using (ParameterBinderBase.bindingTracer.TraceScope(
"MANDATORY PARAMETER CHECK on cmdlet [{0}]",
this.CommandInfo.Name))
{
// Check for unbound mandatory parameters but don't prompt
mandatoryParametersSpecified =
this.CmdletParameterBinderController.HandleUnboundMandatoryParameters(out missingMandatoryParameters);
}
if (!mandatoryParametersSpecified)
{
string missingParameters =
CmdletParameterBinderController.BuildMissingParamsString(missingMandatoryParameters);
// Since the input object did not satisfy all mandatory parameters
// for the command, write an ErrorRecord to the error pipe with
// the target as the input object.
WriteInputObjectError(
inputObject,
ParameterBinderStrings.InputObjectMissingMandatory,
"InputObjectMissingMandatory",
missingParameters);
}
}
return true;
}
/// <summary>
/// Writes an ErrorRecord to the commands error pipe because the specified
/// input object was not bound to the command.
/// </summary>
/// <param name="inputObject">
/// The pipeline input object that was not bound.
/// </param>
/// <param name="resourceString">
/// The error message.
/// </param>
/// <param name="errorId">
/// The resource ID of the error message is also used as error ID
/// of the ErrorRecord.
/// </param>
/// <param name="args">
/// Additional arguments to be formatted into the error message that represented in <paramref name="resourceString"/>.
/// </param>
private void WriteInputObjectError(
object inputObject,
string resourceString,
string errorId,
params object[] args)
{
Type inputObjectType = inputObject?.GetType();
ParameterBindingException bindingException = new ParameterBindingException(
ErrorCategory.InvalidArgument,
this.Command.MyInvocation,
null,
null,
null,
inputObjectType,
resourceString,
errorId,
args);
ErrorRecord errorRecord =
new ErrorRecord(
bindingException,
errorId,
ErrorCategory.InvalidArgument,
inputObject);
errorRecord.SetInvocationInfo(this.Command.MyInvocation);
this.commandRuntime._WriteErrorSkipAllowCheck(errorRecord);
}
/// <summary>
/// Reads an object from an input pipeline and attempts to bind the parameters.
/// </summary>
/// <param name="inputObject">
/// The pipeline input object to be processed.
/// </param>
/// <returns>
/// False the pipeline input object was not bound in any way to the command.
/// </returns>
/// <exception cref="ParameterBindingException">
/// If a ShouldProcess parameter is specified but the cmdlet does not support
/// ShouldProcess.
/// or
/// If an error occurred trying to bind a parameter from the pipeline object.
/// </exception>
private bool ProcessInputPipelineObject(object inputObject)
{
PSObject inputToOperateOn = null;
// Use ETS to retrieve properties from the input object
// turn it into a shell object if it isn't one already...
// we depend on PSObject.AsPSObject() being idempotent - if
// it's already a shell object, don't encapsulate it again.
if (inputObject != null)
{
inputToOperateOn = PSObject.AsPSObject(inputObject);
}
Command.CurrentPipelineObject = inputToOperateOn;
return this.CmdletParameterBinderController.BindPipelineParameters(inputToOperateOn);
}
private static readonly ConcurrentDictionary<Type, Func<Cmdlet>> s_constructInstanceCache;
private static Cmdlet ConstructInstance(Type type)
{
// Call the default constructor if type derives from Cmdlet.
// Return null (and the caller will generate an appropriate error) if
// type does not derive from Cmdlet. We do it this way so the expensive type check
// is performed just once per type.
return s_constructInstanceCache.GetOrAdd(type,
t => Expression.Lambda<Func<Cmdlet>>(
typeof(Cmdlet).IsAssignableFrom(t)
? (Expression)Expression.New(t)
: Expression.Constant(null, typeof(Cmdlet))).Compile())();
}
/// <summary>
/// Initializes the command's request object.
/// </summary>
/// <param name="cmdletInformation">
/// The information about the cmdlet.
/// </param>
/// <exception cref="CmdletInvocationException">
/// If the constructor for the cmdlet threw an exception.
/// </exception>
/// <exception cref="MemberAccessException">
/// The type referenced by <paramref name="cmdletInformation"/> referred to an
/// abstract type or them member was invoked via a late-binding mechanism.
/// </exception>
/// <exception cref="TypeLoadException">
/// If <paramref name="cmdletInformation"/> refers to a type that is invalid.
/// </exception>
private void Init(CmdletInfo cmdletInformation)
{
Diagnostics.Assert(cmdletInformation != null, "Constructor should throw exception if LookupCommand returned null.");
Cmdlet newCmdlet = null;
Exception initError = null;
string errorIdAndResourceId = null;
string resourceStr = null;
try
{
// Create the request object
newCmdlet = ConstructInstance(cmdletInformation.ImplementingType);
if (newCmdlet == null)
{
// We could test the inheritance before constructing, but that's
// expensive. Much cheaper to just check for null.
initError = new InvalidCastException();
errorIdAndResourceId = "CmdletDoesNotDeriveFromCmdletType";
resourceStr = DiscoveryExceptions.CmdletDoesNotDeriveFromCmdletType;
}
}
catch (MemberAccessException memberAccessException)
{
initError = memberAccessException;
}
catch (TypeLoadException typeLoadException)
{
initError = typeLoadException;
}
catch (Exception e) // Catch-all OK, 3rd party callout.
{
// We don't have a Command or InvocationInfo at this point,
// since the command failed to initialize.
var commandException = new CmdletInvocationException(e, null);
// Log a command health event
MshLog.LogCommandHealthEvent(
this._context,
commandException,
Severity.Warning);
throw commandException;
}
if (initError != null)
{
// Log a command health event
MshLog.LogCommandHealthEvent(
this._context,
initError,
Severity.Warning);
CommandNotFoundException exception =
new CommandNotFoundException(
cmdletInformation.Name,
initError,
errorIdAndResourceId ?? "CmdletNotFoundException",
resourceStr ?? DiscoveryExceptions.CmdletNotFoundException,
initError.Message);
throw exception;
}
this.Command = newCmdlet;
this.CommandScope = Context.EngineSessionState.CurrentScope;
InitCommon();
}
private void Init(IScriptCommandInfo scriptCommandInfo)
{
var scriptCmdlet = new PSScriptCmdlet(scriptCommandInfo.ScriptBlock, UseLocalScope, FromScriptFile, _context);
this.Command = scriptCmdlet;
this.CommandScope = UseLocalScope
? this.CommandSessionState.NewScope(_fromScriptFile)
: this.CommandSessionState.CurrentScope;
if (UseLocalScope)
{
// Set the 'LocalsTuple' of the new scope to that of the scriptCmdlet
scriptCmdlet.SetLocalsTupleForNewScope(CommandScope);
}
InitCommon();
// If the script has been dotted, throw an error if it's from a different language mode.
if (!this.UseLocalScope)
{
ValidateCompatibleLanguageMode(scriptCommandInfo.ScriptBlock, _context, Command.MyInvocation);
}
}
private void InitCommon()
{
// set the metadata
this.Command.CommandInfo = this.CommandInfo;
// set the ObsoleteAttribute of the current command
_obsoleteAttribute = this.CommandInfo.CommandMetadata.Obsolete;
// set the execution context
this.Command.Context = this._context;
// Now set up the command runtime for this command.
try
{
this.commandRuntime = new MshCommandRuntime(_context, this.CommandInfo, this.Command);
this.Command.commandRuntime = this.commandRuntime;
}
catch (Exception e) // Catch-all OK, 3rd party callout.
{
// Log a command health event
MshLog.LogCommandHealthEvent(
this._context,
e,
Severity.Warning);
throw;
}
}
/// <summary>
/// Checks if user has requested help (for example passing "-?" parameter for a cmdlet)
/// and if yes, then returns the help target to display.
/// </summary>
/// <param name="helpTarget">Help target to request.</param>
/// <param name="helpCategory">Help category to request.</param>
/// <returns><see langword="true"/> if user requested help; <see langword="false"/> otherwise.</returns>
internal override bool IsHelpRequested(out string helpTarget, out HelpCategory helpCategory)
{
if (this.arguments != null)
{
foreach (CommandParameterInternal parameter in this.arguments)
{
Dbg.Assert(parameter != null, "CommandProcessor.arguments shouldn't have any null arguments");
if (parameter.IsDashQuestion())
{
helpCategory = HelpCategory.All;
// using InvocationName mainly to avoid bogus this.CommandInfo.Name
// (when CmdletInfo.Name is initialized from "cmdlet" declaration
// of a scriptblock and when "cmdlet" declaration doesn't specify any name)
if ((this.Command != null) && (this.Command.MyInvocation != null) &&
(!string.IsNullOrEmpty(this.Command.MyInvocation.InvocationName)))
{
helpTarget = this.Command.MyInvocation.InvocationName;
// Win8: 391035 get-help does not work properly for aliased cmdlets
// For aliased cmdlets/functions,example Initialize-Volume -> Format-Volume,
// MyInvocation.InvocationName is different from CommandInfo.Name
// - CommandInfo.Name points to Format-Volume
// - MyInvocation.InvocationName points to Initialize-Volume
if (string.Equals(this.Command.MyInvocation.InvocationName, this.CommandInfo.Name, StringComparison.OrdinalIgnoreCase))
{
helpCategory = this.CommandInfo.HelpCategory;
}
}
else
{
helpTarget = this.CommandInfo.Name;
helpCategory = this.CommandInfo.HelpCategory;
}
return true;
}
}
}
return base.IsHelpRequested(out helpTarget, out helpCategory);
}
#endregion helper_methods
}
}
|