File size: 40,589 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 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Management.Automation.Host;
using System.Management.Automation.Internal;
using System.Management.Automation.Remoting;
using Dbg = System.Management.Automation.Diagnostics;
namespace System.Management.Automation.Runspaces.Internal
{
/// <summary>
/// PowerShell client side proxy base which handles invocation
/// of powershell on a remote machine.
/// </summary>
internal sealed class ClientRemotePowerShell : IDisposable
{
#region Tracer
[TraceSource("CRPS", "ClientRemotePowerShell")]
private static readonly PSTraceSource s_tracer = PSTraceSource.GetTracer("CRPS", "ClientRemotePowerShellBase");
#endregion Tracer
#region Constructors
/// <summary>
/// Constructor which creates a client remote powershell.
/// </summary>
/// <param name="shell">Powershell instance.</param>
/// <param name="runspacePool">The runspace pool associated with
/// this shell</param>
internal ClientRemotePowerShell(PowerShell shell, RemoteRunspacePoolInternal runspacePool)
{
this.shell = shell;
clientRunspacePoolId = runspacePool.InstanceId;
this.runspacePool = runspacePool;
// retrieve the computer name from the runspacepool
// information so that it can be used in adding
// warning to host messages
computerName = runspacePool.ConnectionInfo.ComputerName;
}
#endregion Constructors
#region Internal Methods/Properties
/// <summary>
/// Instance Id associated with this
/// client remote powershell.
/// </summary>
internal Guid InstanceId
{
get
{
return PowerShell.InstanceId;
}
}
/// <summary>
/// PowerShell associated with this ClientRemotePowerShell.
/// </summary>
internal PowerShell PowerShell
{
get
{
return shell;
}
}
/// <summary>
/// Set the state information of the client powershell.
/// </summary>
/// <param name="stateInfo">State information to set.</param>
internal void SetStateInfo(PSInvocationStateInfo stateInfo)
{
shell.SetStateChanged(stateInfo);
}
/// <summary>
/// Whether input is available when this object is created.
/// </summary>
internal bool NoInput
{
get
{
return noInput;
}
}
/// <summary>
/// Input stream associated with this object.
/// </summary>
internal ObjectStreamBase InputStream
{
get
{
return inputstream;
}
set
{
inputstream = value;
if (inputstream != null && (inputstream.IsOpen || inputstream.Count > 0))
{
noInput = false;
}
else
{
noInput = true;
}
}
}
/// <summary>
/// Output stream associated with this object.
/// </summary>
internal ObjectStreamBase OutputStream
{
get
{
return outputstream;
}
set
{
outputstream = value;
}
}
/// <summary>
/// Data structure handler object.
/// </summary>
internal ClientPowerShellDataStructureHandler DataStructureHandler
{
get
{
return dataStructureHandler;
}
}
/// <summary>
/// Invocation settings associated with this
/// ClientRemotePowerShell.
/// </summary>
internal PSInvocationSettings Settings
{
get
{
return settings;
}
}
/// <summary>
/// Close the output, error and other collections
/// associated with the shell, so that the
/// enumerator does not block.
/// </summary>
internal void UnblockCollections()
{
shell.ClearRemotePowerShell();
outputstream.Close();
errorstream.Close();
inputstream?.Close();
}
/// <summary>
/// Stop the remote powershell asynchronously.
/// </summary>
/// <remarks>This method will be called from
/// within the lock on PowerShell. Hence no need
/// to lock</remarks>
internal void StopAsync()
{
// If we are in robust connection retry mode then auto-disconnect this command
// rather than try to stop it.
PSConnectionRetryStatus retryStatus = _connectionRetryStatus;
if ((retryStatus == PSConnectionRetryStatus.NetworkFailureDetected ||
retryStatus == PSConnectionRetryStatus.ConnectionRetryAttempt) &&
this.runspacePool.RunspacePoolStateInfo.State == RunspacePoolState.Opened)
{
// While in robust connection retry mode, this call forces robust connections
// to abort retries and go directly to auto-disconnect.
this.runspacePool.BeginDisconnect(null, null);
return;
}
// PowerShell CoreStop would have handled cases
// for NotStarted, Stopping and already Stopped
// so at this point, there is no need to make any
// check. The message simply needs to be sent
// across to the server
stopCalled = true;
dataStructureHandler.SendStopPowerShellMessage();
}
/// <summary>
/// </summary>
internal void SendInput()
{
dataStructureHandler.SendInput(this.inputstream);
}
/// <summary>
/// This event is raised, when a host call is for a remote pipeline
/// which this remote powershell wraps.
/// </summary>
internal event EventHandler<RemoteDataEventArgs<RemoteHostCall>> HostCallReceived;
/// <summary>
/// Initialize the client remote powershell instance.
/// </summary>
/// <param name="inputstream">Input for execution.</param>
/// <param name="errorstream">error stream to which
/// data needs to be written to</param>
/// <param name="informationalBuffers">informational buffers
/// which will hold debug, verbose and warning messages</param>
/// <param name="settings">settings based on which this powershell
/// needs to be executed</param>
/// <param name="outputstream">output stream to which data
/// needs to be written to</param>
internal void Initialize(
ObjectStreamBase inputstream, ObjectStreamBase outputstream,
ObjectStreamBase errorstream, PSInformationalBuffers informationalBuffers,
PSInvocationSettings settings)
{
initialized = true;
this.informationalBuffers = informationalBuffers;
InputStream = inputstream;
this.errorstream = errorstream;
this.outputstream = outputstream;
this.settings = settings;
if (settings == null || settings.Host == null)
{
hostToUse = runspacePool.Host;
}
else
{
hostToUse = settings.Host;
}
dataStructureHandler = runspacePool.DataStructureHandler.CreatePowerShellDataStructureHandler(this);
// register for events from the data structure handler
dataStructureHandler.InvocationStateInfoReceived += HandleInvocationStateInfoReceived;
dataStructureHandler.OutputReceived += HandleOutputReceived;
dataStructureHandler.ErrorReceived += HandleErrorReceived;
dataStructureHandler.InformationalMessageReceived += HandleInformationalMessageReceived;
dataStructureHandler.HostCallReceived += HandleHostCallReceived;
dataStructureHandler.ClosedNotificationFromRunspacePool += HandleCloseNotificationFromRunspacePool;
dataStructureHandler.BrokenNotificationFromRunspacePool += HandleBrokenNotificationFromRunspacePool;
dataStructureHandler.ConnectCompleted += HandleConnectCompleted;
dataStructureHandler.ReconnectCompleted += HandleConnectCompleted;
dataStructureHandler.RobustConnectionNotification += HandleRobustConnectionNotification;
dataStructureHandler.CloseCompleted += HandleCloseCompleted;
}
/// <summary>
/// Do any clean up operation per initialization here.
/// </summary>
internal void Clear()
{
initialized = false;
}
/// <summary>
/// If this client remote powershell has been initialized.
/// </summary>
internal bool Initialized
{
get
{
return initialized;
}
}
/// <summary>
/// </summary>
/// <param name="sender"></param>
/// <param name="eventArgs"></param>
internal static void ExitHandler(object sender, RemoteDataEventArgs<RemoteHostCall> eventArgs)
{
RemoteHostCall hostcall = eventArgs.Data;
if (hostcall.IsSetShouldExitOrPopRunspace)
{
return;
}
// use the method from the RemotePowerShell to indeed execute this call
ClientRemotePowerShell remotePowerShell = (ClientRemotePowerShell)sender;
remotePowerShell.ExecuteHostCall(hostcall);
}
/// <summary>
/// Attempts to reconnect or connect to a running command on a remote server,
/// which will resume events and data collection from the server.
/// If connectCmdInfo parameter is null then a reconnection is attempted and
/// it is assumed that the current client state is unchanged since disconnection.
/// If connectCmdInfo parameter is non-null then a connection is attempted to
/// the specified remote running command.
/// This is an asynchronous call and results will be reported in the ReconnectCompleted
/// or the ConnectCompleted call back as appropriate.
/// </summary>
/// <param name="connectCmdInfo">ConnectCommandInfo specifying remote command.</param>
internal void ConnectAsync(ConnectCommandInfo connectCmdInfo)
{
if (connectCmdInfo == null)
{
// Attempt to do a reconnect with the current PSRP client state.
this.dataStructureHandler.ReconnectAsync();
}
else
{
// First add this command DS handler to the remote runspace pool list.
Dbg.Assert(this.shell.RunspacePool != null, "Invalid runspace pool for this powershell object.");
this.shell.RunspacePool.RemoteRunspacePoolInternal.AddRemotePowerShellDSHandler(
this.InstanceId, this.dataStructureHandler);
// Now do the asynchronous connect.
this.dataStructureHandler.ConnectAsync();
}
}
/// <summary>
/// This event is fired when this PowerShell object receives a robust connection
/// notification from the transport.
/// </summary>
internal event EventHandler<PSConnectionRetryStatusEventArgs> RCConnectionNotification;
/// <summary>
/// Current remote connection retry status.
/// </summary>
internal PSConnectionRetryStatus ConnectionRetryStatus
{
get { return _connectionRetryStatus; }
}
#endregion Internal Methods/Properties
#region Private Methods
/// <summary>
/// An error record is received from the powershell at the
/// server side. It is added to the error collection of the
/// client powershell.
/// </summary>
/// <param name="sender">Sender of this event, unused.</param>
/// <param name="eventArgs">Arguments describing this event.</param>
private void HandleErrorReceived(object sender, RemoteDataEventArgs<ErrorRecord> eventArgs)
{
using (s_tracer.TraceEventHandlers())
{
shell.SetHadErrors(true);
errorstream.Write(eventArgs.Data);
}
}
/// <summary>
/// An output object is received from the powershell at the
/// server side. It is added to the output collection of the
/// client powershell.
/// </summary>
/// <param name="sender">Sender of this event, unused.</param>
/// <param name="eventArgs">Arguments describing this event.</param>
private void HandleOutputReceived(object sender, RemoteDataEventArgs<object> eventArgs)
{
using (s_tracer.TraceEventHandlers())
{
object data = eventArgs.Data;
try
{
outputstream.Write(data);
}
catch (PSInvalidCastException e)
{
shell.SetStateChanged(new PSInvocationStateInfo(PSInvocationState.Failed, e));
}
}
}
/// <summary>
/// The invocation state of the server powershell has changed.
/// The state of the client powershell is reflected accordingly.
/// </summary>
/// <param name="sender">Sender of this event, unused.</param>
/// <param name="eventArgs">Arguments describing this event.</param>
private void HandleInvocationStateInfoReceived(object sender,
RemoteDataEventArgs<PSInvocationStateInfo> eventArgs)
{
using (s_tracer.TraceEventHandlers())
{
PSInvocationStateInfo stateInfo = eventArgs.Data;
// we should not receive any transient state from
// the server
Dbg.Assert(!(stateInfo.State == PSInvocationState.Running ||
stateInfo.State == PSInvocationState.Stopping),
"Transient states should not be received from the server");
if (stateInfo.State == PSInvocationState.Disconnected)
{
SetStateInfo(stateInfo);
}
else if (stateInfo.State == PSInvocationState.Stopped ||
stateInfo.State == PSInvocationState.Failed ||
stateInfo.State == PSInvocationState.Completed)
{
// Special case for failure error due to ErrorCode==-2144108453 (no ShellId found).
// In this case terminate session since there is no longer a shell to communicate
// with.
bool terminateSession = false;
if (stateInfo.State == PSInvocationState.Failed)
{
PSRemotingTransportException remotingTransportException = stateInfo.Reason as PSRemotingTransportException;
terminateSession = (remotingTransportException != null) &&
(remotingTransportException.ErrorCode == System.Management.Automation.Remoting.Client.WSManNativeApi.ERROR_WSMAN_TARGETSESSION_DOESNOTEXIST);
}
// if state is completed or failed or stopped,
// then the collections need to be closed as
// well, else the enumerator will block
UnblockCollections();
if (stopCalled || terminateSession)
{
// Reset stop called flag.
stopCalled = false;
// if a Stop method has been called, then powershell
// would have already raised a Stopping event, after
// which only a Stopped should be raised
_stateInfoQueue.Enqueue(new PSInvocationStateInfo(PSInvocationState.Stopped,
stateInfo.Reason));
// If the stop call failed due to network problems then close the runspace
// since it is now unusable.
CheckAndCloseRunspaceAfterStop(stateInfo.Reason);
}
else
{
_stateInfoQueue.Enqueue(stateInfo);
}
// calling close async only after making sure all the internal members are prepared
// to handle close complete.
dataStructureHandler.CloseConnectionAsync(null);
}
}
}
/// <summary>
/// Helper method to check any error condition after a stop call
/// and close the remote runspace/pool if the stop call failed due
/// to network outage problems.
/// </summary>
/// <param name="ex">Exception.</param>
private void CheckAndCloseRunspaceAfterStop(Exception ex)
{
PSRemotingTransportException transportException = ex as PSRemotingTransportException;
if (transportException != null &&
(transportException.ErrorCode == System.Management.Automation.Remoting.Client.WSManNativeApi.ERROR_WSMAN_SENDDATA_CANNOT_CONNECT ||
transportException.ErrorCode == System.Management.Automation.Remoting.Client.WSManNativeApi.ERROR_WSMAN_SENDDATA_CANNOT_COMPLETE ||
transportException.ErrorCode == System.Management.Automation.Remoting.Client.WSManNativeApi.ERROR_WSMAN_TARGETSESSION_DOESNOTEXIST))
{
object rsObject = shell.GetRunspaceConnection();
if (rsObject is Runspace)
{
Runspace runspace = (Runspace)rsObject;
if (runspace.RunspaceStateInfo.State == RunspaceState.Opened)
{
try
{
runspace.Close();
}
catch (PSRemotingTransportException)
{ }
}
}
else if (rsObject is RunspacePool)
{
RunspacePool runspacePool = (RunspacePool)rsObject;
if (runspacePool.RunspacePoolStateInfo.State == RunspacePoolState.Opened)
{
try
{
runspacePool.Close();
}
catch (PSRemotingTransportException)
{ }
}
}
}
}
/// <summary>
/// Handler for handling any informational message received
/// from the server side.
/// </summary>
/// <param name="sender">Sender of this event, unused.</param>
/// <param name="eventArgs">Arguments describing this event.</param>
private void HandleInformationalMessageReceived(object sender,
RemoteDataEventArgs<InformationalMessage> eventArgs)
{
using (s_tracer.TraceEventHandlers())
{
InformationalMessage infoMessage = eventArgs.Data;
switch (infoMessage.DataType)
{
case RemotingDataType.PowerShellDebug:
{
informationalBuffers.AddDebug((DebugRecord)infoMessage.Message);
}
break;
case RemotingDataType.PowerShellVerbose:
{
informationalBuffers.AddVerbose((VerboseRecord)infoMessage.Message);
}
break;
case RemotingDataType.PowerShellWarning:
{
informationalBuffers.AddWarning((WarningRecord)infoMessage.Message);
}
break;
case RemotingDataType.PowerShellProgress:
{
ProgressRecord progress = (ProgressRecord)LanguagePrimitives.ConvertTo(infoMessage.Message,
typeof(ProgressRecord), System.Globalization.CultureInfo.InvariantCulture);
informationalBuffers.AddProgress(progress);
}
break;
case RemotingDataType.PowerShellInformationStream:
{
informationalBuffers.AddInformation((InformationRecord)infoMessage.Message);
}
break;
}
}
}
/// <summary>
/// </summary>
/// <param name="sender"></param>
/// <param name="eventArgs"></param>
private void HandleHostCallReceived(object sender, RemoteDataEventArgs<RemoteHostCall> eventArgs)
{
using (s_tracer.TraceEventHandlers())
{
Collection<RemoteHostCall> prerequisiteCalls =
eventArgs.Data.PerformSecurityChecksOnHostMessage(computerName);
if (HostCallReceived != null)
{
// raise events for all prerequisite calls
if (prerequisiteCalls.Count > 0)
{
foreach (RemoteHostCall hostcall in prerequisiteCalls)
{
RemoteDataEventArgs<RemoteHostCall> args =
new RemoteDataEventArgs<RemoteHostCall>(hostcall);
HostCallReceived.SafeInvoke(this, args);
}
}
HostCallReceived.SafeInvoke(this, eventArgs);
}
else
{
// execute any prerequisite calls before
// executing this host call
if (prerequisiteCalls.Count > 0)
{
foreach (RemoteHostCall hostcall in prerequisiteCalls)
{
ExecuteHostCall(hostcall);
}
}
ExecuteHostCall(eventArgs.Data);
}
}
}
/// <summary>
/// Handler for ConnectCompleted and ReconnectCompleted events from the
/// PSRP layer.
/// </summary>
/// <param name="sender">Sender of this event, unused.</param>
/// <param name="e">Event arguments.</param>
private void HandleConnectCompleted(object sender, RemoteDataEventArgs<Exception> e)
{
// After initial connect/reconnect set state to "Running". Later events
// will update state to appropriate command execution state.
SetStateInfo(new PSInvocationStateInfo(PSInvocationState.Running, null));
}
/// <summary>
/// This is need for the state change events that resulted in closing the underlying
/// datastructure handler. We cannot send the state back to the upper layers until
/// close is completed from the datastructure/transport layer. We have to send
/// the terminal state only when we know that underlying datastructure/transport
/// is closed.
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private void HandleCloseCompleted(object sender, EventArgs args)
{
// if state is completed or failed or stopped,
// then the collections need to be closed as
// well, else the enumerator will block
UnblockCollections();
// close the transport manager when CreateCloseAckPacket is received
// otherwise may have race conditions in Server.OutOfProcessMediator
dataStructureHandler.RaiseRemoveAssociationEvent();
if (_stateInfoQueue.Count == 0)
{
// If shell state is not finished on client side and queue is empty
// then set state to stopped unless the current state is Disconnected
// in which case transition state to failed.
if (!IsFinished(shell.InvocationStateInfo.State))
{
// If RemoteSessionStateEventArgs are provided then use them to set the
// session close reason when setting finished state.
RemoteSessionStateEventArgs sessionEventArgs = args as RemoteSessionStateEventArgs;
Exception closeReason = sessionEventArgs?.SessionStateInfo.Reason;
PSInvocationState finishedState = (shell.InvocationStateInfo.State == PSInvocationState.Disconnected) ?
PSInvocationState.Failed : PSInvocationState.Stopped;
SetStateInfo(new PSInvocationStateInfo(finishedState, closeReason));
}
}
else
{
// Apply queued state changes.
while (_stateInfoQueue.Count > 0)
{
PSInvocationStateInfo stateInfo = _stateInfoQueue.Dequeue();
SetStateInfo(stateInfo);
}
}
}
private static bool IsFinished(PSInvocationState state)
{
return (state == PSInvocationState.Completed ||
state == PSInvocationState.Failed ||
state == PSInvocationState.Stopped);
}
/// <summary>
/// Execute the specified host call.
/// </summary>
/// <param name="hostcall">Host call to execute.</param>
private void ExecuteHostCall(RemoteHostCall hostcall)
{
if (hostcall.IsVoidMethod)
{
if (hostcall.IsSetShouldExitOrPopRunspace)
{
this.shell.ClearRemotePowerShell();
}
hostcall.ExecuteVoidMethod(hostToUse);
}
else
{
RemoteHostResponse remoteHostResponse = hostcall.ExecuteNonVoidMethod(hostToUse);
dataStructureHandler.SendHostResponseToServer(remoteHostResponse);
}
}
/// <summary>
/// </summary>
/// <param name="sender"></param>
/// <param name="eventArgs"></param>
private void HandleCloseNotificationFromRunspacePool(object sender,
RemoteDataEventArgs<Exception> eventArgs)
{
// RunspacePool is closed...so going to set the state of PowerShell
// to stopped here.
// if state is completed or failed or stopped,
// then the collections need to be closed as
// well, else the enumerator will block
UnblockCollections();
// Since this is a terminal state..close the transport manager.
dataStructureHandler.RaiseRemoveAssociationEvent();
// RunspacePool is closed...so going to set the state of PowerShell
// to stopped here.
SetStateInfo(new PSInvocationStateInfo(PSInvocationState.Stopped,
eventArgs.Data));
// Not calling dataStructureHandler.CloseConnection() as this must
// have already been called by RunspacePool.Close()
}
/// <summary>
/// Handles notification from RunspacePool indicating
/// that the pool is broken. This sets the state of
/// all the powershell objects associated with the
/// runspace pool to Failed.
/// </summary>
/// <param name="sender">Sender of this information, unused.</param>
/// <param name="eventArgs">arguments describing this event
/// contains information on the reason associated with the
/// runspace pool entering a Broken state</param>
private void HandleBrokenNotificationFromRunspacePool(object sender,
RemoteDataEventArgs<Exception> eventArgs)
{
// RunspacePool is closed...so going to set the state of PowerShell
// to stopped here.
// if state is completed or failed or stopped,
// then the collections need to be closed as
// well, else the enumerator will block
UnblockCollections();
// Since this is a terminal state..close the transport manager.
dataStructureHandler.RaiseRemoveAssociationEvent();
if (stopCalled)
{
// Reset stop called flag.
stopCalled = false;
// if a Stop method has been called, then powershell
// would have already raised a Stopping event, after
// which only a Stopped should be raised
SetStateInfo(new PSInvocationStateInfo(PSInvocationState.Stopped,
eventArgs.Data));
}
else
{
SetStateInfo(new PSInvocationStateInfo(PSInvocationState.Failed,
eventArgs.Data));
}
// Not calling dataStructureHandler.CloseConnection() as this must
// have already been called by RunspacePool.Close()
}
/// <summary>
/// Handles a robust connection layer notification from the transport
/// manager.
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void HandleRobustConnectionNotification(
object sender,
ConnectionStatusEventArgs e)
{
// Create event arguments and warnings/errors for this robust connection notification.
PSConnectionRetryStatusEventArgs connectionRetryStatusArgs = null;
WarningRecord warningRecord = null;
ErrorRecord errorRecord = null;
int maxRetryConnectionTimeMSecs = this.runspacePool.MaxRetryConnectionTime;
int maxRetryConnectionTimeMinutes = maxRetryConnectionTimeMSecs / 60000;
switch (e.Notification)
{
case ConnectionStatus.NetworkFailureDetected:
warningRecord = new WarningRecord(
PSConnectionRetryStatusEventArgs.FQIDNetworkFailureDetected,
StringUtil.Format(RemotingErrorIdStrings.RCNetworkFailureDetected,
this.computerName, maxRetryConnectionTimeMinutes));
connectionRetryStatusArgs =
new PSConnectionRetryStatusEventArgs(PSConnectionRetryStatus.NetworkFailureDetected,
this.computerName, maxRetryConnectionTimeMSecs, warningRecord);
break;
case ConnectionStatus.ConnectionRetryAttempt:
warningRecord = new WarningRecord(
PSConnectionRetryStatusEventArgs.FQIDConnectionRetryAttempt,
StringUtil.Format(RemotingErrorIdStrings.RCConnectionRetryAttempt, this.computerName));
connectionRetryStatusArgs =
new PSConnectionRetryStatusEventArgs(PSConnectionRetryStatus.ConnectionRetryAttempt,
this.computerName, maxRetryConnectionTimeMSecs, warningRecord);
break;
case ConnectionStatus.ConnectionRetrySucceeded:
warningRecord = new WarningRecord(
PSConnectionRetryStatusEventArgs.FQIDConnectionRetrySucceeded,
StringUtil.Format(RemotingErrorIdStrings.RCReconnectSucceeded, this.computerName));
connectionRetryStatusArgs =
new PSConnectionRetryStatusEventArgs(PSConnectionRetryStatus.ConnectionRetrySucceeded,
this.computerName, maxRetryConnectionTimeMinutes, warningRecord);
break;
case ConnectionStatus.AutoDisconnectStarting:
{
warningRecord = new WarningRecord(
PSConnectionRetryStatusEventArgs.FQIDAutoDisconnectStarting,
StringUtil.Format(RemotingErrorIdStrings.RCAutoDisconnectingWarning, this.computerName));
connectionRetryStatusArgs =
new PSConnectionRetryStatusEventArgs(PSConnectionRetryStatus.AutoDisconnectStarting,
this.computerName, maxRetryConnectionTimeMinutes, warningRecord);
}
break;
case ConnectionStatus.AutoDisconnectSucceeded:
warningRecord = new WarningRecord(
PSConnectionRetryStatusEventArgs.FQIDAutoDisconnectSucceeded,
StringUtil.Format(RemotingErrorIdStrings.RCAutoDisconnected, this.computerName));
connectionRetryStatusArgs =
new PSConnectionRetryStatusEventArgs(PSConnectionRetryStatus.AutoDisconnectSucceeded,
this.computerName, maxRetryConnectionTimeMinutes, warningRecord);
break;
case ConnectionStatus.InternalErrorAbort:
{
string msg = StringUtil.Format(RemotingErrorIdStrings.RCInternalError, this.computerName);
RuntimeException reason = new RuntimeException(msg);
errorRecord = new ErrorRecord(reason,
PSConnectionRetryStatusEventArgs.FQIDNetworkOrDisconnectFailed,
ErrorCategory.InvalidOperation, this);
connectionRetryStatusArgs =
new PSConnectionRetryStatusEventArgs(PSConnectionRetryStatus.InternalErrorAbort,
this.computerName, maxRetryConnectionTimeMinutes, errorRecord);
}
break;
}
if (connectionRetryStatusArgs == null)
{
return;
}
// Update connection status.
_connectionRetryStatus = connectionRetryStatusArgs.Notification;
if (warningRecord != null)
{
RemotingWarningRecord remotingWarningRecord = new RemotingWarningRecord(
warningRecord,
new OriginInfo(this.computerName, this.InstanceId));
// Add warning record to information channel.
HandleInformationalMessageReceived(this,
new RemoteDataEventArgs<InformationalMessage>(
new InformationalMessage(remotingWarningRecord, RemotingDataType.PowerShellWarning)));
// Write warning to host.
RemoteHostCall writeWarning = new RemoteHostCall(
-100,
RemoteHostMethodId.WriteWarningLine,
new object[] { warningRecord.Message });
try
{
HandleHostCallReceived(this,
new RemoteDataEventArgs<RemoteHostCall>(writeWarning));
}
catch (PSNotImplementedException)
{ }
}
if (errorRecord != null)
{
RemotingErrorRecord remotingErrorRecord = new RemotingErrorRecord(
errorRecord,
new OriginInfo(this.computerName, this.InstanceId));
// Add error record to error channel, will also be written to host.
HandleErrorReceived(this,
new RemoteDataEventArgs<ErrorRecord>(remotingErrorRecord));
}
// Raise event.
RCConnectionNotification.SafeInvoke(this, connectionRetryStatusArgs);
}
#endregion Private Methods
#region Private Fields
private ObjectStreamBase inputstream;
private ObjectStreamBase errorstream;
private PSInformationalBuffers informationalBuffers;
private readonly PowerShell shell;
private readonly Guid clientRunspacePoolId;
private bool noInput;
private PSInvocationSettings settings;
private ObjectStreamBase outputstream;
private readonly string computerName;
private ClientPowerShellDataStructureHandler dataStructureHandler;
private bool stopCalled = false;
private PSHost hostToUse;
private readonly RemoteRunspacePoolInternal runspacePool;
private const string WRITE_DEBUG_LINE = "WriteDebugLine";
private const string WRITE_VERBOSE_LINE = "WriteVerboseLine";
private const string WRITE_WARNING_LINE = "WriteWarningLine";
private const string WRITE_PROGRESS = "WriteProgress";
private bool initialized = false;
/// <summary>
/// This queue is for the state change events that resulted in closing the underlying
/// datastructure handler. We cannot send the state back to the upper layers until
/// close is completed from the datastructure/transport layer.
/// </summary>
private readonly Queue<PSInvocationStateInfo> _stateInfoQueue = new Queue<PSInvocationStateInfo>();
private PSConnectionRetryStatus _connectionRetryStatus = PSConnectionRetryStatus.None;
#endregion Private Fields
#region IDisposable
/// <summary>
/// Release all resources.
/// </summary>
public void Dispose()
{
// inputstream.Dispose();
// outputstream.Dispose();
// errorstream.Dispose();
}
#endregion IDisposable
}
#region PSConnectionRetryStatusEventArgs
/// <summary>
/// Robust Connection notifications.
/// </summary>
internal enum PSConnectionRetryStatus
{
None = 0,
NetworkFailureDetected = 1,
ConnectionRetryAttempt = 2,
ConnectionRetrySucceeded = 3,
AutoDisconnectStarting = 4,
AutoDisconnectSucceeded = 5,
InternalErrorAbort = 6
}
/// <summary>
/// PSConnectionRetryStatusEventArgs.
/// </summary>
internal sealed class PSConnectionRetryStatusEventArgs : EventArgs
{
internal const string FQIDNetworkFailureDetected = "PowerShellNetworkFailureDetected";
internal const string FQIDConnectionRetryAttempt = "PowerShellConnectionRetryAttempt";
internal const string FQIDConnectionRetrySucceeded = "PowerShellConnectionRetrySucceeded";
internal const string FQIDAutoDisconnectStarting = "PowerShellNetworkFailedStartDisconnect";
internal const string FQIDAutoDisconnectSucceeded = "PowerShellAutoDisconnectSucceeded";
internal const string FQIDNetworkOrDisconnectFailed = "PowerShellNetworkOrDisconnectFailed";
internal PSConnectionRetryStatusEventArgs(
PSConnectionRetryStatus notification,
string computerName,
int maxRetryConnectionTime,
object infoRecord)
{
Notification = notification;
ComputerName = computerName;
MaxRetryConnectionTime = maxRetryConnectionTime;
InformationRecord = infoRecord;
}
internal PSConnectionRetryStatus Notification { get; }
internal string ComputerName { get; }
internal int MaxRetryConnectionTime { get; }
internal object InformationRecord { get; }
}
#endregion
}
|