File size: 40,406 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 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections.ObjectModel;
using Dbg = System.Management.Automation;
namespace System.Management.Automation
{
/// <summary>
/// The context of the core command that is being run. This
/// includes data like the user name and password, as well
/// as callbacks for streaming output, prompting, and progress.
///
/// This allows the providers to be called in a variety of situations.
/// The most common will be from the core cmdlets themselves but they
/// can also be called programmatically either by having the results
/// accumulated or by providing delegates for the various streams.
///
/// NOTE: USER Feedback mechanism are only enabled for the CoreCmdlet
/// case. This is because we have not seen a use-case for them in the
/// other scenarios.
/// </summary>
internal sealed class CmdletProviderContext
{
#region Trace object
/// <summary>
/// An instance of the PSTraceSource class used for trace output
/// using "CmdletProviderContext" as the category.
/// </summary>
[Dbg.TraceSource(
"CmdletProviderContext",
"The context under which a core command is being run.")]
private static readonly Dbg.PSTraceSource s_tracer =
Dbg.PSTraceSource.GetTracer("CmdletProviderContext",
"The context under which a core command is being run.");
#endregion Trace object
#region Constructor
/// <summary>
/// Constructs the context under which the core command providers
/// operate.
/// </summary>
/// <param name="executionContext">
/// The context of the engine.
/// </param>
/// <exception cref="ArgumentNullException">
/// If <paramref name="executionContext"/> is null.
/// </exception>
internal CmdletProviderContext(ExecutionContext executionContext)
{
if (executionContext == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(executionContext));
}
ExecutionContext = executionContext;
Origin = CommandOrigin.Internal;
Drive = executionContext.EngineSessionState.CurrentDrive;
if ((executionContext.CurrentCommandProcessor != null) &&
(executionContext.CurrentCommandProcessor.Command is Cmdlet))
{
_command = (Cmdlet)executionContext.CurrentCommandProcessor.Command;
}
}
/// <summary>
/// Constructs the context under which the core command providers
/// operate.
/// </summary>
/// <param name="executionContext">
/// The context of the engine.
/// </param>
/// <param name="origin">
/// The origin of the caller of this API
/// </param>
/// <exception cref="ArgumentNullException">
/// If <paramref name="executionContext"/> is null.
/// </exception>
internal CmdletProviderContext(ExecutionContext executionContext, CommandOrigin origin)
{
if (executionContext == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(executionContext));
}
ExecutionContext = executionContext;
Origin = origin;
}
/// <summary>
/// Constructs the context under which the core command providers
/// operate.
/// </summary>
/// <param name="command">
/// The command object that is running.
/// </param>
/// <param name="credentials">
/// The credentials the core command provider should use.
/// </param>
/// <param name="drive">
/// The drive under which this context should operate.
/// </param>
/// <exception cref="ArgumentNullException">
/// If <paramref name="command"/> is null.
/// </exception>
/// <exception cref="ArgumentException">
/// If <paramref name="command"/> contains a null Host or Context reference.
/// </exception>
internal CmdletProviderContext(
PSCmdlet command,
PSCredential credentials,
PSDriveInfo drive)
{
// verify the command parameter
if (command == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(command));
}
_command = command;
Origin = command.CommandOrigin;
if (credentials != null)
{
_credentials = credentials;
}
Drive = drive;
if (command.Host == null)
{
throw PSTraceSource.NewArgumentException("command.Host");
}
if (command.Context == null)
{
throw PSTraceSource.NewArgumentException("command.Context");
}
ExecutionContext = command.Context;
// Stream will default to true because command methods will be used.
PassThru = true;
_streamErrors = true;
}
/// <summary>
/// Constructs the context under which the core command providers
/// operate.
/// </summary>
/// <param name="command">
/// The command object that is running.
/// </param>
/// <param name="credentials">
/// The credentials the core command provider should use.
/// </param>
/// <exception cref="ArgumentNullException">
/// If <paramref name="command"/> is null.
/// </exception>
/// <exception cref="ArgumentException">
/// If <paramref name="command"/> contains a null Host or Context reference.
/// </exception>
internal CmdletProviderContext(
PSCmdlet command,
PSCredential credentials)
{
// verify the command parameter
if (command == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(command));
}
_command = command;
Origin = command.CommandOrigin;
if (credentials != null)
{
_credentials = credentials;
}
if (command.Host == null)
{
throw PSTraceSource.NewArgumentException("command.Host");
}
if (command.Context == null)
{
throw PSTraceSource.NewArgumentException("command.Context");
}
ExecutionContext = command.Context;
// Stream will default to true because command methods will be used.
PassThru = true;
_streamErrors = true;
}
/// <summary>
/// Constructs the context under which the core command providers
/// operate.
/// </summary>
/// <param name="command">
/// The command object that is running.
/// </param>
/// <exception cref="ArgumentNullException">
/// If <paramref name="command"/> is null.
/// </exception>
/// <exception cref="ArgumentException">
/// If <paramref name="command"/> contains a null Host or Context reference.
/// </exception>
internal CmdletProviderContext(
Cmdlet command)
{
// verify the command parameter
if (command == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(command));
}
_command = command;
Origin = command.CommandOrigin;
if (command.Context == null)
{
throw PSTraceSource.NewArgumentException("command.Context");
}
ExecutionContext = command.Context;
// Stream will default to true because command methods will be used.
PassThru = true;
_streamErrors = true;
}
/// <summary>
/// Constructs the context under which the core command providers
/// operate using an existing context.
/// </summary>
/// <param name="contextToCopyFrom">
/// A CmdletProviderContext instance to copy the filters, ExecutionContext,
/// Credentials, Drive, and Force options from.
/// </param>
/// <exception cref="ArgumentNullException">
/// If <paramref name="contextToCopyFrom"/> is null.
/// </exception>
internal CmdletProviderContext(
CmdletProviderContext contextToCopyFrom)
{
if (contextToCopyFrom == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(contextToCopyFrom));
}
ExecutionContext = contextToCopyFrom.ExecutionContext;
_command = contextToCopyFrom._command;
if (contextToCopyFrom.Credential != null)
{
_credentials = contextToCopyFrom.Credential;
}
Drive = contextToCopyFrom.Drive;
_force = contextToCopyFrom.Force;
this.CopyFilters(contextToCopyFrom);
SuppressWildcardExpansion = contextToCopyFrom.SuppressWildcardExpansion;
DynamicParameters = contextToCopyFrom.DynamicParameters;
Origin = contextToCopyFrom.Origin;
// Copy the stopping state incase the source context
// has already been signaled for stopping
Stopping = contextToCopyFrom.Stopping;
// add this context to the stop referral on the copied
// context
contextToCopyFrom.StopReferrals.Add(this);
_copiedContext = contextToCopyFrom;
}
#endregion Constructor
#region private properties
/// <summary>
/// If the constructor that takes a context to copy is
/// called, this will be set to the context being copied.
/// </summary>
private readonly CmdletProviderContext _copiedContext;
/// <summary>
/// The credentials under which the operation should run.
/// </summary>
private readonly PSCredential _credentials = PSCredential.Empty;
/// <summary>
/// The force parameter gives guidance to providers on how vigorously they
/// should try to perform an operation.
/// </summary>
private bool _force;
/// <summary>
/// The command which defines the context. This should not be
/// made visible to anyone and should only be set through the
/// constructor.
/// </summary>
private readonly Cmdlet _command;
/// <summary>
/// This makes the origin of the provider request visible to the internals.
/// </summary>
internal CommandOrigin Origin { get; } = CommandOrigin.Internal;
/// <summary>
/// This defines the default behavior for the WriteError method.
/// If it is true, a call to this method will result in an immediate call
/// to the command WriteError method, or to the writeErrorDelegate if
/// one has been supplied.
/// If it is false, the objects will be accumulated until the
/// GetErrorObjects method is called.
/// </summary>
private readonly bool _streamErrors;
/// <summary>
/// A collection in which objects that are written using the WriteObject(s)
/// methods are accumulated if <see cref="PassThru"/> is false.
/// </summary>
private Collection<PSObject> _accumulatedObjects = new Collection<PSObject>();
/// <summary>
/// A collection in which objects that are written using the WriteError
/// method are accumulated if <see cref="PassThru"/> is false.
/// </summary>
private Collection<ErrorRecord> _accumulatedErrorObjects = new Collection<ErrorRecord>();
/// <summary>
/// The instance of the provider that is currently executing in this context.
/// </summary>
private System.Management.Automation.Provider.CmdletProvider _providerInstance;
#endregion private properties
#region Internal properties
/// <summary>
/// Gets the execution context of the engine.
/// </summary>
internal ExecutionContext ExecutionContext { get; }
/// <summary>
/// Gets or sets the provider instance for the current
/// execution context.
/// </summary>
internal System.Management.Automation.Provider.CmdletProvider ProviderInstance
{
get
{
return _providerInstance;
}
set
{
_providerInstance = value;
}
}
/// <summary>
/// Copies the include, exclude, and provider filters from
/// the specified context to this context.
/// </summary>
/// <param name="context">
/// The context to copy the filters from.
/// </param>
private void CopyFilters(CmdletProviderContext context)
{
Dbg.Diagnostics.Assert(
context != null,
"The caller should have verified the context");
Include = context.Include;
Exclude = context.Exclude;
Filter = context.Filter;
}
internal void RemoveStopReferral() => _copiedContext?.StopReferrals.Remove(this);
#endregion Internal properties
#region Public properties
/// <summary>
/// Gets or sets the dynamic parameters for the context.
/// </summary>
internal object DynamicParameters { get; set; }
/// <summary>
/// Returns MyInvocation from the underlying cmdlet.
/// </summary>
internal InvocationInfo MyInvocation
{
get
{
if (_command != null)
{
return _command.MyInvocation;
}
else
{
return null;
}
}
}
/// <summary>
/// Determines if the Write* calls should be passed through to the command
/// instance if there is one. The default value is true.
/// </summary>
internal bool PassThru { get; set; }
/// <summary>
/// The drive associated with this context.
/// </summary>
/// <exception cref="ArgumentNullException">
/// If <paramref name="value"/> is null on set.
/// </exception>
internal PSDriveInfo Drive { get; set; }
/// <summary>
/// Gets the user name under which the operation should run.
/// </summary>
internal PSCredential Credential
{
get
{
PSCredential result = _credentials;
// If the username wasn't specified, use the drive credentials
if (_credentials == null && Drive != null)
{
result = Drive.Credential;
}
return result;
}
}
#region Transaction Support
/// <summary>
/// Gets the flag that determines if the command requested a transaction.
/// </summary>
internal bool UseTransaction
{
get
{
if ((_command != null) && (_command.CommandRuntime != null))
{
MshCommandRuntime mshRuntime = _command.CommandRuntime as MshCommandRuntime;
if (mshRuntime != null)
{
return mshRuntime.UseTransaction;
}
}
return false;
}
}
/// <summary>
/// Returns true if a transaction is available and active.
/// </summary>
public bool TransactionAvailable()
{
if (_command != null)
{
return _command.TransactionAvailable();
}
return false;
}
/// <summary>
/// Gets an object that surfaces the current PowerShell transaction.
/// When this object is disposed, PowerShell resets the active transaction.
/// </summary>
public PSTransactionContext CurrentPSTransaction
{
get
{
if (_command != null)
{
return _command.CurrentPSTransaction;
}
return null;
}
}
#endregion Transaction Support
/// <summary>
/// Gets or sets the Force property that is passed to providers.
/// </summary>
internal SwitchParameter Force
{
get { return _force; }
set { _force = value; }
}
/// <summary>
/// The provider specific filter that should be used when determining
/// which items an action should take place on.
/// </summary>
internal string Filter { get; set; }
/// <summary>
/// A glob string that signifies which items should be included when determining
/// which items the action should occur on.
/// </summary>
internal Collection<string> Include { get; private set; }
/// <summary>
/// A glob string that signifies which items should be excluded when determining
/// which items the action should occur on.
/// </summary>
internal Collection<string> Exclude { get; private set; }
/// <summary>
/// Gets or sets the property that tells providers (that
/// declare their own wildcard support) to suppress wildcard
/// expansion. This is set when the user specifies the
/// -LiteralPath parameter to one of the core commands.
/// </summary>
public bool SuppressWildcardExpansion { get; internal set; }
#region User feedback mechanisms
/// <summary>
/// Confirm the operation with the user.
/// </summary>
/// <param name="target">
/// Name of the target resource being acted upon
/// </param>
/// <remarks>true if-and-only-if the action should be performed</remarks>
/// <exception cref="PipelineStoppedException">
/// The ActionPreference.Stop or ActionPreference.Inquire policy
/// triggered a terminating error. The pipeline failure will be
/// ActionPreferenceStopException.
/// Also, this occurs if the pipeline was already stopped.
/// </exception>
internal bool ShouldProcess(
string target)
{
bool result = true;
if (_command != null)
{
result = _command.ShouldProcess(target);
}
return result;
}
/// <summary>
/// Confirm the operation with the user.
/// </summary>
/// <param name="target">
/// Name of the target resource being acted upon
/// </param>
/// <param name="action">What action was being performed.</param>
/// <remarks>true if-and-only-if the action should be performed</remarks>
/// <exception cref="PipelineStoppedException">
/// The ActionPreference.Stop or ActionPreference.Inquire policy
/// triggered a terminating error. The pipeline failure will be
/// ActionPreferenceStopException.
/// Also, this occurs if the pipeline was already stopped.
/// </exception>
internal bool ShouldProcess(
string target,
string action)
{
bool result = true;
if (_command != null)
{
result = _command.ShouldProcess(target, action);
}
return result;
}
/// <summary>
/// Confirm the operation with the user.
/// </summary>
/// <param name="verboseDescription">
/// This should contain a textual description of the action to be
/// performed. This is what will be displayed to the user for
/// ActionPreference.Continue.
/// </param>
/// <param name="verboseWarning">
/// This should contain a textual query of whether the action
/// should be performed, usually in the form of a question.
/// This is what will be displayed to the user for
/// ActionPreference.Inquire.
/// </param>
/// <param name="caption">
/// This is the caption of the window which may be displayed
/// if the user is prompted whether or not to perform the action.
/// It may be displayed by some hosts, but not all.
/// </param>
/// <remarks>true if-and-only-if the action should be performed</remarks>
/// <exception cref="PipelineStoppedException">
/// The ActionPreference.Stop or ActionPreference.Inquire policy
/// triggered a terminating error. The pipeline failure will be
/// ActionPreferenceStopException.
/// Also, this occurs if the pipeline was already stopped.
/// </exception>
internal bool ShouldProcess(
string verboseDescription,
string verboseWarning,
string caption)
{
bool result = true;
if (_command != null)
{
result = _command.ShouldProcess(
verboseDescription,
verboseWarning,
caption);
}
return result;
}
/// <summary>
/// Confirm the operation with the user.
/// </summary>
/// <param name="verboseDescription">
/// This should contain a textual description of the action to be
/// performed. This is what will be displayed to the user for
/// ActionPreference.Continue.
/// </param>
/// <param name="verboseWarning">
/// This should contain a textual query of whether the action
/// should be performed, usually in the form of a question.
/// This is what will be displayed to the user for
/// ActionPreference.Inquire.
/// </param>
/// <param name="caption">
/// This is the caption of the window which may be displayed
/// if the user is prompted whether or not to perform the action.
/// It may be displayed by some hosts, but not all.
/// </param>
/// <param name="shouldProcessReason">
/// Indicates the reason(s) why ShouldProcess returned what it returned.
/// Only the reasons enumerated in
/// <see cref="System.Management.Automation.ShouldProcessReason"/>
/// are returned.
/// </param>
/// <remarks>true if-and-only-if the action should be performed</remarks>
/// <exception cref="PipelineStoppedException">
/// The ActionPreference.Stop or ActionPreference.Inquire policy
/// triggered a terminating error. The pipeline failure will be
/// ActionPreferenceStopException.
/// Also, this occurs if the pipeline was already stopped.
/// </exception>
internal bool ShouldProcess(
string verboseDescription,
string verboseWarning,
string caption,
out ShouldProcessReason shouldProcessReason)
{
bool result = true;
if (_command != null)
{
result = _command.ShouldProcess(
verboseDescription,
verboseWarning,
caption,
out shouldProcessReason);
}
else
{
shouldProcessReason = ShouldProcessReason.None;
}
return result;
}
/// <summary>
/// Ask the user whether to continue/stop or break to a subshell.
/// </summary>
/// <param name="query">
/// Message to display to the user. This routine will append
/// the text "Continue" to ensure that people know what question
/// they are answering.
/// </param>
/// <param name="caption">
/// Dialog caption if the host uses a dialog.
/// </param>
/// <returns>
/// True if the user wants to continue, false if not.
/// </returns>
internal bool ShouldContinue(
string query,
string caption)
{
bool result = true;
if (_command != null)
{
result = _command.ShouldContinue(query, caption);
}
return result;
}
/// <summary>
/// Ask the user whether to continue/stop or break to a subshell.
/// </summary>
/// <param name="query">
/// Message to display to the user. This routine will append
/// the text "Continue" to ensure that people know what question
/// they are answering.
/// </param>
/// <param name="caption">
/// Dialog caption if the host uses a dialog.
/// </param>
/// <param name="yesToAll">
/// Indicates whether the user selected YesToAll
/// </param>
/// <param name="noToAll">
/// Indicates whether the user selected NoToAll
/// </param>
/// <returns>
/// True if the user wants to continue, false if not.
/// </returns>
internal bool ShouldContinue(
string query,
string caption,
ref bool yesToAll,
ref bool noToAll)
{
bool result = true;
if (_command != null)
{
result = _command.ShouldContinue(
query, caption, ref yesToAll, ref noToAll);
}
else
{
yesToAll = false;
noToAll = false;
}
return result;
}
/// <summary>
/// Writes the object to the Verbose pipe.
/// </summary>
/// <param name="text">
/// The string that needs to be written.
/// </param>
internal void WriteVerbose(string text) => _command?.WriteVerbose(text);
/// <summary>
/// Writes the object to the Warning pipe.
/// </summary>
/// <param name="text">
/// The string that needs to be written.
/// </param>
internal void WriteWarning(string text) => _command?.WriteWarning(text);
internal void WriteProgress(ProgressRecord record) => _command?.WriteProgress(record);
/// <summary>
/// Writes a debug string.
/// </summary>
/// <param name="text">
/// The String that needs to be written.
/// </param>
internal void WriteDebug(string text) => _command?.WriteDebug(text);
internal void WriteInformation(InformationRecord record) => _command?.WriteInformation(record);
internal void WriteInformation(object messageData, string[] tags) => _command?.WriteInformation(messageData, tags);
#endregion User feedback mechanisms
#endregion Public properties
#region Public methods
/// <summary>
/// Sets the filters that are used within this context.
/// </summary>
/// <param name="include">
/// The include filters which determines which items are included in
/// operations within this context.
/// </param>
/// <param name="exclude">
/// The exclude filters which determines which items are excluded from
/// operations within this context.
/// </param>
/// <param name="filter">
/// The provider specific filter for the operation.
/// </param>
internal void SetFilters(Collection<string> include, Collection<string> exclude, string filter)
{
Include = include;
Exclude = exclude;
Filter = filter;
}
/// <summary>
/// Gets an array of the objects that have been accumulated
/// and the clears the collection.
/// </summary>
/// <returns>
/// An object array of the objects that have been accumulated
/// through the WriteObject method.
/// </returns>
internal Collection<PSObject> GetAccumulatedObjects()
{
// Get the contents as an array
Collection<PSObject> results = _accumulatedObjects;
_accumulatedObjects = new Collection<PSObject>();
// Return the array
return results;
}
/// <summary>
/// Gets an array of the error objects that have been accumulated
/// and the clears the collection.
/// </summary>
/// <returns>
/// An object array of the objects that have been accumulated
/// through the WriteError method.
/// </returns>
internal Collection<ErrorRecord> GetAccumulatedErrorObjects()
{
// Get the contents as an array
Collection<ErrorRecord> results = _accumulatedErrorObjects;
_accumulatedErrorObjects = new Collection<ErrorRecord>();
// Return the array
return results;
}
/// <summary>
/// If there are any errors accumulated, the first error is thrown.
/// </summary>
/// <exception cref="ProviderInvocationException">
/// If a CmdletProvider wrote any exceptions to the error pipeline, it is
/// wrapped and then thrown.
/// </exception>
internal void ThrowFirstErrorOrDoNothing()
{
ThrowFirstErrorOrDoNothing(true);
}
/// <summary>
/// If there are any errors accumulated, the first error is thrown.
/// </summary>
/// <param name="wrapExceptionInProviderException">
/// If true, the error will be wrapped in a ProviderInvocationException before
/// being thrown. If false, the error will be thrown as is.
/// </param>
/// <exception cref="ProviderInvocationException">
/// If <paramref name="wrapExceptionInProviderException"/> is true, the
/// first exception that was written to the error pipeline by a CmdletProvider
/// is wrapped and thrown.
/// </exception>
/// <exception>
/// If <paramref name="wrapExceptionInProviderException"/> is false,
/// the first exception that was written to the error pipeline by a CmdletProvider
/// is thrown.
/// </exception>
internal void ThrowFirstErrorOrDoNothing(bool wrapExceptionInProviderException)
{
if (HasErrors())
{
Collection<ErrorRecord> errors = GetAccumulatedErrorObjects();
if (errors != null && errors.Count > 0)
{
// Throw the first exception
if (wrapExceptionInProviderException)
{
ProviderInfo providerInfo = null;
if (this.ProviderInstance != null)
{
providerInfo = this.ProviderInstance.ProviderInfo;
}
ProviderInvocationException e =
new ProviderInvocationException(
providerInfo,
errors[0]);
// Log a provider health event
MshLog.LogProviderHealthEvent(
this.ExecutionContext,
providerInfo != null ? providerInfo.Name : "unknown provider",
e,
Severity.Warning);
throw e;
}
else
{
throw errors[0].Exception;
}
}
}
}
/// <summary>
/// Writes all the accumulated errors to the specified context using WriteError.
/// </summary>
/// <param name="errorContext">
/// The context to write the errors to.
/// </param>
/// <exception cref="ArgumentNullException">
/// If <paramref name="errorContext"/> is null.
/// </exception>
internal void WriteErrorsToContext(CmdletProviderContext errorContext)
{
if (errorContext == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(errorContext));
}
if (HasErrors())
{
foreach (ErrorRecord errorRecord in GetAccumulatedErrorObjects())
{
errorContext.WriteError(errorRecord);
}
}
}
/// <summary>
/// Writes an object to the output.
/// </summary>
/// <param name="obj">
/// The object to be written.
/// </param>
/// <remarks>
/// If streaming is on and the writeObjectHandler was specified then the object
/// gets written to the writeObjectHandler. If streaming is on and the writeObjectHandler
/// was not specified and the command object was specified, the object gets written to
/// the WriteObject method of the command object.
/// If streaming is off the object gets written to an accumulator collection. The collection
/// of written object can be retrieved using the AccumulatedObjects method.
/// </remarks>
/// <exception cref="InvalidOperationException">
/// The CmdletProvider could not stream the results because no
/// cmdlet was specified to stream the output through.
/// </exception>
/// <exception cref="PipelineStoppedException">
/// If the pipeline has been signaled for stopping but
/// the provider calls this method.
/// </exception>
internal void WriteObject(object obj)
{
// Making sure to obey the StopProcessing by
// throwing an exception anytime a provider tries
// to WriteObject
if (Stopping)
{
PipelineStoppedException stopPipeline =
new PipelineStoppedException();
throw stopPipeline;
}
if (PassThru)
{
if (_command != null)
{
s_tracer.WriteLine("Writing to command pipeline");
// Since there was no writeObject handler use
// the command WriteObject method.
_command.WriteObject(obj);
}
else
{
// The flag was set for streaming but we have no where
// to stream to.
InvalidOperationException e =
PSTraceSource.NewInvalidOperationException(
SessionStateStrings.OutputStreamingNotEnabled);
throw e;
}
}
else
{
s_tracer.WriteLine("Writing to accumulated objects");
// Convert the object to a PSObject if it's not already
// one.
PSObject newObj = PSObject.AsPSObject(obj);
// Since we are not streaming, just add the object to the accumulatedObjects
_accumulatedObjects.Add(newObj);
}
}
/// <summary>
/// Writes the error to the pipeline or accumulates the error in an internal
/// buffer.
/// </summary>
/// <param name="errorRecord">
/// The error record to write to the pipeline or the internal buffer.
/// </param>
/// <exception cref="InvalidOperationException">
/// The CmdletProvider could not stream the error because no
/// cmdlet was specified to stream the output through.
/// </exception>
/// <exception cref="PipelineStoppedException">
/// If the pipeline has been signaled for stopping but
/// the provider calls this method.
/// </exception>
internal void WriteError(ErrorRecord errorRecord)
{
// Making sure to obey the StopProcessing by
// throwing an exception anytime a provider tries
// to WriteError
if (Stopping)
{
PipelineStoppedException stopPipeline =
new PipelineStoppedException();
throw stopPipeline;
}
if (_streamErrors)
{
if (_command != null)
{
s_tracer.WriteLine("Writing error package to command error pipe");
_command.WriteError(errorRecord);
}
else
{
InvalidOperationException e =
PSTraceSource.NewInvalidOperationException(
SessionStateStrings.ErrorStreamingNotEnabled);
throw e;
}
}
else
{
// Since we are not streaming, just add the object to the accumulatedErrorObjects
_accumulatedErrorObjects.Add(errorRecord);
if (errorRecord.ErrorDetails != null
&& errorRecord.ErrorDetails.TextLookupError != null)
{
Exception textLookupError = errorRecord.ErrorDetails.TextLookupError;
errorRecord.ErrorDetails.TextLookupError = null;
MshLog.LogProviderHealthEvent(
this.ExecutionContext,
this.ProviderInstance.ProviderInfo.Name,
textLookupError,
Severity.Warning);
}
}
}
/// <summary>
/// If the error pipeline hasn't been supplied a delegate or a command then this method
/// will determine if any errors have accumulated.
/// </summary>
/// <returns>
/// True if the errors are being accumulated and some errors have been accumulated. False otherwise.
/// </returns>
internal bool HasErrors()
{
return _accumulatedErrorObjects != null && _accumulatedErrorObjects.Count > 0;
}
/// <summary>
/// Call this on a separate thread when a provider is using
/// this context to do work. This method will call the StopProcessing
/// method of the provider.
/// </summary>
internal void StopProcessing()
{
Stopping = true;
// We don't need to catch any of the exceptions here because
// we are terminating the pipeline and any exception will
// be caught by the engine.
_providerInstance?.StopProcessing();
// Call the stop referrals if any
foreach (CmdletProviderContext referralContext in StopReferrals)
{
referralContext.StopProcessing();
}
}
internal bool Stopping { get; private set; }
/// <summary>
/// The list of contexts to which the StopProcessing calls
/// should be referred.
/// </summary>
internal Collection<CmdletProviderContext> StopReferrals { get; } = new Collection<CmdletProviderContext>();
internal bool HasIncludeOrExclude
{
get
{
return ((Include != null && Include.Count > 0) ||
(Exclude != null && Exclude.Count > 0));
}
}
#endregion Public methods
}
}
|