File size: 35,872 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 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections.Generic;
using System.Management.Automation.Internal;
using System.Management.Automation.Remoting;
using System.Management.Automation.Runspaces;
using System.Security.Principal;
using System.Threading;
using Dbg = System.Management.Automation.Diagnostics;
namespace System.Management.Automation
{
/// <summary>
/// This class wraps a PowerShell object. It is used to function
/// as a server side powershell.
/// </summary>
internal class ServerPowerShellDriver
{
#region Private Members
private bool _extraPowerShellAlreadyScheduled;
// extra PowerShell at the server to be run after localPowerShell
private readonly PowerShell _extraPowerShell;
// output buffer for the local PowerShell that is associated with this powershell driver
// associated with this powershell data structure handler object to handle all communications with the client
private readonly PSDataCollection<PSObject> _localPowerShellOutput;
// if the remaining data has been sent to the client before sending state information
private readonly bool[] _datasent = new bool[2];
// sync object for synchronizing sending data to client
private readonly object _syncObject = new object();
// there is no input when this driver was created
private readonly bool _noInput;
private readonly bool _addToHistory;
// the server remote host instance
// associated with this powershell
private readonly ServerRemoteHost _remoteHost;
// apartment state for this powershell
private readonly ApartmentState apartmentState;
// Handles nested invocation of PS drivers.
private readonly IRSPDriverInvoke _psDriverInvoker;
#endregion Private Members
#region Constructors
/// <summary>
/// Default constructor for creating ServerPowerShellDrivers.
/// </summary>
/// <param name="powershell">Decoded powershell object.</param>
/// <param name="extraPowerShell">Extra pipeline to be run after <paramref name="powershell"/> completes.</param>
/// <param name="noInput">Whether there is input for this powershell.</param>
/// <param name="clientPowerShellId">The client powershell id.</param>
/// <param name="clientRunspacePoolId">The client runspacepool id.</param>
/// <param name="runspacePoolDriver">runspace pool driver
/// which is creating this powershell driver</param>
/// <param name="apartmentState">Apartment state for this powershell.</param>
/// <param name="hostInfo">host info using which the host for
/// this powershell will be constructed</param>
/// <param name="streamOptions">Serialization options for the streams in this powershell.</param>
/// <param name="addToHistory">
/// true if the command is to be added to history list of the runspace. false, otherwise.
/// </param>
/// <param name="rsToUse">
/// If not null, this Runspace will be used to invoke Powershell.
/// If null, the RunspacePool pointed by <paramref name="runspacePoolDriver"/> will be used.
/// </param>
internal ServerPowerShellDriver(PowerShell powershell, PowerShell extraPowerShell, bool noInput, Guid clientPowerShellId,
Guid clientRunspacePoolId, ServerRunspacePoolDriver runspacePoolDriver,
ApartmentState apartmentState, HostInfo hostInfo, RemoteStreamOptions streamOptions,
bool addToHistory, Runspace rsToUse)
: this(powershell, extraPowerShell, noInput, clientPowerShellId, clientRunspacePoolId, runspacePoolDriver,
apartmentState, hostInfo, streamOptions, addToHistory, rsToUse, null)
{
}
/// <summary>
/// Default constructor for creating ServerPowerShellDrivers.
/// </summary>
/// <param name="powershell">Decoded powershell object.</param>
/// <param name="extraPowerShell">Extra pipeline to be run after <paramref name="powershell"/> completes.</param>
/// <param name="noInput">Whether there is input for this powershell.</param>
/// <param name="clientPowerShellId">The client powershell id.</param>
/// <param name="clientRunspacePoolId">The client runspacepool id.</param>
/// <param name="runspacePoolDriver">runspace pool driver
/// which is creating this powershell driver</param>
/// <param name="apartmentState">Apartment state for this powershell.</param>
/// <param name="hostInfo">host info using which the host for
/// this powershell will be constructed</param>
/// <param name="streamOptions">Serialization options for the streams in this powershell.</param>
/// <param name="addToHistory">
/// true if the command is to be added to history list of the runspace. false, otherwise.
/// </param>
/// <param name="rsToUse">
/// If not null, this Runspace will be used to invoke Powershell.
/// If null, the RunspacePool pointed by <paramref name="runspacePoolDriver"/> will be used.
/// </param>
/// <param name="output">
/// If not null, this is used as another source of output sent to the client.
/// </param>
internal ServerPowerShellDriver(PowerShell powershell, PowerShell extraPowerShell, bool noInput, Guid clientPowerShellId,
Guid clientRunspacePoolId, ServerRunspacePoolDriver runspacePoolDriver,
ApartmentState apartmentState, HostInfo hostInfo, RemoteStreamOptions streamOptions,
bool addToHistory, Runspace rsToUse, PSDataCollection<PSObject> output)
{
InstanceId = clientPowerShellId;
RunspacePoolId = clientRunspacePoolId;
RemoteStreamOptions = streamOptions;
this.apartmentState = apartmentState;
LocalPowerShell = powershell;
_extraPowerShell = extraPowerShell;
_localPowerShellOutput = new PSDataCollection<PSObject>();
_noInput = noInput;
_addToHistory = addToHistory;
_psDriverInvoker = runspacePoolDriver;
DataStructureHandler = runspacePoolDriver.DataStructureHandler.CreatePowerShellDataStructureHandler(clientPowerShellId, clientRunspacePoolId, RemoteStreamOptions, LocalPowerShell);
_remoteHost = DataStructureHandler.GetHostAssociatedWithPowerShell(hostInfo, runspacePoolDriver.ServerRemoteHost);
if (!noInput)
{
InputCollection = new PSDataCollection<object>();
InputCollection.ReleaseOnEnumeration = true;
InputCollection.IdleEvent += HandleIdleEvent;
}
RegisterPipelineOutputEventHandlers(_localPowerShellOutput);
if (LocalPowerShell != null)
{
RegisterPowerShellEventHandlers(LocalPowerShell);
_datasent[0] = false;
}
if (extraPowerShell != null)
{
RegisterPowerShellEventHandlers(extraPowerShell);
_datasent[1] = false;
}
RegisterDataStructureHandlerEventHandlers(DataStructureHandler);
// set the runspace pool and invoke this powershell
if (rsToUse != null)
{
LocalPowerShell.Runspace = rsToUse;
if (extraPowerShell != null)
{
extraPowerShell.Runspace = rsToUse;
}
}
else
{
LocalPowerShell.RunspacePool = runspacePoolDriver.RunspacePool;
if (extraPowerShell != null)
{
extraPowerShell.RunspacePool = runspacePoolDriver.RunspacePool;
}
}
if (output != null)
{
output.DataAdded += (sender, args) =>
{
if (_localPowerShellOutput.IsOpen)
{
var items = output.ReadAll();
foreach (var item in items)
{
_localPowerShellOutput.Add(item);
}
}
};
}
}
#endregion Constructors
#region Internal Methods
/// <summary>
/// Input collection sync object.
/// </summary>
internal PSDataCollection<object> InputCollection { get; }
/// <summary>
/// Local PowerShell instance.
/// </summary>
internal PowerShell LocalPowerShell { get; }
/// <summary>
/// Instance id by which this powershell driver is
/// identified. This is the same as the id of the
/// powershell on the client side.
/// </summary>
internal Guid InstanceId { get; }
/// <summary>
/// Serialization options for the streams in this powershell.
/// </summary>
internal RemoteStreamOptions RemoteStreamOptions { get; }
/// <summary>
/// Id of the runspace pool driver which created
/// this object. This is the same as the id of
/// the runspace pool at the client side which
/// is associated with the powershell on the
/// client side.
/// </summary>
internal Guid RunspacePoolId { get; }
/// <summary>
/// ServerPowerShellDataStructureHandler associated with this
/// powershell driver.
/// </summary>
internal ServerPowerShellDataStructureHandler DataStructureHandler { get; }
private PSInvocationSettings PrepInvoke(bool startMainPowerShell)
{
if (startMainPowerShell)
{
// prepare transport manager for sending and receiving data.
DataStructureHandler.Prepare();
}
PSInvocationSettings settings = new PSInvocationSettings();
settings.ApartmentState = apartmentState;
settings.Host = _remoteHost;
// Flow the impersonation policy to pipeline execution thread
// only if the current thread is impersonated (Delegation is
// also a kind of impersonation).
if (Platform.IsWindows)
{
WindowsIdentity currentThreadIdentity = WindowsIdentity.GetCurrent();
switch (currentThreadIdentity.ImpersonationLevel)
{
case TokenImpersonationLevel.Impersonation:
case TokenImpersonationLevel.Delegation:
settings.FlowImpersonationPolicy = true;
break;
default:
settings.FlowImpersonationPolicy = false;
break;
}
}
else
{
settings.FlowImpersonationPolicy = false;
}
settings.AddToHistory = _addToHistory;
return settings;
}
private IAsyncResult Start(bool startMainPowerShell)
{
PSInvocationSettings settings = PrepInvoke(startMainPowerShell);
if (startMainPowerShell)
{
return LocalPowerShell.BeginInvoke<object, PSObject>(InputCollection, _localPowerShellOutput, settings, null, null);
}
else
{
return _extraPowerShell.BeginInvoke<object, PSObject>(InputCollection, _localPowerShellOutput, settings, null, null);
}
}
/// <summary>
/// Invokes the powershell asynchronously.
/// </summary>
internal IAsyncResult Start()
{
return Start(true);
}
/// <summary>
/// Runs no command but allows the PowerShell object on the client
/// to complete. This is used for running "virtual" remote debug
/// commands that sets debugger state but doesn't run any command
/// on the server runspace.
/// </summary>
/// <param name="output">The output from preprocessing that we want to send to the client.</param>
internal void RunNoOpCommand(IReadOnlyCollection<object> output)
{
if (LocalPowerShell != null)
{
System.Threading.ThreadPool.QueueUserWorkItem(
(state) =>
{
LocalPowerShell.SetStateChanged(
new PSInvocationStateInfo(
PSInvocationState.Running, null));
foreach (var item in output)
{
if (item != null)
{
_localPowerShellOutput.Add(PSObject.AsPSObject(item));
}
}
LocalPowerShell.SetStateChanged(
new PSInvocationStateInfo(
PSInvocationState.Completed, null));
});
}
}
/// <summary>
/// Invokes the Main PowerShell object synchronously.
/// </summary>
internal void InvokeMain()
{
PSInvocationSettings settings = PrepInvoke(true);
Exception ex = null;
try
{
LocalPowerShell.InvokeWithDebugger(InputCollection, _localPowerShellOutput, settings, true);
}
catch (Exception e)
{
ex = e;
}
if (ex != null)
{
// Since this is being invoked asynchronously on a single pipeline thread
// any invoke failures (such as possible debugger failures) need to be
// passed back to client or the original client invoke request will not respond.
string failedCommand = LocalPowerShell.Commands.Commands[0].CommandText;
LocalPowerShell.Commands.Clear();
string msg = StringUtil.Format(
RemotingErrorIdStrings.ServerSideNestedCommandInvokeFailed,
failedCommand ?? string.Empty,
ex.Message ?? string.Empty);
LocalPowerShell.AddCommand("Write-Error").AddArgument(msg);
LocalPowerShell.Invoke();
}
}
#endregion Internal Methods
#region Private Methods
private void RegisterPowerShellEventHandlers(PowerShell powerShell)
{
powerShell.InvocationStateChanged += HandlePowerShellInvocationStateChanged;
powerShell.Streams.Error.DataAdded += HandleErrorDataAdded;
powerShell.Streams.Debug.DataAdded += HandleDebugAdded;
powerShell.Streams.Verbose.DataAdded += HandleVerboseAdded;
powerShell.Streams.Warning.DataAdded += HandleWarningAdded;
powerShell.Streams.Progress.DataAdded += HandleProgressAdded;
powerShell.Streams.Information.DataAdded += HandleInformationAdded;
}
private void UnregisterPowerShellEventHandlers(PowerShell powerShell)
{
powerShell.InvocationStateChanged -= HandlePowerShellInvocationStateChanged;
powerShell.Streams.Error.DataAdded -= HandleErrorDataAdded;
powerShell.Streams.Debug.DataAdded -= HandleDebugAdded;
powerShell.Streams.Verbose.DataAdded -= HandleVerboseAdded;
powerShell.Streams.Warning.DataAdded -= HandleWarningAdded;
powerShell.Streams.Progress.DataAdded -= HandleProgressAdded;
powerShell.Streams.Information.DataAdded -= HandleInformationAdded;
}
private void RegisterDataStructureHandlerEventHandlers(ServerPowerShellDataStructureHandler dsHandler)
{
dsHandler.InputEndReceived += HandleInputEndReceived;
dsHandler.InputReceived += HandleInputReceived;
dsHandler.StopPowerShellReceived += HandleStopReceived;
dsHandler.HostResponseReceived += HandleHostResponseReceived;
dsHandler.OnSessionConnected += HandleSessionConnected;
}
private void UnregisterDataStructureHandlerEventHandlers(ServerPowerShellDataStructureHandler dsHandler)
{
dsHandler.InputEndReceived -= HandleInputEndReceived;
dsHandler.InputReceived -= HandleInputReceived;
dsHandler.StopPowerShellReceived -= HandleStopReceived;
dsHandler.HostResponseReceived -= HandleHostResponseReceived;
dsHandler.OnSessionConnected -= HandleSessionConnected;
}
private void RegisterPipelineOutputEventHandlers(PSDataCollection<PSObject> pipelineOutput)
{
pipelineOutput.DataAdded += HandleOutputDataAdded;
}
private void UnregisterPipelineOutputEventHandlers(PSDataCollection<PSObject> pipelineOutput)
{
pipelineOutput.DataAdded -= HandleOutputDataAdded;
}
/// <summary>
/// Handle state changed information from PowerShell
/// and send it to the client.
/// </summary>
/// <param name="sender">Sender of this event.</param>
/// <param name="eventArgs">arguments describing state changed
/// information for this powershell</param>
private void HandlePowerShellInvocationStateChanged(object sender,
PSInvocationStateChangedEventArgs eventArgs)
{
PSInvocationState state = eventArgs.InvocationStateInfo.State;
switch (state)
{
case PSInvocationState.Completed:
case PSInvocationState.Failed:
case PSInvocationState.Stopped:
{
if (LocalPowerShell.RunningExtraCommands)
{
// If completed successfully then allow extra commands to run.
if (state == PSInvocationState.Completed)
{
return;
}
// For failed or stopped state, extra commands cannot run and
// we allow this command invocation to finish.
}
// send the remaining data before sending in
// state information. This is required because
// the client side runspace pool will remove
// the association with the client side powershell
// once the powershell reaches a terminal state.
// If the association is removed, then any data
// sent to the powershell will be discarded by
// the runspace pool data structure handler on the client side
SendRemainingData();
if (state == PSInvocationState.Completed &&
(_extraPowerShell != null) &&
!_extraPowerShellAlreadyScheduled)
{
_extraPowerShellAlreadyScheduled = true;
Start(false);
}
else
{
DataStructureHandler.RaiseRemoveAssociationEvent();
// send the state change notification to the client
DataStructureHandler.SendStateChangedInformationToClient(
eventArgs.InvocationStateInfo);
UnregisterPowerShellEventHandlers(LocalPowerShell);
if (_extraPowerShell != null)
{
UnregisterPowerShellEventHandlers(_extraPowerShell);
}
UnregisterDataStructureHandlerEventHandlers(DataStructureHandler);
UnregisterPipelineOutputEventHandlers(_localPowerShellOutput);
// BUGBUG: currently the local powershell cannot
// be disposed as raising the events is
// not done towards the end. Need to fix
// powershell in order to get this enabled
// localPowerShell.Dispose();
}
}
break;
case PSInvocationState.Stopping:
{
// abort all pending host calls
_remoteHost.ServerMethodExecutor.AbortAllCalls();
}
break;
}
}
/// <summary>
/// Handles DataAdded event from the Output of the powershell.
/// </summary>
/// <param name="sender">Sender of this information.</param>
/// <param name="e">Arguments describing this event.</param>
private void HandleOutputDataAdded(object sender, DataAddedEventArgs e)
{
int index = e.Index;
lock (_syncObject)
{
int indexIntoDataSent = (!_extraPowerShellAlreadyScheduled) ? 0 : 1;
if (!_datasent[indexIntoDataSent])
{
PSObject data = _localPowerShellOutput[index];
// once send the output is removed so that the same
// is not sent again by SendRemainingData() method
_localPowerShellOutput.RemoveAt(index);
// send the output data to the client
DataStructureHandler.SendOutputDataToClient(data);
}
}
}
/// <summary>
/// Handles DataAdded event from Error of the PowerShell.
/// </summary>
/// <param name="sender">Sender of this event.</param>
/// <param name="e">Arguments describing this event.</param>
private void HandleErrorDataAdded(object sender, DataAddedEventArgs e)
{
int index = e.Index;
lock (_syncObject)
{
int indexIntoDataSent = (!_extraPowerShellAlreadyScheduled) ? 0 : 1;
if ((indexIntoDataSent == 0) && (!_datasent[indexIntoDataSent]))
{
ErrorRecord errorRecord = LocalPowerShell.Streams.Error[index];
// once send the error record is removed so that the same
// is not sent again by SendRemainingData() method
LocalPowerShell.Streams.Error.RemoveAt(index);
// send the error record to the client
DataStructureHandler.SendErrorRecordToClient(errorRecord);
}
}
}
/// <summary>
/// Handles DataAdded event from Progress of PowerShell.
/// </summary>
/// <param name="sender">Sender of this information, unused.</param>
/// <param name="eventArgs">Arguments describing this event.</param>
private void HandleProgressAdded(object sender, DataAddedEventArgs eventArgs)
{
int index = eventArgs.Index;
lock (_syncObject)
{
int indexIntoDataSent = (!_extraPowerShellAlreadyScheduled) ? 0 : 1;
if ((indexIntoDataSent == 0) && (!_datasent[indexIntoDataSent]))
{
ProgressRecord data = LocalPowerShell.Streams.Progress[index];
// once the debug message is sent, it is removed so that
// the same is not sent again by SendRemainingData() method
LocalPowerShell.Streams.Progress.RemoveAt(index);
// send the output data to the client
DataStructureHandler.SendProgressRecordToClient(data);
}
}
}
/// <summary>
/// Handles DataAdded event from Warning of PowerShell.
/// </summary>
/// <param name="sender">Sender of this information, unused.</param>
/// <param name="eventArgs">Arguments describing this event.</param>
private void HandleWarningAdded(object sender, DataAddedEventArgs eventArgs)
{
int index = eventArgs.Index;
lock (_syncObject)
{
int indexIntoDataSent = (!_extraPowerShellAlreadyScheduled) ? 0 : 1;
if ((indexIntoDataSent == 0) && (!_datasent[indexIntoDataSent]))
{
WarningRecord data = LocalPowerShell.Streams.Warning[index];
// once the debug message is sent, it is removed so that
// the same is not sent again by SendRemainingData() method
LocalPowerShell.Streams.Warning.RemoveAt(index);
// send the output data to the client
DataStructureHandler.SendWarningRecordToClient(data);
}
}
}
/// <summary>
/// Handles DataAdded from Verbose of PowerShell.
/// </summary>
/// <param name="sender">Sender of this information, unused.</param>
/// <param name="eventArgs">Sender of this information.</param>
private void HandleVerboseAdded(object sender, DataAddedEventArgs eventArgs)
{
int index = eventArgs.Index;
lock (_syncObject)
{
int indexIntoDataSent = (!_extraPowerShellAlreadyScheduled) ? 0 : 1;
if ((indexIntoDataSent == 0) && (!_datasent[indexIntoDataSent]))
{
VerboseRecord data = LocalPowerShell.Streams.Verbose[index];
// once the debug message is sent, it is removed so that
// the same is not sent again by SendRemainingData() method
LocalPowerShell.Streams.Verbose.RemoveAt(index);
// send the output data to the client
DataStructureHandler.SendVerboseRecordToClient(data);
}
}
}
/// <summary>
/// Handles DataAdded from Debug of PowerShell.
/// </summary>
/// <param name="sender">Sender of this information, unused.</param>
/// <param name="eventArgs">Sender of this information.</param>
private void HandleDebugAdded(object sender, DataAddedEventArgs eventArgs)
{
int index = eventArgs.Index;
lock (_syncObject)
{
int indexIntoDataSent = (!_extraPowerShellAlreadyScheduled) ? 0 : 1;
if ((indexIntoDataSent == 0) && (!_datasent[indexIntoDataSent]))
{
DebugRecord data = LocalPowerShell.Streams.Debug[index];
// once the debug message is sent, it is removed so that
// the same is not sent again by SendRemainingData() method
LocalPowerShell.Streams.Debug.RemoveAt(index);
// send the output data to the client
DataStructureHandler.SendDebugRecordToClient(data);
}
}
}
/// <summary>
/// Handles DataAdded from Information of PowerShell.
/// </summary>
/// <param name="sender">Sender of this information, unused.</param>
/// <param name="eventArgs">Sender of this information.</param>
private void HandleInformationAdded(object sender, DataAddedEventArgs eventArgs)
{
int index = eventArgs.Index;
lock (_syncObject)
{
int indexIntoDataSent = (!_extraPowerShellAlreadyScheduled) ? 0 : 1;
if ((indexIntoDataSent == 0) && (!_datasent[indexIntoDataSent]))
{
InformationRecord data = LocalPowerShell.Streams.Information[index];
// once the Information message is sent, it is removed so that
// the same is not sent again by SendRemainingData() method
LocalPowerShell.Streams.Information.RemoveAt(index);
// send the output data to the client
DataStructureHandler.SendInformationRecordToClient(data);
}
}
}
/// <summary>
/// Send the remaining output and error information to
/// client.
/// </summary>
/// <remarks>This method should be called before
/// sending the state information. The client will
/// remove the association between a powershell and
/// runspace pool if it receives any of the terminal
/// states. Hence all the remaining data should be
/// sent before this happens. Else the data will be
/// discarded</remarks>
private void SendRemainingData()
{
int indexIntoDataSent = (!_extraPowerShellAlreadyScheduled) ? 0 : 1;
lock (_syncObject)
{
_datasent[indexIntoDataSent] = true;
}
try
{
// BUGBUG: change this code to use enumerator
// blocked on bug #108824, to be fixed by Kriscv
for (int i = 0; i < _localPowerShellOutput.Count; i++)
{
PSObject data = _localPowerShellOutput[i];
DataStructureHandler.SendOutputDataToClient(data);
}
_localPowerShellOutput.Clear();
// foreach (ErrorRecord errorRecord in localPowerShell.Error)
for (int i = 0; i < LocalPowerShell.Streams.Error.Count; i++)
{
ErrorRecord errorRecord = LocalPowerShell.Streams.Error[i];
DataStructureHandler.SendErrorRecordToClient(errorRecord);
}
LocalPowerShell.Streams.Error.Clear();
}
finally
{
lock (_syncObject)
{
// reset to original state so other pipelines can stream.
_datasent[indexIntoDataSent] = true;
}
}
}
/// <summary>
/// Stop the local powershell.
/// </summary>
/// <param name="sender">Sender of this event, unused.</param>
/// <param name="eventArgs">Unused.</param>
private void HandleStopReceived(object sender, EventArgs eventArgs)
{
do // false loop
{
if (LocalPowerShell.InvocationStateInfo.State == PSInvocationState.Stopped ||
LocalPowerShell.InvocationStateInfo.State == PSInvocationState.Completed ||
LocalPowerShell.InvocationStateInfo.State == PSInvocationState.Failed ||
LocalPowerShell.InvocationStateInfo.State == PSInvocationState.Stopping)
{
break;
}
else
{
// Ensure that the local PowerShell command is not stopped in debug mode.
bool handledByDebugger = false;
if (!LocalPowerShell.IsNested &&
_psDriverInvoker != null)
{
handledByDebugger = _psDriverInvoker.HandleStopSignal();
}
if (!handledByDebugger)
{
LocalPowerShell.Stop();
}
}
} while (false);
if (_extraPowerShell != null)
{
do // false loop
{
if (_extraPowerShell.InvocationStateInfo.State == PSInvocationState.Stopped ||
_extraPowerShell.InvocationStateInfo.State == PSInvocationState.Completed ||
_extraPowerShell.InvocationStateInfo.State == PSInvocationState.Failed ||
_extraPowerShell.InvocationStateInfo.State == PSInvocationState.Stopping)
{
break;
}
else
{
_extraPowerShell.Stop();
}
} while (false);
}
}
/// <summary>
/// Add input to the local powershell's input collection.
/// </summary>
/// <param name="sender">Sender of this event, unused.</param>
/// <param name="eventArgs">Arguments describing this event.</param>
private void HandleInputReceived(object sender, RemoteDataEventArgs<object> eventArgs)
{
// This can be called in pushed runspace scenarios for error reporting (pipeline stopped).
// Ignore for noInput.
if (!_noInput && (InputCollection != null))
{
InputCollection.Add(eventArgs.Data);
}
}
/// <summary>
/// Close the input collection of the local powershell.
/// </summary>
/// <param name="sender">Sender of this event, unused.</param>
/// <param name="eventArgs">Arguments describing this event.</param>
private void HandleInputEndReceived(object sender, EventArgs eventArgs)
{
// This can be called in pushed runspace scenarios for error reporting (pipeline stopped).
// Ignore for noInput.
if (!_noInput && (InputCollection != null))
{
InputCollection.Complete();
}
}
private void HandleSessionConnected(object sender, EventArgs eventArgs)
{
// Close input if its active. no need to synchronize as input stream would have already been processed
// when connect call came into PS plugin
// TODO: Post an ETW event
InputCollection?.Complete();
}
/// <summary>
/// Handle a host message response received.
/// </summary>
/// <param name="sender">Sender of this event, unused.</param>
/// <param name="eventArgs">Arguments describing this event.</param>
private void HandleHostResponseReceived(object sender, RemoteDataEventArgs<RemoteHostResponse> eventArgs)
{
_remoteHost.ServerMethodExecutor.HandleRemoteHostResponseFromClient(eventArgs.Data);
}
/// <summary>
/// Handles the PSDataCollection idle event.
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private void HandleIdleEvent(object sender, EventArgs args)
{
Runspace rs = DataStructureHandler.RunspaceUsedToInvokePowerShell;
if (rs != null)
{
PSLocalEventManager events = (object)rs.Events as PSLocalEventManager;
if (events != null)
{
foreach (PSEventSubscriber subscriber in events.Subscribers)
{
// Use the synchronous version
events.DrainPendingActions(subscriber);
}
}
}
}
#endregion Private Methods
}
}
|