File size: 18,646 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Management.Automation.Language;
using System.Text;
namespace System.Management.Automation
{
/// <summary>
/// The command information for cmdlets that are directly executable by PowerShell.
/// </summary>
public class CmdletInfo : CommandInfo
{
#region ctor
/// <summary>
/// Constructs a CmdletInfo object from the raw cmdlet data. This should only
/// be used for Intrinsic commands.
/// </summary>
/// <param name="name">
/// The name of the cmdlet.
/// </param>
/// <param name="implementingType">
/// The type information about the class that implements the cmdlet.
/// </param>
/// <param name="helpFile">
/// The name of the help file associated with the cmdlet
/// </param>
/// <param name="PSSnapin">
/// The PSSnapInInfo of the PSSnapin the cmdlet comes from.
/// </param>
/// <param name="context">
/// The current engine context.
/// </param>
internal CmdletInfo(
string name,
Type implementingType,
string helpFile,
PSSnapInInfo PSSnapin,
ExecutionContext context)
: base(name, CommandTypes.Cmdlet, context)
{
if (string.IsNullOrEmpty(name))
{
throw PSTraceSource.NewArgumentException(nameof(name));
}
// Get the verb and noun from the name
if (!SplitCmdletName(name, out _verb, out _noun))
{
throw
PSTraceSource.NewArgumentException(
nameof(name),
DiscoveryExceptions.InvalidCmdletNameFormat,
name);
}
_implementingType = implementingType;
_helpFilePath = helpFile;
_PSSnapin = PSSnapin;
_options = ScopedItemOptions.ReadOnly;
// CmdletInfo represents cmdlets exposed from assemblies. On a locked down system, only trusted
// assemblies will be loaded. Therefore, a CmdletInfo instance will always be trusted.
this.DefiningLanguageMode = PSLanguageMode.FullLanguage;
}
/// <summary>
/// This is a copy constructor, used primarily for get-command.
/// </summary>
internal CmdletInfo(CmdletInfo other)
: base(other)
{
_verb = other._verb;
_noun = other._noun;
_implementingType = other._implementingType;
_helpFilePath = other._helpFilePath;
_PSSnapin = other._PSSnapin;
_options = ScopedItemOptions.ReadOnly;
}
/// <summary>
/// Create a copy of commandInfo for GetCommandCommand so that we can generate parameter
/// sets based on an argument list (so we can get the dynamic parameters.)
/// </summary>
internal override CommandInfo CreateGetCommandCopy(object[] arguments)
{
CmdletInfo copy = new CmdletInfo(this);
copy.IsGetCommandCopy = true;
copy.Arguments = arguments;
return copy;
}
/// <summary>
/// Directly create a cmdlet object from a type. This allows
/// unregistered commands to be called. It also allows the overhead of
/// command discovery to be bypassed.
/// </summary>
/// <param name="name">The name to use for the cmdlet, must be in the form Noun-Verb.</param>
/// <param name="implementingType">The .NET class implementing this cmdlet.</param>
public CmdletInfo(string name, Type implementingType)
: base(name, CommandTypes.Cmdlet, null)
{
if (string.IsNullOrEmpty(name))
{
throw PSTraceSource.NewArgumentNullException(nameof(name));
}
if (implementingType == null)
{
throw PSTraceSource.NewArgumentNullException(nameof(name));
}
if (!typeof(Cmdlet).IsAssignableFrom(implementingType))
{
throw PSTraceSource.NewInvalidOperationException(DiscoveryExceptions.CmdletDoesNotDeriveFromCmdletType, "implementingType", implementingType.FullName);
}
// Get the verb and noun from the name
if (!SplitCmdletName(name, out _verb, out _noun))
{
throw
PSTraceSource.NewArgumentException(
nameof(name),
DiscoveryExceptions.InvalidCmdletNameFormat,
name);
}
_implementingType = implementingType;
_helpFilePath = string.Empty;
_PSSnapin = null;
_options = ScopedItemOptions.ReadOnly;
}
#endregion ctor
#region public members
/// <summary>
/// Gets the verb of the cmdlet.
/// </summary>
public string Verb
{
get
{
return _verb;
}
}
private readonly string _verb = string.Empty;
/// <summary>
/// Gets the noun of the cmdlet.
/// </summary>
public string Noun
{
get
{
return _noun;
}
}
private readonly string _noun = string.Empty;
internal static bool SplitCmdletName(string name, out string verb, out string noun)
{
noun = verb = string.Empty;
if (string.IsNullOrEmpty(name))
return false;
int index = 0;
for (int i = 0; i < name.Length; i++)
{
if (CharExtensions.IsDash(name[i]))
{
index = i;
break;
}
}
if (index > 0)
{
verb = name.Substring(0, index);
noun = name.Substring(index + 1);
return true;
}
return false;
}
/// <summary>
/// Gets the help file path for the cmdlet.
/// </summary>
public string HelpFile
{
get
{
return _helpFilePath;
}
internal set
{
_helpFilePath = value;
}
}
private string _helpFilePath = string.Empty;
internal override HelpCategory HelpCategory
{
get { return HelpCategory.Cmdlet; }
}
/// <summary>
/// Gets the Snap-in in which the cmdlet is implemented.
/// </summary>
public PSSnapInInfo PSSnapIn
{
get
{
return _PSSnapin;
}
}
private readonly PSSnapInInfo _PSSnapin;
/// <summary>
/// Gets the name of the PSSnapin the cmdlet is implemented in.
/// </summary>
internal string PSSnapInName
{
get
{
string result = null;
if (_PSSnapin != null)
{
result = _PSSnapin.Name;
}
return result;
}
}
/// <summary>
/// Gets the source version.
/// </summary>
public override Version Version
{
get
{
if (_version == null)
{
if (Module != null)
{
_version = base.Version;
}
else if (_PSSnapin != null)
{
_version = _PSSnapin.Version;
}
}
return _version;
}
}
private Version _version;
/// <summary>
/// Gets the type that implements the cmdlet.
/// </summary>
public Type ImplementingType
{
get
{
return _implementingType;
}
}
private readonly Type _implementingType = null;
/// <summary>
/// Gets the synopsis of the cmdlet.
/// </summary>
public override string Definition
{
get
{
StringBuilder synopsis = new StringBuilder();
if (this.ImplementingType != null)
{
foreach (CommandParameterSetInfo parameterSet in ParameterSets)
{
synopsis.AppendLine();
synopsis.AppendLine(
string.Format(
System.Globalization.CultureInfo.CurrentCulture,
"{0}{1}{2} {3}",
_verb,
StringLiterals.CommandVerbNounSeparator,
_noun,
parameterSet.ToString()));
}
}
else
{
// Skip the synopsis documentation if the cmdlet hasn't been loaded yet.
synopsis.AppendLine(
string.Format(
System.Globalization.CultureInfo.CurrentCulture,
"{0}{1}{2}",
_verb,
StringLiterals.CommandVerbNounSeparator,
_noun));
}
return synopsis.ToString();
}
}
/// <summary>
/// Gets the name of the default parameter set.
/// </summary>
public string DefaultParameterSet
{
get
{
return this.CommandMetadata.DefaultParameterSetName;
}
}
/// <summary>
/// Return the output types specified on the cmdlet.
/// </summary>
public override ReadOnlyCollection<PSTypeName> OutputType
{
get
{
if (_outputType == null)
{
_outputType = new List<PSTypeName>();
if (ImplementingType != null)
{
foreach (object o in ImplementingType.GetCustomAttributes(typeof(OutputTypeAttribute), false))
{
OutputTypeAttribute attr = (OutputTypeAttribute)o;
_outputType.AddRange(attr.Type);
}
}
}
List<PSTypeName> providerTypes = new List<PSTypeName>();
if (Context != null)
{
ProviderInfo provider = null;
if (Arguments != null)
{
// See if we have a path argument - we only consider named arguments -Path and -LiteralPath,
// and only if they are fully specified (no prefixes allowed, so we don't need to deal with
// ambiguities that the parameter binder would resolve for us.
for (int i = 0; i < Arguments.Length - 1; i++)
{
var arg = Arguments[i] as string;
if (arg != null &&
(arg.Equals("-Path", StringComparison.OrdinalIgnoreCase) ||
(arg.Equals("-LiteralPath", StringComparison.OrdinalIgnoreCase))))
{
var path = Arguments[i + 1] as string;
if (path != null)
{
Context.SessionState.Path.GetResolvedProviderPathFromPSPath(path, true, out provider);
}
}
}
}
// If no path argument, just use the current path to choose the provider.
provider ??= Context.SessionState.Path.CurrentLocation.Provider;
provider.GetOutputTypes(Name, providerTypes);
if (providerTypes.Count > 0)
{
providerTypes.InsertRange(0, _outputType);
return new ReadOnlyCollection<PSTypeName>(providerTypes);
}
}
return new ReadOnlyCollection<PSTypeName>(_outputType);
}
}
private List<PSTypeName> _outputType = null;
/// <summary>
/// Gets or sets the scope options for the alias.
/// </summary>
/// <exception cref="System.Management.Automation.SessionStateUnauthorizedAccessException">
/// If the trying to set an cmdlet that is constant or
/// if the value trying to be set is ScopedItemOptions.Constant
/// </exception>
public ScopedItemOptions Options
{
get
{
return _options;
}
set
{
SetOptions(value, false);
}
}
private ScopedItemOptions _options = ScopedItemOptions.None;
/// <summary>
/// Sets the options for the cmdlet and allows changes ReadOnly options only if force is specified.
/// </summary>
/// <param name="newOptions">
/// The new options value.
/// </param>
/// <param name="force">
/// If true the change to the options will happen even if the existing options are read-only.
/// </param>
internal void SetOptions(ScopedItemOptions newOptions, bool force)
{
// Check to see if the cmdlet is readonly, if so
// throw an exception because the options cannot be changed.
if ((_options & ScopedItemOptions.ReadOnly) != 0)
{
SessionStateUnauthorizedAccessException e =
new SessionStateUnauthorizedAccessException(
Name,
SessionStateCategory.Cmdlet,
"CmdletIsReadOnly",
SessionStateStrings.CmdletIsReadOnly);
throw e;
}
_options = newOptions;
}
#endregion public members
#region internal/private members
/// <summary>
/// Gets the full name of the cmdlet including the PSSnapin name.
/// </summary>
private static string GetFullName(string moduleName, string cmdletName)
{
System.Diagnostics.Debug.Assert(cmdletName != null, "cmdletName != null");
string result = cmdletName;
if (!string.IsNullOrEmpty(moduleName))
{
result = moduleName + '\\' + result;
}
return result;
}
/// <summary>
/// Gets the full name of the cmdlet including the PSSnapin name.
/// </summary>
private static string GetFullName(CmdletInfo cmdletInfo)
{
return GetFullName(cmdletInfo.ModuleName, cmdletInfo.Name);
}
/// <summary>
/// Gets the full name of the cmdlet including the PSSnapin name.
/// </summary>
internal static string GetFullName(PSObject psObject)
{
// If this is a high-fidelity object then extract full-name normally.
if (psObject.BaseObject is CmdletInfo)
{
CmdletInfo cmdletInfo = (CmdletInfo)psObject.BaseObject;
return GetFullName(cmdletInfo);
}
// Otherwise, it is a PSCustomObject shredded in a remote call: extract name as a property.
else
{
// Handle the case in one or both of the properties might not be defined.
PSPropertyInfo nameProperty = psObject.Properties["Name"];
PSPropertyInfo psSnapInProperty = psObject.Properties["PSSnapIn"];
string nameString = nameProperty == null ? string.Empty : (string)nameProperty.Value;
string psSnapInString = psSnapInProperty == null ? string.Empty : (string)psSnapInProperty.Value;
return GetFullName(psSnapInString, nameString);
}
}
/// <summary>
/// Gets the full name of the cmdlet including the PSSnapin name.
/// </summary>
internal string FullName
{
get
{
return GetFullName(this);
}
}
/// <summary>
/// Gets the CommandMetadata for this cmdlet.
/// </summary>
/// <exception cref="ArgumentException">
/// The type name is invalid or the length of the type name
/// exceeds 1024 characters.
/// </exception>
/// <exception cref="System.Security.SecurityException">
/// The caller does not have the required permission to load the assembly
/// or create the type.
/// </exception>
/// <exception cref="ParsingMetadataException">
/// If more than int.MaxValue parameter-sets are defined for the command.
/// </exception>
/// <exception cref="MetadataException">
/// If a parameter defines the same parameter-set name multiple times.
/// If the attributes could not be read from a property or field.
/// </exception>
internal override CommandMetadata CommandMetadata
{
get
{
return _cmdletMetadata ??= CommandMetadata.Get(this.Name, this.ImplementingType, Context);
}
}
private CommandMetadata _cmdletMetadata;
internal override bool ImplementsDynamicParameters
{
get
{
if (ImplementingType != null)
{
return (ImplementingType.GetInterface(nameof(IDynamicParameters), true) != null);
}
else
{
return false;
}
}
}
#endregion internal/private members
}
}
|