2018-02-13 18:23:53 +01:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
// Licensed under the MIT License.
|
2016-07-29 22:02:49 +02:00
|
|
|
|
2016-03-30 23:20:52 +02:00
|
|
|
using System.Management.Automation;
|
2019-04-30 07:25:11 +02:00
|
|
|
|
2016-03-30 23:20:52 +02:00
|
|
|
using Dbg = System.Management.Automation;
|
|
|
|
|
|
|
|
namespace Microsoft.PowerShell.Commands
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// A command to copy a property on an item.
|
|
|
|
/// </summary>
|
2016-07-29 22:02:49 +02:00
|
|
|
[Cmdlet(VerbsCommon.Copy, "ItemProperty", DefaultParameterSetName = "Path", SupportsShouldProcess = true, SupportsTransactions = true,
|
2016-08-24 03:21:31 +02:00
|
|
|
HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113293")]
|
2016-03-30 23:20:52 +02:00
|
|
|
public class CopyItemPropertyCommand : PassThroughItemPropertyCommandBase
|
|
|
|
{
|
|
|
|
#region Parameters
|
|
|
|
|
|
|
|
/// <summary>
|
2019-01-06 00:40:24 +01:00
|
|
|
/// Gets or sets the path parameter to the command.
|
2016-03-30 23:20:52 +02:00
|
|
|
/// </summary>
|
|
|
|
[Parameter(Position = 0, ParameterSetName = "Path",
|
2016-07-29 22:02:49 +02:00
|
|
|
Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
|
2016-03-30 23:20:52 +02:00
|
|
|
public string[] Path
|
|
|
|
{
|
2016-08-02 22:29:09 +02:00
|
|
|
get { return paths; }
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2016-08-02 22:29:09 +02:00
|
|
|
set { paths = value; }
|
|
|
|
}
|
2016-03-30 23:20:52 +02:00
|
|
|
|
|
|
|
/// <summary>
|
2019-01-06 00:40:24 +01:00
|
|
|
/// Gets or sets the literal path parameter to the command.
|
2016-03-30 23:20:52 +02:00
|
|
|
/// </summary>
|
|
|
|
[Parameter(ParameterSetName = "LiteralPath",
|
|
|
|
Mandatory = true, ValueFromPipeline = false, ValueFromPipelineByPropertyName = true)]
|
2018-07-26 19:14:09 +02:00
|
|
|
[Alias("PSPath", "LP")]
|
2016-03-30 23:20:52 +02:00
|
|
|
public string[] LiteralPath
|
|
|
|
{
|
2016-08-02 22:29:09 +02:00
|
|
|
get { return paths; }
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2016-03-30 23:20:52 +02:00
|
|
|
set
|
|
|
|
{
|
|
|
|
base.SuppressWildcardExpansion = true;
|
|
|
|
paths = value;
|
2016-08-02 22:29:09 +02:00
|
|
|
}
|
|
|
|
}
|
2016-03-30 23:20:52 +02:00
|
|
|
|
|
|
|
/// <summary>
|
2019-01-06 00:40:24 +01:00
|
|
|
/// The name of the property to create on the item.
|
2016-03-30 23:20:52 +02:00
|
|
|
/// </summary>
|
|
|
|
[Parameter(Position = 2, Mandatory = true, ValueFromPipelineByPropertyName = true)]
|
|
|
|
[Alias("PSProperty")]
|
2016-08-02 22:29:09 +02:00
|
|
|
public string Name { get; set; }
|
2016-03-30 23:20:52 +02:00
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// The path to the destination item to copy the property to.
|
|
|
|
/// </summary>
|
2016-07-29 22:02:49 +02:00
|
|
|
[Parameter(Mandatory = true, Position = 1, ValueFromPipelineByPropertyName = true)]
|
2016-08-02 22:29:09 +02:00
|
|
|
public string Destination { get; set; }
|
2016-07-29 22:02:49 +02:00
|
|
|
|
2016-03-30 23:20:52 +02:00
|
|
|
/// <summary>
|
|
|
|
/// A virtual method for retrieving the dynamic parameters for a cmdlet. Derived cmdlets
|
|
|
|
/// that require dynamic parameters should override this method and return the
|
|
|
|
/// dynamic parameter object.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="context">
|
|
|
|
/// The context under which the command is running.
|
|
|
|
/// </param>
|
|
|
|
/// <returns>
|
|
|
|
/// An object representing the dynamic parameters for the cmdlet or null if there
|
|
|
|
/// are none.
|
|
|
|
/// </returns>
|
|
|
|
internal override object GetDynamicParameters(CmdletProviderContext context)
|
|
|
|
{
|
2016-08-21 23:56:12 +02:00
|
|
|
if (Path != null && Path.Length > 0)
|
2016-03-30 23:20:52 +02:00
|
|
|
{
|
|
|
|
return InvokeProvider.Property.CopyPropertyDynamicParameters(
|
2016-07-29 22:02:49 +02:00
|
|
|
Path[0],
|
2016-08-02 22:29:09 +02:00
|
|
|
Name,
|
2016-07-29 22:02:49 +02:00
|
|
|
Destination,
|
2016-08-02 22:29:09 +02:00
|
|
|
Name,
|
2016-03-30 23:20:52 +02:00
|
|
|
context);
|
|
|
|
}
|
2018-12-24 07:20:06 +01:00
|
|
|
|
2016-03-30 23:20:52 +02:00
|
|
|
return InvokeProvider.Property.CopyPropertyDynamicParameters(
|
|
|
|
".",
|
2016-08-02 22:29:09 +02:00
|
|
|
Name,
|
2016-03-30 23:20:52 +02:00
|
|
|
Destination,
|
2016-08-02 22:29:09 +02:00
|
|
|
Name,
|
2016-03-30 23:20:52 +02:00
|
|
|
context);
|
2018-12-08 17:53:30 +01:00
|
|
|
}
|
2016-07-29 22:02:49 +02:00
|
|
|
|
2016-03-30 23:20:52 +02:00
|
|
|
#endregion Parameters
|
|
|
|
|
|
|
|
#region parameter data
|
2016-07-29 22:02:49 +02:00
|
|
|
|
2016-03-30 23:20:52 +02:00
|
|
|
#endregion parameter data
|
|
|
|
|
|
|
|
#region Command code
|
|
|
|
|
|
|
|
/// <summary>
|
2019-01-06 00:40:24 +01:00
|
|
|
/// Copies the property from one item to another.
|
2016-03-30 23:20:52 +02:00
|
|
|
/// </summary>
|
2016-07-29 22:02:49 +02:00
|
|
|
protected override void ProcessRecord()
|
2016-03-30 23:20:52 +02:00
|
|
|
{
|
|
|
|
foreach (string path in Path)
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
InvokeProvider.Property.Copy(
|
2016-07-29 22:02:49 +02:00
|
|
|
path,
|
2016-08-02 22:29:09 +02:00
|
|
|
Name,
|
2016-07-29 22:02:49 +02:00
|
|
|
Destination,
|
2016-08-02 22:29:09 +02:00
|
|
|
Name,
|
2016-03-30 23:20:52 +02:00
|
|
|
GetCurrentContext());
|
|
|
|
}
|
|
|
|
catch (PSNotSupportedException notSupported)
|
|
|
|
{
|
|
|
|
WriteError(
|
|
|
|
new ErrorRecord(
|
|
|
|
notSupported.ErrorRecord,
|
|
|
|
notSupported));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
catch (DriveNotFoundException driveNotFound)
|
|
|
|
{
|
|
|
|
WriteError(
|
|
|
|
new ErrorRecord(
|
|
|
|
driveNotFound.ErrorRecord,
|
|
|
|
driveNotFound));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
catch (ProviderNotFoundException providerNotFound)
|
|
|
|
{
|
|
|
|
WriteError(
|
|
|
|
new ErrorRecord(
|
|
|
|
providerNotFound.ErrorRecord,
|
|
|
|
providerNotFound));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
catch (ItemNotFoundException pathNotFound)
|
|
|
|
{
|
|
|
|
WriteError(
|
|
|
|
new ErrorRecord(
|
|
|
|
pathNotFound.ErrorRecord,
|
|
|
|
pathNotFound));
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2018-12-08 17:53:30 +01:00
|
|
|
}
|
2016-03-30 23:20:52 +02:00
|
|
|
#endregion Command code
|
|
|
|
|
2018-12-08 17:53:30 +01:00
|
|
|
}
|
|
|
|
}
|