MegaBites-AI/Windows-powershell
0372
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System.Collections.ObjectModel;5using System.Management.Automation.Provider;6 7using Dbg = System.Management.Automation;8 9#pragma warning disable 1634, 1691 // Stops compiler from warning about unknown warnings10#pragma warning disable 5650011 12namespace System.Management.Automation13{14 /// <summary>15 /// Holds the state of a Monad Shell session.16 /// </summary>17 internal sealed partial class SessionStateInternal18 {19 #region ItemCmdletProvider accessors20 21 #region GetItem22 23 /// <summary>24 /// Gets the specified object.25 /// </summary>26 /// <param name="paths">27 /// The path(s) to the object(s). They can be either a relative (most common)28 /// or absolute path.29 /// </param>30 /// <param name="force">31 /// Passed on to providers to force operations.32 /// </param>33 /// <param name="literalPath">34 /// If true, globbing is not done on paths.35 /// </param>36 /// <returns>37 /// The item at the specified path.38 /// </returns>39 /// <exception cref="ArgumentNullException">40 /// If <paramref name="path"/> is null.41 /// </exception>42 /// <exception cref="ProviderNotFoundException">43 /// If the <paramref name="path"/> refers to a provider that could not be found.44 /// </exception>45 /// <exception cref="DriveNotFoundException">46 /// If the <paramref name="path"/> refers to a drive that could not be found.47 /// </exception>48 /// <exception cref="NotSupportedException">49 /// If the provider that the <paramref name="path"/> refers to does50 /// not support this operation.51 /// </exception>52 /// <exception cref="ProviderInvocationException">53 /// If the provider threw an exception.54 /// </exception>55 internal Collection<PSObject> GetItem(string[] paths, bool force, bool literalPath)56 {57 if (paths == null)58 {59 throw PSTraceSource.NewArgumentNullException(nameof(paths));60 }61 62 CmdletProviderContext context = new CmdletProviderContext(this.ExecutionContext);63 context.Force = force;64 context.SuppressWildcardExpansion = literalPath;65 66 GetItem(paths, context);67 68 context.ThrowFirstErrorOrDoNothing();69 70 // Since there was not errors return the accumulated objects71 72 Collection<PSObject> results = context.GetAccumulatedObjects();73 74 return results;75 }76 77 /// <summary>78 /// Gets the specified object.79 /// </summary>80 /// <param name="paths">81 /// The path(s) to the object(s). They can be either a relative (most common)82 /// or absolute path.83 /// </param>84 /// <param name="context">85 /// The context which the core command is running.86 /// </param>87 /// <returns>88 /// Nothing is returned, but all objects should be written to the WriteObject89 /// method of the <paramref name="context"/> parameter.90 /// </returns>91 /// <exception cref="ArgumentNullException">92 /// If <paramref name="path"/> is null.93 /// </exception>94 /// <exception cref="ProviderNotFoundException">95 /// If the <paramref name="path"/> refers to a provider that could not be found.96 /// </exception>97 /// <exception cref="DriveNotFoundException">98 /// If the <paramref name="path"/> refers to a drive that could not be found.99 /// </exception>100 /// <exception cref="NotSupportedException">101 /// If the provider that the <paramref name="path"/> refers to does102 /// not support this operation.103 /// </exception>104 /// <exception cref="ProviderInvocationException">105 /// If the provider threw an exception.106 /// </exception>107 /// <exception cref="ItemNotFoundException">108 /// If <paramref name="path"/> does not contain glob characters and109 /// could not be found.110 /// </exception>111 internal void GetItem(112 string[] paths,113 CmdletProviderContext context)114 {115 if (paths == null)116 {117 throw PSTraceSource.NewArgumentNullException(nameof(paths));118 }119 120 ProviderInfo provider = null;121 CmdletProvider providerInstance = null;122 123 foreach (string path in paths)124 {125 if (path == null)126 {127 throw PSTraceSource.NewArgumentNullException(nameof(paths));128 }129 130 Collection<string> providerPaths =131 Globber.GetGlobbedProviderPathsFromMonadPath(132 path,133 false,134 context,135 out provider,136 out providerInstance);137 138 foreach (string providerPath in providerPaths)139 {140 GetItemPrivate(providerInstance, providerPath, context);141 }142 }143 }144 145 /// <summary>146 /// Gets the item at the specified path.147 /// </summary>148 /// <param name="providerInstance">149 /// The provider instance to use.150 /// </param>151 /// <param name="path">152 /// The path to the item if it was specified on the command line.153 /// </param>154 /// <param name="context">155 /// The context which the core command is running.156 /// </param>157 /// <exception cref="NotSupportedException">158 /// If the <paramref name="providerInstance"/> does not support this operation.159 /// </exception>160 /// <exception cref="PipelineStoppedException">161 /// If the pipeline is being stopped while executing the command.162 /// </exception>163 /// <exception cref="ProviderInvocationException">164 /// If the provider threw an exception.165 /// </exception>166 private void GetItemPrivate(167 CmdletProvider providerInstance,168 string path,169 CmdletProviderContext context)170 {171 // All parameters should have been validated by caller172 Dbg.Diagnostics.Assert(173 providerInstance != null,174 "Caller should validate providerInstance before calling this method");175 176 Dbg.Diagnostics.Assert(177 path != null,178 "Caller should validate path before calling this method");179 180 Dbg.Diagnostics.Assert(181 context != null,182 "Caller should validate context before calling this method");183 184 ItemCmdletProvider itemCmdletProvider =185 GetItemProviderInstance(providerInstance);186 187 try188 {189 itemCmdletProvider.GetItem(path, context);190 }191 catch (LoopFlowException)192 {193 throw;194 }195 catch (PipelineStoppedException)196 {197 throw;198 }199 catch (ActionPreferenceStopException)200 {201 throw;202 }203 catch (Exception e) // Catch-all OK, 3rd party callout.204 {205 throw NewProviderInvocationException(206 "GetItemProviderException",207 SessionStateStrings.GetItemProviderException,208 itemCmdletProvider.ProviderInfo,209 path,210 e);211 }212 }213 214 /// <summary>215 /// Gets the dynamic parameters for the get-item cmdlet.216 /// </summary>217 /// <param name="path">218 /// The path to the item if it was specified on the command line.219 /// </param>220 /// <param name="context">221 /// The context which the core command is running.222 /// </param>223 /// <returns>224 /// An object that has properties and fields decorated with225 /// parsing attributes similar to a cmdlet class.226 /// </returns>227 /// <exception cref="ProviderNotFoundException">228 /// If the <paramref name="path"/> refers to a provider that could not be found.229 /// </exception>230 /// <exception cref="DriveNotFoundException">231 /// If the <paramref name="path"/> refers to a drive that could not be found.232 /// </exception>233 /// <exception cref="NotSupportedException">234 /// If the provider that the <paramref name="path"/> refers to does235 /// not support this operation.236 /// </exception>237 /// <exception cref="ProviderInvocationException">238 /// If the provider threw an exception.239 /// </exception>240 /// <exception cref="ItemNotFoundException">241 /// If <paramref name="path"/> does not contain glob characters and242 /// could not be found.243 /// </exception>244 internal object GetItemDynamicParameters(string path, CmdletProviderContext context)245 {246 if (path == null)247 {248 return null;249 }250 251 ProviderInfo provider = null;252 CmdletProvider providerInstance = null;253 254 CmdletProviderContext newContext =255 new CmdletProviderContext(context);256 newContext.SetFilters(257 new Collection<string>(),258 new Collection<string>(),259 null);260 261 Collection<string> providerPaths =262 Globber.GetGlobbedProviderPathsFromMonadPath(263 path,264 true,265 newContext,266 out provider,267 out providerInstance);268 269 if (providerPaths.Count > 0)270 {271 // Get the dynamic parameters for the first resolved path272 273 return GetItemDynamicParameters(providerInstance, providerPaths[0], newContext);274 }275 276 return null;277 }278 279 /// <summary>280 /// Gets the dynamic parameters for the get-item cmdlet.281 /// </summary>282 /// <param name="path">283 /// The path to the item if it was specified on the command line.284 /// </param>285 /// <param name="providerInstance">286 /// The instance of the provider to use.287 /// </param>288 /// <param name="context">289 /// The context which the core command is running.290 /// </param>291 /// <returns>292 /// An object that has properties and fields decorated with293 /// parsing attributes similar to a cmdlet class.294 /// </returns>295 /// <exception cref="NotSupportedException">296 /// If the <paramref name="providerInstance"/> does not support this operation.297 /// </exception>298 /// <exception cref="PipelineStoppedException">299 /// If the pipeline is being stopped while executing the command.300 /// </exception>301 /// <exception cref="ProviderInvocationException">302 /// If the provider threw an exception.303 /// </exception>304 private object GetItemDynamicParameters(305 CmdletProvider providerInstance,306 string path,307 CmdletProviderContext context)308 {309 // All parameters should have been validated by caller310 Dbg.Diagnostics.Assert(311 providerInstance != null,312 "Caller should validate providerInstance before calling this method");313 314 Dbg.Diagnostics.Assert(315 path != null,316 "Caller should validate path before calling this method");317 318 Dbg.Diagnostics.Assert(319 context != null,320 "Caller should validate context before calling this method");321 322 ItemCmdletProvider itemCmdletProvider =323 GetItemProviderInstance(providerInstance);324 325 object result = null;326 try327 {328 result = itemCmdletProvider.GetItemDynamicParameters(path, context);329 }330 catch (LoopFlowException)331 {332 throw;333 }334 catch (PipelineStoppedException)335 {336 throw;337 }338 catch (ActionPreferenceStopException)339 {340 throw;341 }342 catch (Exception e) // Catch-all OK, 3rd party callout.343 {344 throw NewProviderInvocationException(345 "GetItemDynamicParametersProviderException",346 SessionStateStrings.GetItemDynamicParametersProviderException,347 itemCmdletProvider.ProviderInfo,348 path,349 e);350 }351 352 return result;353 }354 355 #endregion GetItem356 357 #region SetItem358 359 /// <summary>360 /// Gets the specified object.361 /// </summary>362 /// <param name="paths">363 /// The path(s) to the object. It can be either a relative (most common)364 /// or absolute path.365 /// </param>366 /// <param name="value">367 /// The new value for the item at the specified path.368 /// </param>369 /// <param name="force">370 /// Passed on to providers to force operations.371 /// </param>372 /// <param name="literalPath">373 /// If true, globbing is not done on paths.374 /// </param>375 /// <returns>376 /// The item that was modified at the specified path.377 /// </returns>378 /// <exception cref="ArgumentNullException">379 /// If <paramref name="path"/> is null.380 /// </exception>381 /// <exception cref="ProviderNotFoundException">382 /// If the <paramref name="path"/> refers to a provider that could not be found.383 /// </exception>384 /// <exception cref="DriveNotFoundException">385 /// If the <paramref name="path"/> refers to a drive that could not be found.386 /// </exception>387 /// <exception cref="NotSupportedException">388 /// If the provider that the <paramref name="path"/> refers to does389 /// not support this operation.390 /// </exception>391 /// <exception cref="ProviderInvocationException">392 /// If the provider threw an exception.393 /// </exception>394 internal Collection<PSObject> SetItem(string[] paths, object value, bool force, bool literalPath)395 {396 if (paths == null)397 {398 throw PSTraceSource.NewArgumentNullException(nameof(paths));399 }400 401 CmdletProviderContext context = new CmdletProviderContext(this.ExecutionContext);402 context.Force = force;403 context.SuppressWildcardExpansion = literalPath;404 405 SetItem(paths, value, context);406 407 context.ThrowFirstErrorOrDoNothing();408 409 // Since there was no errors return the accumulated objects410 411 return context.GetAccumulatedObjects();412 }413 414 /// <summary>415 /// Sets the specified object to the specified value.416 /// </summary>417 /// <param name="paths">418 /// The path(s) to the object. It can be either a relative (most common)419 /// or absolute path.420 /// </param>421 /// <param name="value">422 /// The new value of the item at the specified path.423 /// </param>424 /// <param name="context">425 /// The context which the core command is running.426 /// </param>427 /// <exception cref="ArgumentNullException">428 /// If <paramref name="path"/> is null.429 /// </exception>430 /// <exception cref="ProviderNotFoundException">431 /// If the <paramref name="path"/> refers to a provider that could not be found.432 /// </exception>433 /// <exception cref="DriveNotFoundException">434 /// If the <paramref name="path"/> refers to a drive that could not be found.435 /// </exception>436 /// <exception cref="NotSupportedException">437 /// If the provider that the <paramref name="path"/> refers to does438 /// not support this operation.439 /// </exception>440 /// <exception cref="ProviderInvocationException">441 /// If the provider threw an exception.442 /// </exception>443 /// <exception cref="ItemNotFoundException">444 /// If <paramref name="path"/> does not contain glob characters and445 /// could not be found.446 /// </exception>447 internal void SetItem(448 string[] paths,449 object value,450 CmdletProviderContext context)451 {452 if (paths == null)453 {454 throw PSTraceSource.NewArgumentNullException(nameof(paths));455 }456 457 foreach (string path in paths)458 {459 if (path == null)460 {461 throw PSTraceSource.NewArgumentNullException(nameof(paths));462 }463 464 ProviderInfo provider = null;465 CmdletProvider providerInstance = null;466 467 Collection<string> providerPaths =468 Globber.GetGlobbedProviderPathsFromMonadPath(469 path,470 true,471 context,472 out provider,473 out providerInstance);474 475 if (providerPaths != null)476 {477 foreach (string providerPath in providerPaths)478 {479 SetItem(providerInstance, providerPath, value, context);480 }481 }482 }483 }484 485 /// <summary>486 /// Sets item at the specified path.487 /// </summary>488 /// <param name="providerInstance">489 /// The provider instance to use.490 /// </param>491 /// <param name="path">492 /// The path to the item if it was specified on the command line.493 /// </param>494 /// <param name="value">495 /// The value of the item.496 /// </param>497 /// <param name="context">498 /// The context which the core command is running.499 /// </param>500 /// <exception cref="NotSupportedException">501 /// If the <paramref name="providerInstance"/> does not support this operation.502 /// </exception>503 /// <exception cref="PipelineStoppedException">504 /// If the pipeline is being stopped while executing the command.505 /// </exception>506 /// <exception cref="ProviderInvocationException">507 /// If the provider threw an exception.508 /// </exception>509 private void SetItem(510 CmdletProvider providerInstance,511 string path,512 object value,513 CmdletProviderContext context)514 {515 // All parameters should have been validated by caller516 Dbg.Diagnostics.Assert(517 providerInstance != null,518 "Caller should validate providerInstance before calling this method");519 520 Dbg.Diagnostics.Assert(521 path != null,522 "Caller should validate path before calling this method");523 524 Dbg.Diagnostics.Assert(525 context != null,526 "Caller should validate context before calling this method");527 528 ItemCmdletProvider itemCmdletProvider =529 GetItemProviderInstance(providerInstance);530 531 try532 {533 itemCmdletProvider.SetItem(path, value, context);534 }535 catch (LoopFlowException)536 {537 throw;538 }539 catch (PipelineStoppedException)540 {541 throw;542 }543 catch (ActionPreferenceStopException)544 {545 throw;546 }547 catch (Exception e) // Catch-all OK, 3rd party callout.548 {549 throw NewProviderInvocationException(550 "SetItemProviderException",551 SessionStateStrings.SetItemProviderException,552 itemCmdletProvider.ProviderInfo,553 path,554 e);555 }556 }557 558 /// <summary>559 /// Gets the dynamic parameters for the set-item cmdlet.560 /// </summary>561 /// <param name="path">562 /// The path to the item if it was specified on the command line.563 /// </param>564 /// <param name="value">565 /// The new value of the item at the specified path.566 /// </param>567 /// <param name="context">568 /// The context which the core command is running.569 /// </param>570 /// <returns>571 /// An object that has properties and fields decorated with572 /// parsing attributes similar to a cmdlet class.573 /// </returns>574 /// <exception cref="ProviderNotFoundException">575 /// If the <paramref name="path"/> refers to a provider that could not be found.576 /// </exception>577 /// <exception cref="DriveNotFoundException">578 /// If the <paramref name="path"/> refers to a drive that could not be found.579 /// </exception>580 /// <exception cref="NotSupportedException">581 /// If the provider that the <paramref name="path"/> refers to does582 /// not support this operation.583 /// </exception>584 /// <exception cref="ProviderInvocationException">585 /// If the provider threw an exception.586 /// </exception>587 /// <exception cref="ItemNotFoundException">588 /// If <paramref name="path"/> does not contain glob characters and589 /// could not be found.590 /// </exception>591 internal object SetItemDynamicParameters(string path, object value, CmdletProviderContext context)592 {593 if (path == null)594 {595 return null;596 }597 598 ProviderInfo provider = null;599 CmdletProvider providerInstance = null;600 601 CmdletProviderContext newContext =602 new CmdletProviderContext(context);603 newContext.SetFilters(604 new Collection<string>(),605 new Collection<string>(),606 null);607 608 Collection<string> providerPaths =609 Globber.GetGlobbedProviderPathsFromMonadPath(610 path,611 true,612 newContext,613 out provider,614 out providerInstance);615 616 if (providerPaths.Count > 0)617 {618 // Get the dynamic parameters for the first resolved path619 620 return SetItemDynamicParameters(providerInstance, providerPaths[0], value, newContext);621 }622 623 return null;624 }625 626 /// <summary>627 /// Gets the dynamic parameters for the set-item cmdlet.628 /// </summary>629 /// <param name="path">630 /// The path to the item if it was specified on the command line.631 /// </param>632 /// <param name="providerInstance">633 /// The instance of the provider to use.634 /// </param>635 /// <param name="value">636 /// The value to be set.637 /// </param>638 /// <param name="context">639 /// The context which the core command is running.640 /// </param>641 /// <returns>642 /// An object that has properties and fields decorated with643 /// parsing attributes similar to a cmdlet class.644 /// </returns>645 /// <exception cref="NotSupportedException">646 /// If the <paramref name="providerInstance"/> does not support this operation.647 /// </exception>648 /// <exception cref="PipelineStoppedException">649 /// If the pipeline is being stopped while executing the command.650 /// </exception>651 /// <exception cref="ProviderInvocationException">652 /// If the provider threw an exception.653 /// </exception>654 private object SetItemDynamicParameters(655 CmdletProvider providerInstance,656 string path,657 object value,658 CmdletProviderContext context)659 {660 // All parameters should have been validated by caller661 Dbg.Diagnostics.Assert(662 providerInstance != null,663 "Caller should validate providerInstance before calling this method");664 665 Dbg.Diagnostics.Assert(666 path != null,667 "Caller should validate path before calling this method");668 669 Dbg.Diagnostics.Assert(670 context != null,671 "Caller should validate context before calling this method");672 673 ItemCmdletProvider itemCmdletProvider =674 GetItemProviderInstance(providerInstance);675 676 object result = null;677 try678 {679 result = itemCmdletProvider.SetItemDynamicParameters(path, value, context);680 }681 catch (LoopFlowException)682 {683 throw;684 }685 catch (PipelineStoppedException)686 {687 throw;688 }689 catch (ActionPreferenceStopException)690 {691 throw;692 }693 catch (Exception e) // Catch-all OK, 3rd party callout.694 {695 throw NewProviderInvocationException(696 "SetItemDynamicParametersProviderException",697 SessionStateStrings.SetItemDynamicParametersProviderException,698 itemCmdletProvider.ProviderInfo,699 path,700 e);701 }702 703 return result;704 }705 706 #endregion SetItem707 708 #region ClearItem709 710 /// <summary>711 /// Clears the specified object. Depending on the provider that the path712 /// maps to, this could mean the properties and/or content and/or value is713 /// cleared.714 /// </summary>715 /// <param name="paths">716 /// The path(s) to the object. It can be either a relative (most common)717 /// or absolute path.718 /// </param>719 /// <param name="force">720 /// Passed on to providers to force operations.721 /// </param>722 /// <param name="literalPath">723 /// If true, globbing is not done on paths.724 /// </param>725 /// <returns>726 /// The items that were cleared.727 /// </returns>728 /// <remarks>729 /// If an error occurs that error will be thrown.730 /// </remarks>731 /// <exception cref="ArgumentNullException">732 /// If <paramref name="path"/> is null.733 /// </exception>734 /// <exception cref="ProviderNotFoundException">735 /// If the <paramref name="path"/> refers to a provider that could not be found.736 /// </exception>737 /// <exception cref="DriveNotFoundException">738 /// If the <paramref name="path"/> refers to a drive that could not be found.739 /// </exception>740 /// <exception cref="NotSupportedException">741 /// If the provider that the <paramref name="path"/> refers to does742 /// not support this operation.743 /// </exception>744 /// <exception cref="ProviderInvocationException">745 /// If the provider threw an exception.746 /// </exception>747 internal Collection<PSObject> ClearItem(string[] paths, bool force, bool literalPath)748 {749 if (paths == null)750 {751 throw PSTraceSource.NewArgumentNullException(nameof(paths));752 }753 754 CmdletProviderContext context = new CmdletProviderContext(this.ExecutionContext);755 context.Force = force;756 context.SuppressWildcardExpansion = literalPath;757 758 ClearItem(paths, context);759 760 context.ThrowFirstErrorOrDoNothing();761 762 return context.GetAccumulatedObjects();763 }764 765 /// <summary>766 /// Clears the specified item. Depending on the provider that the path767 /// maps to, this could mean the properties and/or content and/or value is768 /// cleared.769 /// </summary>770 /// <param name="paths">771 /// The path(s) to the object. It can be either a relative (most common)772 /// or absolute path.773 /// </param>774 /// <param name="context">775 /// The context which the core command is running.776 /// </param>777 /// <exception cref="ArgumentNullException">778 /// If <paramref name="path"/> is null.779 /// </exception>780 /// <exception cref="ProviderNotFoundException">781 /// If the <paramref name="path"/> refers to a provider that could not be found.782 /// </exception>783 /// <exception cref="DriveNotFoundException">784 /// If the <paramref name="path"/> refers to a drive that could not be found.785 /// </exception>786 /// <exception cref="NotSupportedException">787 /// If the provider that the <paramref name="path"/> refers to does788 /// not support this operation.789 /// </exception>790 /// <exception cref="ProviderInvocationException">791 /// If the provider threw an exception.792 /// </exception>793 /// <exception cref="ItemNotFoundException">794 /// If <paramref name="path"/> does not contain glob characters and795 /// could not be found.796 /// </exception>797 internal void ClearItem(798 string[] paths,799 CmdletProviderContext context)800 {801 if (paths == null)802 {803 throw PSTraceSource.NewArgumentNullException(nameof(paths));804 }805 806 ProviderInfo provider = null;807 CmdletProvider providerInstance = null;808 809 foreach (string path in paths)810 {811 if (path == null)812 {813 throw PSTraceSource.NewArgumentNullException(nameof(paths));814 }815 816 Collection<string> providerPaths =817 Globber.GetGlobbedProviderPathsFromMonadPath(818 path,819 false,820 context,821 out provider,822 out providerInstance);823 824 if (providerPaths != null)825 {826 foreach (string providerPath in providerPaths)827 {828 ClearItemPrivate(providerInstance, providerPath, context);829 }830 }831 }832 }833 834 /// <summary>835 /// Clears the item at the specified path.836 /// </summary>837 /// <param name="providerInstance">838 /// The provider instance to use.839 /// </param>840 /// <param name="path">841 /// The path to the item if it was specified on the command line.842 /// </param>843 /// <param name="context">844 /// The context which the core command is running.845 /// </param>846 /// <exception cref="NotSupportedException">847 /// If the <paramref name="providerInstance"/> does not support this operation.848 /// </exception>849 /// <exception cref="PipelineStoppedException">850 /// If the pipeline is being stopped while executing the command.851 /// </exception>852 /// <exception cref="ProviderInvocationException">853 /// If the provider threw an exception.854 /// </exception>855 private void ClearItemPrivate(856 CmdletProvider providerInstance,857 string path,858 CmdletProviderContext context)859 {860 // All parameters should have been validated by caller861 Dbg.Diagnostics.Assert(862 providerInstance != null,863 "Caller should validate providerInstance before calling this method");864 865 Dbg.Diagnostics.Assert(866 path != null,867 "Caller should validate path before calling this method");868 869 Dbg.Diagnostics.Assert(870 context != null,871 "Caller should validate context before calling this method");872 873 ItemCmdletProvider itemCmdletProvider =874 GetItemProviderInstance(providerInstance);875 876 try877 {878 itemCmdletProvider.ClearItem(path, context);879 }880 catch (LoopFlowException)881 {882 throw;883 }884 catch (PipelineStoppedException)885 {886 throw;887 }888 catch (ActionPreferenceStopException)889 {890 throw;891 }892 catch (Exception e) // Catch-all OK, 3rd party callout.893 {894 throw NewProviderInvocationException(895 "ClearItemProviderException",896 SessionStateStrings.ClearItemProviderException,897 itemCmdletProvider.ProviderInfo,898 path,899 e);900 }901 }902 903 /// <summary>904 /// Gets the dynamic parameters for the clear-item cmdlet.905 /// </summary>906 /// <param name="path">907 /// The path to the item if it was specified on the command line.908 /// </param>909 /// <param name="context">910 /// The context which the core command is running.911 /// </param>912 /// <returns>913 /// An object that has properties and fields decorated with914 /// parsing attributes similar to a cmdlet class.915 /// </returns>916 /// <exception cref="ProviderNotFoundException">917 /// If the <paramref name="path"/> refers to a provider that could not be found.918 /// </exception>919 /// <exception cref="DriveNotFoundException">920 /// If the <paramref name="path"/> refers to a drive that could not be found.921 /// </exception>922 /// <exception cref="NotSupportedException">923 /// If the provider that the <paramref name="path"/> refers to does924 /// not support this operation.925 /// </exception>926 /// <exception cref="ProviderInvocationException">927 /// If the provider threw an exception.928 /// </exception>929 /// <exception cref="ItemNotFoundException">930 /// If <paramref name="path"/> does not contain glob characters and931 /// could not be found.932 /// </exception>933 internal object ClearItemDynamicParameters(string path, CmdletProviderContext context)934 {935 if (path == null)936 {937 return null;938 }939 940 ProviderInfo provider = null;941 CmdletProvider providerInstance = null;942 943 CmdletProviderContext newContext =944 new CmdletProviderContext(context);945 newContext.SetFilters(946 new Collection<string>(),947 new Collection<string>(),948 null);949 950 Collection<string> providerPaths =951 Globber.GetGlobbedProviderPathsFromMonadPath(952 path,953 true,954 newContext,955 out provider,956 out providerInstance);957 958 if (providerPaths.Count > 0)959 {960 // Get the dynamic parameters for the first resolved path961 962 return ClearItemDynamicParameters(providerInstance, providerPaths[0], newContext);963 }964 965 return null;966 }967 968 /// <summary>969 /// Gets the dynamic parameters for the clear-item cmdlet.970 /// </summary>971 /// <param name="path">972 /// The path to the item if it was specified on the command line.973 /// </param>974 /// <param name="providerInstance">975 /// The instance of the provider to use.976 /// </param>977 /// <param name="context">978 /// The context which the core command is running.979 /// </param>980 /// <returns>981 /// An object that has properties and fields decorated with982 /// parsing attributes similar to a cmdlet class.983 /// </returns>984 /// <exception cref="NotSupportedException">985 /// If the <paramref name="providerInstance"/> does not support this operation.986 /// </exception>987 /// <exception cref="PipelineStoppedException">988 /// If the pipeline is being stopped while executing the command.989 /// </exception>990 /// <exception cref="ProviderInvocationException">991 /// If the provider threw an exception.992 /// </exception>993 private object ClearItemDynamicParameters(994 CmdletProvider providerInstance,995 string path,996 CmdletProviderContext context)997 {998 // All parameters should have been validated by caller999 Dbg.Diagnostics.Assert(1000 providerInstance != null,1001 "Caller should validate providerInstance before calling this method");1002 1003 Dbg.Diagnostics.Assert(1004 path != null,1005 "Caller should validate path before calling this method");1006 1007 Dbg.Diagnostics.Assert(1008 context != null,1009 "Caller should validate context before calling this method");1010 1011 ItemCmdletProvider itemCmdletProvider =1012 GetItemProviderInstance(providerInstance);1013 1014 object result = null;1015 try1016 {1017 result = itemCmdletProvider.ClearItemDynamicParameters(path, context);1018 }1019 catch (LoopFlowException)1020 {1021 throw;1022 }1023 catch (PipelineStoppedException)1024 {1025 throw;1026 }1027 catch (ActionPreferenceStopException)1028 {1029 throw;1030 }1031 catch (Exception e) // Catch-all OK, 3rd party callout.1032 {1033 throw NewProviderInvocationException(1034 "ClearItemProviderException",1035 SessionStateStrings.ClearItemProviderException,1036 itemCmdletProvider.ProviderInfo,1037 path,1038 e);1039 }1040 1041 return result;1042 }1043 1044 #endregion ClearItem1045 1046 #region InvokeDefaultAction1047 1048 /// <summary>1049 /// Performs the default action on the specified item. The default action is1050 /// determined by the provider.1051 /// </summary>1052 /// <param name="paths">1053 /// The path(s) to the object(s). They can be either a relative (most common)1054 /// or absolute path(s).1055 /// </param>1056 /// <param name="literalPath">1057 /// If true, globbing is not done on paths.1058 /// </param>1059 /// <remarks>1060 /// If an error occurs that error will be thrown.1061 /// </remarks>1062 /// <exception cref="ArgumentNullException">1063 /// If <paramref name="path"/> is null.1064 /// </exception>1065 /// <exception cref="ProviderNotFoundException">1066 /// If the <paramref name="path"/> refers to a provider that could not be found.1067 /// </exception>1068 /// <exception cref="DriveNotFoundException">1069 /// If the <paramref name="path"/> refers to a drive that could not be found.1070 /// </exception>1071 /// <exception cref="NotSupportedException">1072 /// If the provider that the <paramref name="path"/> refers to does1073 /// not support this operation.1074 /// </exception>1075 /// <exception cref="ProviderInvocationException">1076 /// If the provider threw an exception.1077 /// </exception>1078 internal void InvokeDefaultAction(string[] paths, bool literalPath)1079 {1080 if (paths == null)1081 {1082 throw PSTraceSource.NewArgumentNullException(nameof(paths));1083 }1084 1085 CmdletProviderContext context = new CmdletProviderContext(this.ExecutionContext);1086 context.SuppressWildcardExpansion = literalPath;1087 1088 InvokeDefaultAction(paths, context);1089 1090 context.ThrowFirstErrorOrDoNothing();1091 }1092 1093 /// <summary>1094 /// Performs the default action on the specified item. The default action1095 /// is determined by the provider.1096 /// </summary>1097 /// <param name="paths">1098 /// The path(s) to the object(s). They can be either a relative (most common)1099 /// or absolute paths.1100 /// </param>1101 /// <param name="context">1102 /// The context which the core command is running.1103 /// </param>1104 /// <exception cref="ArgumentNullException">1105 /// If <paramref name="path"/> is null.1106 /// </exception>1107 /// <exception cref="ProviderNotFoundException">1108 /// If the <paramref name="path"/> refers to a provider that could not be found.1109 /// </exception>1110 /// <exception cref="DriveNotFoundException">1111 /// If the <paramref name="path"/> refers to a drive that could not be found.1112 /// </exception>1113 /// <exception cref="NotSupportedException">1114 /// If the provider that the <paramref name="path"/> refers to does1115 /// not support this operation.1116 /// </exception>1117 /// <exception cref="ProviderInvocationException">1118 /// If the provider threw an exception.1119 /// </exception>1120 /// <exception cref="ItemNotFoundException">1121 /// If <paramref name="path"/> does not contain glob characters and1122 /// could not be found.1123 /// </exception>1124 internal void InvokeDefaultAction(1125 string[] paths,1126 CmdletProviderContext context)1127 {1128 if (paths == null)1129 {1130 throw PSTraceSource.NewArgumentNullException(nameof(paths));1131 }1132 1133 ProviderInfo provider = null;1134 CmdletProvider providerInstance = null;1135 1136 foreach (string path in paths)1137 {1138 if (path == null)1139 {1140 throw PSTraceSource.NewArgumentNullException(nameof(paths));1141 }1142 1143 Collection<string> providerPaths =1144 Globber.GetGlobbedProviderPathsFromMonadPath(1145 path,1146 false,1147 context,1148 out provider,1149 out providerInstance);1150 1151 if (providerPaths != null)1152 {1153 foreach (string providerPath in providerPaths)1154 {1155 InvokeDefaultActionPrivate(providerInstance, providerPath, context);1156 }1157 }1158 }1159 }1160 1161 /// <summary>1162 /// Invokes the default action on the item at the specified path.1163 /// </summary>1164 /// <param name="providerInstance">1165 /// The provider instance to use.1166 /// </param>1167 /// <param name="path">1168 /// The path to the item if it was specified on the command line.1169 /// </param>1170 /// <param name="context">1171 /// The context which the core command is running.1172 /// </param>1173 /// <exception cref="NotSupportedException">1174 /// If the <paramref name="providerInstance"/> does not support this operation.1175 /// </exception>1176 /// <exception cref="PipelineStoppedException">1177 /// If the pipeline is being stopped while executing the command.1178 /// </exception>1179 /// <exception cref="ProviderInvocationException">1180 /// If the provider threw an exception.1181 /// </exception>1182 private void InvokeDefaultActionPrivate(1183 CmdletProvider providerInstance,1184 string path,1185 CmdletProviderContext context)1186 {1187 // All parameters should have been validated by caller1188 Dbg.Diagnostics.Assert(1189 providerInstance != null,1190 "Caller should validate providerInstance before calling this method");1191 1192 Dbg.Diagnostics.Assert(1193 path != null,1194 "Caller should validate path before calling this method");1195 1196 Dbg.Diagnostics.Assert(1197 context != null,1198 "Caller should validate context before calling this method");1199 1200 ItemCmdletProvider itemCmdletProvider =