2016-03-30 14:20:52 -07:00
|
|
|
/********************************************************************++
|
|
|
|
|
Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
|
--********************************************************************/
|
2016-07-29 13:02:49 -07:00
|
|
|
|
2016-03-30 14:20:52 -07:00
|
|
|
using System.Collections.ObjectModel;
|
|
|
|
|
using System.Diagnostics.CodeAnalysis;
|
|
|
|
|
using System.Management.Automation;
|
|
|
|
|
using Dbg = System.Management.Automation;
|
|
|
|
|
|
|
|
|
|
namespace Microsoft.PowerShell.Commands
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A command to get the property of an item at a specified path
|
|
|
|
|
/// </summary>
|
2016-08-23 21:21:31 -04:00
|
|
|
[Cmdlet(VerbsCommon.Get, "ItemProperty", DefaultParameterSetName = "Path", SupportsTransactions = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113320")]
|
2016-03-30 14:20:52 -07:00
|
|
|
public class GetItemPropertyCommand : ItemPropertyCommandBase
|
|
|
|
|
{
|
|
|
|
|
#region Parameters
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the path parameter to the command
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter(Position = 0, ParameterSetName = "Path",
|
2016-07-29 13:02:49 -07:00
|
|
|
Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
|
2016-03-30 14:20:52 -07:00
|
|
|
public string[] Path
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return paths;
|
|
|
|
|
} // get
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
paths = value;
|
|
|
|
|
} // set
|
|
|
|
|
} // Path
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the literal path parameter to the command
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter(ParameterSetName = "LiteralPath",
|
2016-07-29 13:02:49 -07:00
|
|
|
Mandatory = true, ValueFromPipeline = false, ValueFromPipelineByPropertyName = true)]
|
2016-03-30 14:20:52 -07:00
|
|
|
[Alias("PSPath")]
|
|
|
|
|
public string[] LiteralPath
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return paths;
|
|
|
|
|
} // get
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
base.SuppressWildcardExpansion = true;
|
|
|
|
|
paths = value;
|
|
|
|
|
} // set
|
|
|
|
|
} // LiteralPath
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The properties to retrieve from the item
|
|
|
|
|
/// </summary>
|
|
|
|
|
///
|
|
|
|
|
[Parameter(Position = 1)]
|
|
|
|
|
[Alias("PSProperty")]
|
|
|
|
|
public string[] Name
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2016-07-29 13:02:49 -07:00
|
|
|
return _property;
|
2016-03-30 14:20:52 -07:00
|
|
|
} // get
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
2016-07-29 13:02:49 -07:00
|
|
|
_property = value;
|
2016-03-30 14:20:52 -07:00
|
|
|
}
|
|
|
|
|
} // Property
|
|
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
|
{
|
|
|
|
|
if (Path != null && Path.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
return InvokeProvider.Property.GetPropertyDynamicParameters(
|
2016-07-29 13:02:49 -07:00
|
|
|
Path[0],
|
|
|
|
|
SessionStateUtilities.ConvertArrayToCollection<string>(_property), context);
|
2016-03-30 14:20:52 -07:00
|
|
|
}
|
|
|
|
|
return InvokeProvider.Property.GetPropertyDynamicParameters(
|
|
|
|
|
".",
|
2016-07-29 13:02:49 -07:00
|
|
|
SessionStateUtilities.ConvertArrayToCollection<string>(_property), context);
|
2016-03-30 14:20:52 -07:00
|
|
|
} // GetDynamicParameters
|
2016-07-29 13:02:49 -07:00
|
|
|
|
2016-03-30 14:20:52 -07:00
|
|
|
#endregion Parameters
|
|
|
|
|
|
|
|
|
|
#region parameter data
|
2016-07-29 13:02:49 -07:00
|
|
|
|
2016-03-30 14:20:52 -07:00
|
|
|
/// <summary>
|
|
|
|
|
/// The properties to be retrieved.
|
|
|
|
|
/// </summary>
|
2016-07-29 13:02:49 -07:00
|
|
|
private string[] _property;
|
2016-03-30 14:20:52 -07:00
|
|
|
|
|
|
|
|
#endregion parameter data
|
|
|
|
|
|
|
|
|
|
#region Command code
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the properties of an item at the specified path
|
|
|
|
|
/// </summary>
|
2016-07-29 13:02:49 -07:00
|
|
|
protected override void ProcessRecord()
|
2016-03-30 14:20:52 -07:00
|
|
|
{
|
2016-07-29 13:02:49 -07:00
|
|
|
foreach (string path in Path)
|
|
|
|
|
{
|
2016-03-30 14:20:52 -07:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
InvokeProvider.Property.Get(
|
2016-07-29 13:02:49 -07:00
|
|
|
path,
|
|
|
|
|
SessionStateUtilities.ConvertArrayToCollection<string>(_property),
|
2016-03-30 14:20:52 -07:00
|
|
|
CmdletProviderContext);
|
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} // ProcessRecord
|
|
|
|
|
|
|
|
|
|
#endregion Command code
|
|
|
|
|
} // GetItemPropertyCommand
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// A command to get the property value of an item at a specified path.
|
|
|
|
|
/// </summary>
|
2016-08-23 21:21:31 -04:00
|
|
|
[Cmdlet(VerbsCommon.Get, "ItemPropertyValue", DefaultParameterSetName = "Path", SupportsTransactions = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkId=389281")]
|
2016-03-30 14:20:52 -07:00
|
|
|
public sealed class GetItemPropertyValueCommand : ItemPropertyCommandBase
|
|
|
|
|
{
|
|
|
|
|
#region Parameters
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the path parameter to the command
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter(Position = 0, ParameterSetName = "Path", Mandatory = false, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
|
|
|
|
|
[ValidateNotNullOrEmpty]
|
|
|
|
|
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
|
|
|
|
|
public string[] Path
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return paths;
|
|
|
|
|
} // get
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
paths = value;
|
|
|
|
|
} // set
|
|
|
|
|
} // Path
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets or sets the literal path parameter to the command
|
|
|
|
|
/// </summary>
|
|
|
|
|
[Parameter(ParameterSetName = "LiteralPath", Mandatory = true, ValueFromPipelineByPropertyName = true)]
|
|
|
|
|
[Alias("PSPath")]
|
|
|
|
|
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
|
|
|
|
|
public string[] LiteralPath
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
return paths;
|
|
|
|
|
} // get
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
|
|
|
|
base.SuppressWildcardExpansion = true;
|
|
|
|
|
paths = value;
|
|
|
|
|
} // set
|
|
|
|
|
} // LiteralPath
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The properties to retrieve from the item
|
|
|
|
|
/// </summary>
|
|
|
|
|
///
|
|
|
|
|
[Parameter(Position = 1, Mandatory = true)]
|
|
|
|
|
[Alias("PSProperty")]
|
|
|
|
|
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
|
|
|
|
|
public string[] Name
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
2016-07-29 13:02:49 -07:00
|
|
|
return _property;
|
2016-03-30 14:20:52 -07:00
|
|
|
} // get
|
|
|
|
|
|
|
|
|
|
set
|
|
|
|
|
{
|
2016-07-29 13:02:49 -07:00
|
|
|
_property = value;
|
2016-03-30 14:20:52 -07:00
|
|
|
}
|
|
|
|
|
} // Property
|
|
|
|
|
|
|
|
|
|
/// <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)
|
|
|
|
|
{
|
|
|
|
|
if (Path != null && Path.Length > 0)
|
|
|
|
|
{
|
|
|
|
|
return InvokeProvider.Property.GetPropertyDynamicParameters(
|
|
|
|
|
Path[0],
|
2016-07-29 13:02:49 -07:00
|
|
|
SessionStateUtilities.ConvertArrayToCollection<string>(_property), context);
|
2016-03-30 14:20:52 -07:00
|
|
|
}
|
|
|
|
|
return InvokeProvider.Property.GetPropertyDynamicParameters(
|
|
|
|
|
".",
|
2016-07-29 13:02:49 -07:00
|
|
|
SessionStateUtilities.ConvertArrayToCollection<string>(_property), context);
|
2016-03-30 14:20:52 -07:00
|
|
|
} // GetDynamicParameters
|
|
|
|
|
|
|
|
|
|
#endregion Parameters
|
|
|
|
|
|
|
|
|
|
#region parameter data
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// The properties to be retrieved.
|
|
|
|
|
/// </summary>
|
2016-07-29 13:02:49 -07:00
|
|
|
private string[] _property;
|
2016-03-30 14:20:52 -07:00
|
|
|
|
|
|
|
|
#endregion parameter data
|
|
|
|
|
|
|
|
|
|
#region Command code
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Gets the values of the properties of an item at the specified path.
|
|
|
|
|
/// </summary>
|
|
|
|
|
protected override void ProcessRecord()
|
|
|
|
|
{
|
2016-07-29 13:02:49 -07:00
|
|
|
if (Path == null || Path.Length == 0)
|
|
|
|
|
{
|
|
|
|
|
paths = new string[] { "." };
|
|
|
|
|
}
|
|
|
|
|
foreach (string path in Path)
|
|
|
|
|
{
|
2016-03-30 14:20:52 -07:00
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
Collection<PSObject> itemProperties = InvokeProvider.Property.Get(
|
|
|
|
|
new string[] { path },
|
2016-07-29 13:02:49 -07:00
|
|
|
SessionStateUtilities.ConvertArrayToCollection<string>(_property),
|
2016-03-30 14:20:52 -07:00
|
|
|
base.SuppressWildcardExpansion);
|
|
|
|
|
|
|
|
|
|
if (itemProperties != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (PSObject currentItem in itemProperties)
|
|
|
|
|
{
|
|
|
|
|
if (this.Name != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (string currentPropertyName in this.Name)
|
|
|
|
|
{
|
2016-07-29 13:02:49 -07:00
|
|
|
if (currentItem.Properties != null &&
|
2016-03-30 14:20:52 -07:00
|
|
|
currentItem.Properties[currentPropertyName] != null &&
|
|
|
|
|
currentItem.Properties[currentPropertyName].Value != null)
|
|
|
|
|
{
|
|
|
|
|
CmdletProviderContext.WriteObject(currentItem.Properties[currentPropertyName].Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-07-29 13:02:49 -07:00
|
|
|
}
|
2016-03-30 14:20:52 -07:00
|
|
|
|
|
|
|
|
#endregion Command code
|
2016-07-29 13:02:49 -07:00
|
|
|
}
|
2016-03-30 14:20:52 -07:00
|
|
|
} // namespace Microsoft.PowerShell.Commands
|