// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using System.Collections.ObjectModel; using System.Management.Automation.Provider; using Dbg = System.Management.Automation; #pragma warning disable 1634, 1691 // Stops compiler from warning about unknown warnings #pragma warning disable 56500 namespace System.Management.Automation { /// /// Holds the state of a Monad Shell session. /// internal sealed partial class SessionStateInternal { #region IDynamicPropertyCmdletProvider accessors #region NewProperty /// /// Creates a new property on the specified item. /// /// /// The path(s) to the item(s) on which the new property should be created. /// /// /// The name of the property that should be created. /// /// /// The type of the property that should be created. /// /// /// The new value of the property that should be created. /// /// /// Passed on to providers to force operations. /// /// /// If true, globbing is not done on paths. /// /// /// A property table containing the properties and their values. /// /// /// If is null. /// /// /// If the refers to a provider that could not be found. /// /// /// If the refers to a drive that could not be found. /// /// /// If the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// internal Collection NewProperty( string[] paths, string property, string type, object value, bool force, bool literalPath) { if (paths == null) { throw PSTraceSource.NewArgumentNullException(nameof(paths)); } if (property == null) { throw PSTraceSource.NewArgumentNullException(nameof(property)); } CmdletProviderContext context = new CmdletProviderContext(this.ExecutionContext); context.Force = force; context.SuppressWildcardExpansion = literalPath; NewProperty(paths, property, type, value, context); context.ThrowFirstErrorOrDoNothing(); Collection results = context.GetAccumulatedObjects(); return results; } /// /// Creates a new property on the specified item. /// /// /// The path(s) to the item(s) on which the new property should be created. /// /// /// The name of the property that should be created. /// /// /// The type of the property that should be created. /// /// /// The new value of the property that should be created. /// /// /// The context which the core command is running. /// /// /// Nothing. The property should be passed to the context as a PSObject. /// /// /// If is null. /// /// /// If the refers to a provider that could not be found. /// /// /// If the refers to a drive that could not be found. /// /// /// If the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// /// /// If does not contain glob characters and /// could not be found. /// internal void NewProperty( string[] paths, string property, string type, object value, CmdletProviderContext context) { if (paths == null) { throw PSTraceSource.NewArgumentNullException(nameof(paths)); } if (property == null) { throw PSTraceSource.NewArgumentNullException(nameof(property)); } ProviderInfo provider = null; CmdletProvider providerInstance = null; foreach (string path in paths) { if (path == null) { throw PSTraceSource.NewArgumentNullException(nameof(paths)); } Collection providerPaths = Globber.GetGlobbedProviderPathsFromMonadPath( path, false, context, out provider, out providerInstance); foreach (string providerPath in providerPaths) { NewProperty(providerInstance, providerPath, property, type, value, context); } } } /// /// Creates a new property on the item at the specified path. /// /// /// The provider instance to use. /// /// /// The path to the item if it was specified on the command line. /// /// /// The name of the property to create. /// /// /// The type of the property to create. /// /// /// The value of the property to create. /// /// /// The context which the core command is running. /// /// /// If the does not support this operation. /// /// /// If the pipeline is being stopped while executing the command. /// /// /// If the provider threw an exception. /// private void NewProperty( CmdletProvider providerInstance, string path, string property, string type, object value, CmdletProviderContext context) { // All parameters should have been validated by caller Dbg.Diagnostics.Assert( providerInstance != null, "Caller should validate providerInstance before calling this method"); Dbg.Diagnostics.Assert( path != null, "Caller should validate path before calling this method"); Dbg.Diagnostics.Assert( property != null, "Caller should validate path before calling this method"); Dbg.Diagnostics.Assert( context != null, "Caller should validate context before calling this method"); try { providerInstance.NewProperty(path, property, type, value, context); } catch (NotSupportedException) { throw; } catch (LoopFlowException) { throw; } catch (PipelineStoppedException) { throw; } catch (ActionPreferenceStopException) { throw; } catch (Exception e) // Catch-all OK, 3rd party callout. { throw NewProviderInvocationException( "NewPropertyProviderException", SessionStateStrings.NewPropertyProviderException, providerInstance.ProviderInfo, path, e); } } /// /// Gets the dynamic parameters for the new-itemproperty cmdlet. /// /// /// The path to the item if it was specified on the command line. /// /// /// The name of the property that should be created. /// /// /// The type of the property that should be created. /// /// /// The new value of the property that should be created. /// /// /// The context which the core command is running. /// /// /// An object that has properties and fields decorated with /// parsing attributes similar to a cmdlet class. /// /// /// If the refers to a provider that could not be found. /// /// /// If the refers to a drive that could not be found. /// /// /// If the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// /// /// If does not contain glob characters and /// could not be found. /// internal object NewPropertyDynamicParameters( string path, string propertyName, string type, object value, CmdletProviderContext context) { if (path == null) { return null; } ProviderInfo provider = null; CmdletProvider providerInstance = null; CmdletProviderContext newContext = new CmdletProviderContext(context); newContext.SetFilters( new Collection(), new Collection(), null); Collection providerPaths = Globber.GetGlobbedProviderPathsFromMonadPath( path, true, newContext, out provider, out providerInstance); if (providerPaths.Count > 0) { // Get the dynamic parameters for the first resolved path return NewPropertyDynamicParameters(providerInstance, providerPaths[0], propertyName, type, value, newContext); } return null; } /// /// Gets the dynamic parameters for the new-itemproperty cmdlet. /// /// /// The path to the item if it was specified on the command line. /// /// /// The name of the property to create. /// /// /// The type of the property to create. /// /// /// The value of the property. /// /// /// The instance of the provider to use. /// /// /// The context which the core command is running. /// /// /// An object that has properties and fields decorated with /// parsing attributes similar to a cmdlet class. /// /// /// If the does not support this operation. /// /// /// If the pipeline is being stopped while executing the command. /// /// /// If the provider threw an exception. /// private object NewPropertyDynamicParameters( CmdletProvider providerInstance, string path, string propertyName, string type, object value, CmdletProviderContext context) { // All parameters should have been validated by caller Dbg.Diagnostics.Assert( providerInstance != null, "Caller should validate providerInstance before calling this method"); Dbg.Diagnostics.Assert( path != null, "Caller should validate path before calling this method"); Dbg.Diagnostics.Assert( context != null, "Caller should validate context before calling this method"); object result = null; try { result = providerInstance.NewPropertyDynamicParameters(path, propertyName, type, value, context); } catch (NotSupportedException) { throw; } catch (LoopFlowException) { throw; } catch (PipelineStoppedException) { throw; } catch (ActionPreferenceStopException) { throw; } catch (Exception e) // Catch-all OK, 3rd party callout. { throw NewProviderInvocationException( "NewPropertyDynamicParametersProviderException", SessionStateStrings.NewPropertyDynamicParametersProviderException, providerInstance.ProviderInfo, path, e); } return result; } #endregion NewProperty #region RemoveProperty /// /// Removes the specified property from the specified item. /// /// /// The path(s) to the item(s) to remove the property from. /// /// /// The name of the property to remove /// /// /// Passed on to providers to force operations. /// /// /// If true, globbing is not done on paths. /// /// /// If or is null. /// /// /// If the refers to a provider that could not be found. /// /// /// If the refers to a drive that could not be found. /// /// /// If the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// internal void RemoveProperty(string[] paths, string property, bool force, bool literalPath) { if (paths == null) { throw PSTraceSource.NewArgumentNullException(nameof(paths)); } if (property == null) { throw PSTraceSource.NewArgumentNullException(nameof(property)); } CmdletProviderContext context = new CmdletProviderContext(this.ExecutionContext); context.Force = force; context.SuppressWildcardExpansion = literalPath; RemoveProperty(paths, property, context); context.ThrowFirstErrorOrDoNothing(); } /// /// Removes the specified properties from the specified item. /// /// /// The path(s) to the item(s) to remove the properties from. /// /// /// The name of the property to remove /// /// /// The context which the core command is running. /// /// /// If or is null. /// /// /// If the refers to a provider that could not be found. /// /// /// If the refers to a drive that could not be found. /// /// /// If the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// /// /// If does not contain glob characters and /// could not be found. /// internal void RemoveProperty( string[] paths, string property, CmdletProviderContext context) { if (paths == null) { throw PSTraceSource.NewArgumentNullException(nameof(paths)); } if (property == null) { throw PSTraceSource.NewArgumentNullException(nameof(property)); } foreach (string path in paths) { if (path == null) { throw PSTraceSource.NewArgumentNullException(nameof(paths)); } ProviderInfo provider = null; CmdletProvider providerInstance = null; Collection providerPaths = Globber.GetGlobbedProviderPathsFromMonadPath( path, false, context, out provider, out providerInstance); foreach (string providerPath in providerPaths) { RemoveProperty(providerInstance, providerPath, property, context); } } } /// /// Removes the property from the item at the specified path. /// /// /// The provider instance to use. /// /// /// The path to the item if it was specified on the command line. /// /// /// The name of the property to remove. /// /// /// The context which the core command is running. /// /// /// If the does not support this operation. /// /// /// If the pipeline is being stopped while executing the command. /// /// /// If the provider threw an exception. /// private void RemoveProperty( CmdletProvider providerInstance, string path, string property, CmdletProviderContext context) { // All parameters should have been validated by caller Dbg.Diagnostics.Assert( providerInstance != null, "Caller should validate providerInstance before calling this method"); Dbg.Diagnostics.Assert( path != null, "Caller should validate path before calling this method"); Dbg.Diagnostics.Assert( property != null, "Caller should validate property before calling this method"); Dbg.Diagnostics.Assert( context != null, "Caller should validate context before calling this method"); try { providerInstance.RemoveProperty(path, property, context); } catch (NotSupportedException) { throw; } catch (LoopFlowException) { throw; } catch (PipelineStoppedException) { throw; } catch (ActionPreferenceStopException) { throw; } catch (Exception e) // Catch-all OK, 3rd party callout. { throw NewProviderInvocationException( "RemovePropertyProviderException", SessionStateStrings.RemovePropertyProviderException, providerInstance.ProviderInfo, path, e); } } /// /// Gets the dynamic parameters for the remove-itemproperty cmdlet. /// /// /// The path to the item if it was specified on the command line. /// /// /// The name of the property that should be created. /// /// /// The context which the core command is running. /// /// /// An object that has properties and fields decorated with /// parsing attributes similar to a cmdlet class. /// /// /// If the refers to a provider that could not be found. /// /// /// If the refers to a drive that could not be found. /// /// /// If the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// /// /// If does not contain glob characters and /// could not be found. /// internal object RemovePropertyDynamicParameters( string path, string propertyName, CmdletProviderContext context) { if (path == null) { return null; } ProviderInfo provider = null; CmdletProvider providerInstance = null; CmdletProviderContext newContext = new CmdletProviderContext(context); newContext.SetFilters( new Collection(), new Collection(), null); Collection providerPaths = Globber.GetGlobbedProviderPathsFromMonadPath( path, true, newContext, out provider, out providerInstance); if (providerPaths.Count > 0) { // Get the dynamic parameters for the first resolved path return RemovePropertyDynamicParameters(providerInstance, providerPaths[0], propertyName, newContext); } return null; } /// /// Gets the dynamic parameters for the remove-itemproperty cmdlet. /// /// /// The path to the item if it was specified on the command line. /// /// /// The name of the property to remove. /// /// /// The instance of the provider to use. /// /// /// The context which the core command is running. /// /// /// An object that has properties and fields decorated with /// parsing attributes similar to a cmdlet class. /// /// /// If the does not support this operation. /// /// /// If the pipeline is being stopped while executing the command. /// /// /// If the provider threw an exception. /// private object RemovePropertyDynamicParameters( CmdletProvider providerInstance, string path, string propertyName, CmdletProviderContext context) { // All parameters should have been validated by caller Dbg.Diagnostics.Assert( providerInstance != null, "Caller should validate providerInstance before calling this method"); Dbg.Diagnostics.Assert( path != null, "Caller should validate path before calling this method"); Dbg.Diagnostics.Assert( context != null, "Caller should validate context before calling this method"); object result = null; try { result = providerInstance.RemovePropertyDynamicParameters(path, propertyName, context); } catch (NotSupportedException) { throw; } catch (LoopFlowException) { throw; } catch (PipelineStoppedException) { throw; } catch (ActionPreferenceStopException) { throw; } catch (Exception e) // Catch-all OK, 3rd party callout. { throw NewProviderInvocationException( "RemovePropertyDynamicParametersProviderException", SessionStateStrings.RemovePropertyDynamicParametersProviderException, providerInstance.ProviderInfo, path, e); } return result; } #endregion RemoveProperty #region CopyProperty /// /// Copies the specified property on the specified item to the specified property /// on the destination item. The source and destination items can be the same item. /// /// /// The path(s) to the item(s) to copy the property from. /// /// /// The name of the property to be copied. /// /// /// The path to the item to copy the property to. /// /// /// The name of the property to copy the property to. /// /// /// Passed on to providers to force operations. /// /// /// If true, globbing is not done on paths. /// /// /// If , , /// , or /// is null. /// /// /// If the refers to a provider that could not be found. /// /// /// If the refers to a drive that could not be found. /// /// /// If the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// internal Collection CopyProperty( string[] sourcePaths, string sourceProperty, string destinationPath, string destinationProperty, bool force, bool literalPath) { if (sourcePaths == null) { throw PSTraceSource.NewArgumentNullException(nameof(sourcePaths)); } if (sourceProperty == null) { throw PSTraceSource.NewArgumentNullException(nameof(sourceProperty)); } if (destinationPath == null) { throw PSTraceSource.NewArgumentNullException(nameof(destinationPath)); } if (destinationProperty == null) { throw PSTraceSource.NewArgumentNullException(nameof(destinationProperty)); } CmdletProviderContext context = new CmdletProviderContext(this.ExecutionContext); context.Force = force; context.SuppressWildcardExpansion = literalPath; CopyProperty(sourcePaths, sourceProperty, destinationPath, destinationProperty, context); context.ThrowFirstErrorOrDoNothing(); Collection results = context.GetAccumulatedObjects(); return results; } /// /// Copies the specified property on the specified item to the specified property /// on the destination item. The source and destination items can be the same item. /// /// /// The path(s) to the item(s) to copy the property from. /// /// /// The name of the property to be copied. /// /// /// The path to the item to copy the property to. /// /// /// The name of the property to copy the property to. /// /// /// The context which the core command is running. /// /// /// If , , /// , or /// is null. /// /// /// If the refers to a provider that could not be found. /// /// /// If the refers to a drive that could not be found. /// /// /// If the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// /// /// If does not contain glob characters and /// could not be found. /// internal void CopyProperty( string[] sourcePaths, string sourceProperty, string destinationPath, string destinationProperty, CmdletProviderContext context) { if (sourcePaths == null) { throw PSTraceSource.NewArgumentNullException(nameof(sourcePaths)); } if (sourceProperty == null) { throw PSTraceSource.NewArgumentNullException(nameof(sourceProperty)); } if (destinationPath == null) { throw PSTraceSource.NewArgumentNullException(nameof(destinationPath)); } if (destinationProperty == null) { throw PSTraceSource.NewArgumentNullException(nameof(destinationProperty)); } foreach (string sourcePath in sourcePaths) { if (sourcePath == null) { throw PSTraceSource.NewArgumentNullException(nameof(sourcePaths)); } ProviderInfo provider = null; CmdletProvider providerInstance = null; Collection providerPaths = Globber.GetGlobbedProviderPathsFromMonadPath( sourcePath, false, context, out provider, out providerInstance); if (providerPaths.Count > 0) { // Save off the original filters Collection includeFilters = context.Include; Collection excludeFilters = context.Exclude; string filterString = context.Filter; // now modify the filters so that the destination isn't filtered context.SetFilters( new Collection(), new Collection(), null); Collection providerDestinationPaths = Globber.GetGlobbedProviderPathsFromMonadPath( destinationPath, false, context, out provider, out providerInstance); // Now reapply the filters context.SetFilters( includeFilters, excludeFilters, filterString); foreach (string providerPath in providerPaths) { foreach (string providerDestinationPath in providerDestinationPaths) { CopyProperty(providerInstance, providerPath, sourceProperty, providerDestinationPath, destinationProperty, context); } } } } } /// /// Copies the property. /// /// /// The instance of the provider to use. /// /// /// The path to the item if it was specified on the command line. /// /// /// The name of the property to be copied. /// /// /// The path to the item to copy the property to. /// /// /// The name of the property to copy the property to. /// /// /// The context which the core command is running. /// /// /// If the does not support this operation. /// /// /// If the pipeline is being stopped while executing the command. /// /// /// If the provider threw an exception. /// private void CopyProperty( CmdletProvider providerInstance, string sourcePath, string sourceProperty, string destinationPath, string destinationProperty, CmdletProviderContext context) { // All parameters should have been validated by caller Dbg.Diagnostics.Assert( providerInstance != null, "Caller should validate providerInstance before calling this method"); Dbg.Diagnostics.Assert( sourcePath != null, "Caller should validate sourcePath before calling this method"); Dbg.Diagnostics.Assert( sourceProperty != null, "Caller should validate sourceProperty before calling this method"); Dbg.Diagnostics.Assert( destinationPath != null, "Caller should validate destinationPath before calling this method"); Dbg.Diagnostics.Assert( destinationProperty != null, "Caller should validate destinationProperty before calling this method"); Dbg.Diagnostics.Assert( context != null, "Caller should validate context before calling this method"); try { providerInstance.CopyProperty(sourcePath, sourceProperty, destinationPath, destinationProperty, context); } catch (NotSupportedException) { throw; } catch (LoopFlowException) { throw; } catch (PipelineStoppedException) { throw; } catch (ActionPreferenceStopException) { throw; } catch (Exception e) // Catch-all OK, 3rd party callout. { throw NewProviderInvocationException( "CopyPropertyProviderException", SessionStateStrings.CopyPropertyProviderException, providerInstance.ProviderInfo, sourcePath, e); } } /// /// Gets the dynamic parameters for the copy-itemproperty cmdlet. /// /// /// The path to the item if it was specified on the command line. /// /// /// The name of the property to be copied. /// /// /// The path to the item to copy the property to. /// /// /// The name of the property to copy the property to. /// /// /// The context which the core command is running. /// /// /// An object that has properties and fields decorated with /// parsing attributes similar to a cmdlet class. /// /// /// If the refers to a provider that could not be found. /// /// /// If the refers to a drive that could not be found. /// /// /// If the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// /// /// If does not contain glob characters and /// could not be found. /// internal object CopyPropertyDynamicParameters( string path, string sourceProperty, string destinationPath, string destinationProperty, CmdletProviderContext context) { if (path == null) { return null; } ProviderInfo provider = null; CmdletProvider providerInstance = null; CmdletProviderContext newContext = new CmdletProviderContext(context); newContext.SetFilters( new Collection(), new Collection(), null); Collection providerPaths = Globber.GetGlobbedProviderPathsFromMonadPath( path, true, newContext, out provider, out providerInstance); if (providerPaths.Count > 0) { // Get the dynamic parameters for the first resolved path return CopyPropertyDynamicParameters( providerInstance, providerPaths[0], sourceProperty, destinationPath, destinationProperty, newContext); } return null; } /// /// Gets the dynamic parameters for the copy-itemproperty cmdlet. /// /// /// The path to the item if it was specified on the command line. /// /// /// The instance of the provider to use. /// /// /// The name of the property to copy. /// /// /// The path to the item to copy the property to. /// /// /// The name of the property to copy the property to on the destination item. /// /// /// The context which the core command is running. /// /// /// An object that has properties and fields decorated with /// parsing attributes similar to a cmdlet class. /// /// /// If the does not support this operation. /// /// /// If the pipeline is being stopped while executing the command. /// /// /// If the provider threw an exception. /// private object CopyPropertyDynamicParameters( CmdletProvider providerInstance, string path, string sourceProperty, string destinationPath, string destinationProperty, CmdletProviderContext context) { // All parameters should have been validated by caller Dbg.Diagnostics.Assert( providerInstance != null, "Caller should validate providerInstance before calling this method"); Dbg.Diagnostics.Assert( path != null, "Caller should validate path before calling this method"); Dbg.Diagnostics.Assert( context != null, "Caller should validate context before calling this method"); object result = null; try { result = providerInstance.CopyPropertyDynamicParameters( path, sourceProperty, destinationPath, destinationProperty, context); } catch (NotSupportedException) { throw; } catch (LoopFlowException) { throw; } catch (PipelineStoppedException) { throw; } catch (ActionPreferenceStopException) { throw; } catch (Exception e) // Catch-all OK, 3rd party callout. { throw NewProviderInvocationException( "CopyPropertyDynamicParametersProviderException", SessionStateStrings.CopyPropertyDynamicParametersProviderException, providerInstance.ProviderInfo, path, e); } return result; } #endregion CopyProperty #region MoveProperty /// /// Moves the specified property on the specified item to the specified property /// on the destination item. The source and destination items can be the same item. /// /// /// The path(s) to the item(s) to move the property from. /// /// /// The name of the property to be moved. /// /// /// The path to the item to move the property to. /// /// /// The name of the property to move the property to. /// /// /// Passed on to providers to force operations. /// /// /// If true, globbing is not done on paths. /// /// /// If , , /// , or /// is null. /// /// /// If resolves to more than one item. /// /// /// If the refers to a provider that could not be found. /// /// /// If the refers to a drive that could not be found. /// /// /// If the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// internal Collection MoveProperty( string[] sourcePaths, string sourceProperty, string destinationPath, string destinationProperty, bool force, bool literalPath) { if (sourcePaths == null) { throw PSTraceSource.NewArgumentNullException(nameof(sourcePaths)); } if (sourceProperty == null) { throw PSTraceSource.NewArgumentNullException(nameof(sourceProperty)); } if (destinationPath == null) { throw PSTraceSource.NewArgumentNullException(nameof(destinationPath)); } if (destinationProperty == null) { throw PSTraceSource.NewArgumentNullException(nameof(destinationProperty)); } CmdletProviderContext context = new CmdletProviderContext(this.ExecutionContext); context.Force = force; context.SuppressWildcardExpansion = literalPath; MoveProperty(sourcePaths, sourceProperty, destinationPath, destinationProperty, context); context.ThrowFirstErrorOrDoNothing(); Collection results = context.GetAccumulatedObjects(); return results; } /// /// Moves the specified property on the specified item to the specified property /// on the destination item. The source and destination items can be the same item. /// /// /// The path(s) to the item(s) to move the property from. /// /// /// The name of the property to be moved. /// /// /// The path to the item to move the property to. /// /// /// The name of the property to move the property to. /// /// /// The context which the core command is running. /// /// /// If , , /// , or /// is null. /// /// /// If resolves to more than one item. /// /// /// If the refers to a provider that could not be found. /// /// /// If the refers to a drive that could not be found. /// /// /// If the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// /// /// If does not contain glob characters and /// could not be found. /// internal void MoveProperty( string[] sourcePaths, string sourceProperty, string destinationPath, string destinationProperty, CmdletProviderContext context) { if (sourcePaths == null) { throw PSTraceSource.NewArgumentNullException(nameof(sourcePaths)); } if (sourceProperty == null) { throw PSTraceSource.NewArgumentNullException(nameof(sourceProperty)); } if (destinationPath == null) { throw PSTraceSource.NewArgumentNullException(nameof(destinationPath)); } if (destinationProperty == null) { throw PSTraceSource.NewArgumentNullException(nameof(destinationProperty)); } ProviderInfo provider = null; CmdletProvider providerInstance = null; // We shouldn't be filtering the destination path CmdletProviderContext destinationContext = new CmdletProviderContext(context); destinationContext.SetFilters( new Collection(), new Collection(), null); Collection destinationProviderPaths = Globber.GetGlobbedProviderPathsFromMonadPath( destinationPath, false, destinationContext, out provider, out providerInstance); if (destinationProviderPaths.Count > 1) { ArgumentException argException = PSTraceSource.NewArgumentException( nameof(destinationPath), SessionStateStrings.MovePropertyDestinationResolveToSingle); context.WriteError(new ErrorRecord(argException, argException.GetType().FullName, ErrorCategory.InvalidArgument, destinationProviderPaths)); } else { foreach (string sourcePath in sourcePaths) { if (sourcePath == null) { throw PSTraceSource.NewArgumentNullException(nameof(sourcePaths)); } Collection providerPaths = Globber.GetGlobbedProviderPathsFromMonadPath( sourcePath, false, context, out provider, out providerInstance); foreach (string providerPath in providerPaths) { MoveProperty(providerInstance, providerPath, sourceProperty, destinationProviderPaths[0], destinationProperty, context); } } } } /// /// Moves the property from one item to another. /// /// /// The provider instance to use. /// /// /// The path to the item if it was specified on the command line. /// /// /// The property to be moved. /// /// /// The path of the item to move the property to. /// /// /// The name of the property to move the property to. /// /// /// The context which the core command is running. /// /// /// If the does not support this operation. /// /// /// If the pipeline is being stopped while executing the command. /// /// /// If the provider threw an exception. /// private void MoveProperty( CmdletProvider providerInstance, string sourcePath, string sourceProperty, string destinationPath, string destinationProperty, CmdletProviderContext context) { // All parameters should have been validated by caller Dbg.Diagnostics.Assert( providerInstance != null, "Caller should validate providerInstance before calling this method"); Dbg.Diagnostics.Assert( sourcePath != null, "Caller should validate sourcePath before calling this method"); Dbg.Diagnostics.Assert( sourceProperty != null, "Caller should validate sourceProperty before calling this method"); Dbg.Diagnostics.Assert( destinationPath != null, "Caller should validate destinationPath before calling this method"); Dbg.Diagnostics.Assert( destinationProperty != null, "Caller should validate destinationProperty before calling this method"); Dbg.Diagnostics.Assert( context != null, "Caller should validate context before calling this method"); try { providerInstance.MoveProperty(sourcePath, sourceProperty, destinationPath, destinationProperty, context); } catch (NotSupportedException) { throw; } catch (LoopFlowException) { throw; } catch (PipelineStoppedException) { throw; } catch (ActionPreferenceStopException) { throw; } catch (Exception e) // Catch-all OK, 3rd party callout. { throw NewProviderInvocationException( "MovePropertyProviderException", SessionStateStrings.MovePropertyProviderException, providerInstance.ProviderInfo, sourcePath, e); } } /// /// Gets the dynamic parameters for the move-itemproperty cmdlet. /// /// /// The path to the item if it was specified on the command line. /// /// /// The name of the property to be moved. /// /// /// The path to the item to move the property to. /// /// /// The name of the property to move the property to. /// /// /// The context which the core command is running. /// /// /// An object that has properties and fields decorated with /// parsing attributes similar to a cmdlet class. /// /// /// If the refers to a provider that could not be found. /// /// /// If the refers to a drive that could not be found. /// /// /// If the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// /// /// If does not contain glob characters and /// could not be found. /// internal object MovePropertyDynamicParameters( string path, string sourceProperty, string destinationPath, string destinationProperty, CmdletProviderContext context) { if (path == null) { return null; } ProviderInfo provider = null; CmdletProvider providerInstance = null; CmdletProviderContext newContext = new CmdletProviderContext(context); newContext.SetFilters( new Collection(), new Collection(), null); Collection providerPaths = Globber.GetGlobbedProviderPathsFromMonadPath( path, true, newContext, out provider, out providerInstance); if (providerPaths.Count > 0) { // Get the dynamic parameters for the first resolved path return MovePropertyDynamicParameters( providerInstance, providerPaths[0], sourceProperty, destinationPath, destinationProperty, newContext); } return null; } /// /// Gets the dynamic parameters for the move-itemproperty cmdlet. /// /// /// The path to the item if it was specified on the command line. /// /// /// The name of the property to move. /// /// /// The path to the item to move the property to. /// /// /// The name of the property on the destination item to move the property to. /// /// /// The instance of the provider to use. /// /// /// The context which the core command is running. /// /// /// An object that has properties and fields decorated with /// parsing attributes similar to a cmdlet class. /// /// /// If the does not support this operation. /// /// /// If the pipeline is being stopped while executing the command. /// /// /// If the provider threw an exception. /// private object MovePropertyDynamicParameters( CmdletProvider providerInstance, string path, string sourceProperty, string destinationPath, string destinationProperty, CmdletProviderContext context) { // All parameters should have been validated by caller Dbg.Diagnostics.Assert( providerInstance != null, "Caller should validate providerInstance before calling this method"); Dbg.Diagnostics.Assert( path != null, "Caller should validate path before calling this method"); Dbg.Diagnostics.Assert( context != null, "Caller should validate context before calling this method"); object result = null; try { result = providerInstance.MovePropertyDynamicParameters( path, sourceProperty, destinationPath, destinationProperty, context); } catch (NotSupportedException) { throw; } catch (LoopFlowException) { throw; } catch (PipelineStoppedException) { throw; } catch (ActionPreferenceStopException) { throw; } catch (Exception e) // Catch-all OK, 3rd party callout. { throw NewProviderInvocationException( "MovePropertyDynamicParametersProviderException", SessionStateStrings.MovePropertyDynamicParametersProviderException, providerInstance.ProviderInfo, path, e); } return result; } #endregion MoveProperty #region RenameProperty /// /// Renames the specified property on the specified item to the specified property. /// /// /// The path(s) to the item(s) to rename the property on. /// /// /// The name of the property to be renamed. /// /// /// The name of the property to rename the property to. /// /// /// Passed on to providers to force operations. /// /// /// If true, globbing is not done on paths. /// /// /// If , , /// or is null. /// /// /// If the refers to a provider that could not be found. /// /// /// If the refers to a drive that could not be found. /// /// /// If the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// internal Collection RenameProperty( string[] sourcePaths, string sourceProperty, string destinationProperty, bool force, bool literalPath) { if (sourcePaths == null) { throw PSTraceSource.NewArgumentNullException(nameof(sourcePaths)); } if (sourceProperty == null) { throw PSTraceSource.NewArgumentNullException(nameof(sourceProperty)); } if (destinationProperty == null) { throw PSTraceSource.NewArgumentNullException(nameof(destinationProperty)); } CmdletProviderContext context = new CmdletProviderContext(this.ExecutionContext); context.Force = force; context.SuppressWildcardExpansion = literalPath; RenameProperty(sourcePaths, sourceProperty, destinationProperty, context); context.ThrowFirstErrorOrDoNothing(); Collection results = context.GetAccumulatedObjects(); return results; } /// /// Renames the specified property on the specified item to the specified property. /// /// /// The path(s) to the item(s) to rename the property on. /// /// /// The name of the property to be renamed. /// /// /// The name of the property to rename the property to. /// /// /// The context which the core command is running. /// /// /// If , , /// or is null. /// /// /// If the refers to a provider that could not be found. /// /// /// If the refers to a drive that could not be found. /// /// /// If the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// /// /// If does not contain glob characters and /// could not be found. /// internal void RenameProperty( string[] paths, string sourceProperty, string destinationProperty, CmdletProviderContext context) { if (paths == null) { throw PSTraceSource.NewArgumentNullException(nameof(paths)); } if (sourceProperty == null) { throw PSTraceSource.NewArgumentNullException(nameof(sourceProperty)); } if (destinationProperty == null) { throw PSTraceSource.NewArgumentNullException(nameof(destinationProperty)); } foreach (string path in paths) { if (path == null) { throw PSTraceSource.NewArgumentNullException(nameof(paths)); } ProviderInfo provider = null; CmdletProvider providerInstance = null; Collection providerPaths = Globber.GetGlobbedProviderPathsFromMonadPath( path, false, context, out provider, out providerInstance); foreach (string providerPath in providerPaths) { RenameProperty(providerInstance, providerPath, sourceProperty, destinationProperty, context); } } } /// /// Renames the property of the item at the specified path. /// /// /// The provider instance to use. /// /// /// The path to the item if it was specified on the command line. /// /// /// The name of the property to rename. /// /// /// The new name of the property. /// /// /// The context which the core command is running. /// /// /// If the does not support this operation. /// /// /// If the pipeline is being stopped while executing the command. /// /// /// If the provider threw an exception. /// private void RenameProperty( CmdletProvider providerInstance, string sourcePath, string sourceProperty, string destinationProperty, CmdletProviderContext context) { // All parameters should have been validated by caller Dbg.Diagnostics.Assert( providerInstance != null, "Caller should validate providerInstance before calling this method"); Dbg.Diagnostics.Assert( sourcePath != null, "Caller should validate sourcePath before calling this method"); Dbg.Diagnostics.Assert( sourceProperty != null, "Caller should validate sourceProperty before calling this method"); Dbg.Diagnostics.Assert( destinationProperty != null, "Caller should validate destinationProperty before calling this method"); Dbg.Diagnostics.Assert( context != null, "Caller should validate context before calling this method"); try { providerInstance.RenameProperty(sourcePath, sourceProperty, destinationProperty, context); } catch (NotSupportedException) { throw; } catch (LoopFlowException) { throw; } catch (PipelineStoppedException) { throw; } catch (ActionPreferenceStopException) { throw; } catch (Exception e) // Catch-all OK, 3rd party callout. { throw NewProviderInvocationException( "RenamePropertyProviderException", SessionStateStrings.RenamePropertyProviderException, providerInstance.ProviderInfo, sourcePath, e); } } /// /// Gets the dynamic parameters for the rename-itemproperty cmdlet. /// /// /// The path to the item if it was specified on the command line. /// /// /// The name of the property to be renamed. /// /// /// The name of the property to rename the property to. /// /// /// The context which the core command is running. /// /// /// An object that has properties and fields decorated with /// parsing attributes similar to a cmdlet class. /// /// /// If the refers to a provider that could not be found. /// /// /// If the refers to a drive that could not be found. /// /// /// If the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// /// /// If does not contain glob characters and /// could not be found. /// internal object RenamePropertyDynamicParameters( string path, string sourceProperty, string destinationProperty, CmdletProviderContext context) { if (path == null) { return null; } ProviderInfo provider = null; CmdletProvider providerInstance = null; CmdletProviderContext newContext = new CmdletProviderContext(context); newContext.SetFilters( new Collection(), new Collection(), null); Collection providerPaths = Globber.GetGlobbedProviderPathsFromMonadPath( path, true, newContext, out provider, out providerInstance); if (providerPaths.Count > 0) { // Get the dynamic parameters for the first resolved path return RenamePropertyDynamicParameters( providerInstance, providerPaths[0], sourceProperty, destinationProperty, newContext); } return null; } /// /// Gets the dynamic parameters for the rename-itemproperty cmdlet. /// /// /// The path to the item if it was specified on the command line. /// /// /// The instance of the provider to use. /// /// /// The name of the property to rename. /// /// /// The new name for the property. /// /// /// The context which the core command is running. /// /// /// An object that has properties and fields decorated with /// parsing attributes similar to a cmdlet class. /// /// /// If the does not support this operation. /// /// /// If the pipeline is being stopped while executing the command. /// /// /// If the provider threw an exception. /// private object RenamePropertyDynamicParameters( CmdletProvider providerInstance, string path, string sourceProperty, string destinationProperty, CmdletProviderContext context) { // All parameters should have been validated by caller Dbg.Diagnostics.Assert( providerInstance != null, "Caller should validate providerInstance before calling this method"); Dbg.Diagnostics.Assert( path != null, "Caller should validate path before calling this method"); Dbg.Diagnostics.Assert( context != null, "Caller should validate context before calling this method"); object result = null; try { result = providerInstance.RenamePropertyDynamicParameters( path, sourceProperty, destinationProperty, context); } catch (NotSupportedException) { throw; } catch (LoopFlowException) { throw; } catch (PipelineStoppedException) { throw; } catch (ActionPreferenceStopException) { throw; } catch (Exception e) // Catch-all OK, 3rd party callout. { throw NewProviderInvocationException( "RenamePropertyDynamicParametersProviderException", SessionStateStrings.RenamePropertyDynamicParametersProviderException, providerInstance.ProviderInfo, path, e); } return result; } #endregion RenameProperty #endregion IDynamicPropertyCmdletProvider accessors } } #pragma warning restore 56500