Change CimCmdlets to use AliasAttribute (#12617)

This commit is contained in:
Thomas Larsen
2020-05-22 12:56:47 -07:00
committed by GitHub
parent 122bd752fa
commit 62bb436e71
14 changed files with 37 additions and 122 deletions
@@ -1,118 +0,0 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#region Using directives
using System;
using System.Globalization;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
#endregion
namespace Microsoft.Management.Infrastructure.CimCmdlets
{
/// <summary>
/// <para>
/// Initialize the cimcmdlets.
/// </para>
/// </summary>
/// <summary>
/// Provide a hook to the engine for startup initialization
/// w.r.t compiled assembly loading.
/// </summary>
public sealed class CimCmdletsAssemblyInitializer : IModuleAssemblyInitializer
{
/// <summary>
/// <para>
/// The constructor.
/// </para>
/// </summary>
public CimCmdletsAssemblyInitializer()
{
}
/// <summary>
/// PowerShell engine will call this method when the cimcmdlets module
/// is loaded.
/// </summary>
public void OnImport()
{
DebugHelper.WriteLogEx();
using (System.Management.Automation.PowerShell invoker = System.Management.Automation.PowerShell.Create(RunspaceMode.CurrentRunspace))
{
foreach (CimCmdletAliasEntry alias in Aliases)
{
invoker.AddScript(string.Format(CultureInfo.CurrentUICulture, "Set-Alias -Name {0} -Value {1} -Option {2} -ErrorAction SilentlyContinue", alias.Name, alias.Value, alias.Options));
DebugHelper.WriteLog(@"Add commands {0} of {1} with option {2} to current runspace.", 1, alias.Name, alias.Value, alias.Options);
}
System.Collections.ObjectModel.Collection<PSObject> psObjects = invoker.Invoke();
DebugHelper.WriteLog(@"Invoke results {0}.", 1, psObjects.Count);
}
}
#region readonly string
/// <summary>
/// <para>
/// CimCmdlet alias entry.
/// </para>
/// </summary>
internal sealed class CimCmdletAliasEntry
{
/// <summary>
/// <para>
/// The constructor.
/// </para>
/// </summary>
/// <param name="name">The entry name.</param>
/// <param name="value">The entry value.</param>
internal CimCmdletAliasEntry(string name, string value)
{
this._name = name;
this._value = value;
}
/// <summary>
/// The string defining the name of this alias.
/// </summary>
internal string Name { get { return this._name; } }
private string _name;
/// <summary>
/// The string defining real cmdlet name.
/// </summary>
internal string Value { get { return this._value; } }
private string _value = string.Empty;
/// <summary>
/// The string defining real cmdlet name.
/// </summary>
internal ScopedItemOptions Options { get { return this._options; } }
private ScopedItemOptions _options = ScopedItemOptions.AllScope | ScopedItemOptions.ReadOnly;
}
/// <summary>
/// Returns a new array of alias entries everytime it's called.
/// </summary>
internal static CimCmdletAliasEntry[] Aliases = new CimCmdletAliasEntry[] {
new CimCmdletAliasEntry("gcim", "Get-CimInstance"),
new CimCmdletAliasEntry("scim", "Set-CimInstance"),
new CimCmdletAliasEntry("ncim", "New-CimInstance "),
new CimCmdletAliasEntry("rcim", "Remove-cimInstance"),
new CimCmdletAliasEntry("icim", "Invoke-CimMethod"),
new CimCmdletAliasEntry("gcai", "Get-CimAssociatedInstance"),
new CimCmdletAliasEntry("rcie", "Register-CimIndicationEvent"),
new CimCmdletAliasEntry("ncms", "New-CimSession"),
new CimCmdletAliasEntry("rcms", "Remove-cimSession"),
new CimCmdletAliasEntry("gcms", "Get-CimSession"),
new CimCmdletAliasEntry("ncso", "New-CimSessionOption"),
new CimCmdletAliasEntry("gcls", "Get-CimClass"),
};
#endregion
}
}
@@ -22,6 +22,7 @@ namespace Microsoft.Management.Infrastructure.CimCmdlets
/// Association parameter.
/// </para>
/// </summary>
[Alias("gcai")]
[Cmdlet(VerbsCommon.Get,
GetCimAssociatedInstanceCommand.Noun,
DefaultParameterSetName = CimBaseCommand.ComputerSetName,
@@ -23,6 +23,7 @@ namespace Microsoft.Management.Infrastructure.CimCmdlets
/// Should the class remember what Session it came from? No.
/// </para>
/// </summary>
[Alias("gcls")]
[Cmdlet(VerbsCommon.Get, GetCimClassCommand.Noun, DefaultParameterSetName = ComputerSetName, HelpUri = "https://go.microsoft.com/fwlink/?LinkId=227959")]
[OutputType(typeof(CimClass))]
public class GetCimClassCommand : CimBaseCommand
@@ -18,6 +18,7 @@ namespace Microsoft.Management.Infrastructure.CimCmdlets
/// specified in the Property parameter, KeysOnly parameter or the Select clause
/// of the Query parameter.
/// </summary>
[Alias("gcim")]
[Cmdlet(VerbsCommon.Get, "CimInstance", DefaultParameterSetName = CimBaseCommand.ClassNameComputerSet, HelpUri = "https://go.microsoft.com/fwlink/?LinkId=227961")]
[OutputType(typeof(CimInstance))]
public class GetCimInstanceCommand : CimBaseCommand
@@ -14,7 +14,7 @@ namespace Microsoft.Management.Infrastructure.CimCmdlets
/// The command returns zero, one or more CimSession objects that represent
/// connections with remote computers established from the current PS Session.
/// </summary>
[Alias("gcms")]
[Cmdlet(VerbsCommon.Get, "CimSession", DefaultParameterSetName = ComputerNameSet, HelpUri = "https://go.microsoft.com/fwlink/?LinkId=227966")]
[OutputType(typeof(CimSession))]
public sealed class GetCimSessionCommand : CimBaseCommand
@@ -15,7 +15,7 @@ namespace Microsoft.Management.Infrastructure.CimCmdlets
/// This cmdlet enables the user to invoke a static method on a CIM class using
/// the arguments passed as a list of name value pair dictionary.
/// </summary>
[Alias("icim")]
[Cmdlet(
"Invoke",
"CimMethod",
@@ -21,6 +21,7 @@ namespace Microsoft.Management.Infrastructure.CimCmdlets
/// on the server, otherwise just create client in-memory instance
/// </para>
/// </summary>
[Alias("ncim")]
[Cmdlet(VerbsCommon.New, "CimInstance", DefaultParameterSetName = CimBaseCommand.ClassNameComputerSet, SupportsShouldProcess = true, HelpUri = "https://go.microsoft.com/fwlink/?LinkId=227963")]
[OutputType(typeof(CimInstance))]
public class NewCimInstanceCommand : CimBaseCommand
@@ -18,6 +18,7 @@ namespace Microsoft.Management.Infrastructure.CimCmdlets
/// The CimSession object returned by the Cmdlet is used by all other CIM
/// cmdlets.
/// </summary>
[Alias("ncms")]
[Cmdlet(VerbsCommon.New, "CimSession", DefaultParameterSetName = CredentialParameterSet, HelpUri = "https://go.microsoft.com/fwlink/?LinkId=227967")]
[OutputType(typeof(CimSession))]
public sealed class NewCimSessionCommand : CimBaseCommand
@@ -35,6 +35,7 @@ namespace Microsoft.Management.Infrastructure.CimCmdlets
/// DComSessionOptions or WSManSessionOptions, which derive from
/// CimSessionOptions.
/// </summary>
[Alias("ncso")]
[Cmdlet(VerbsCommon.New, "CimSessionOption", DefaultParameterSetName = ProtocolNameParameterSet, HelpUri = "https://go.microsoft.com/fwlink/?LinkId=227969")]
[OutputType(typeof(CimSessionOptions))]
public sealed class NewCimSessionOptionCommand : CimBaseCommand
@@ -19,6 +19,7 @@ namespace Microsoft.Management.Infrastructure.CimCmdlets
/// cancel the subscription
/// Should we have the second parameter set with a -Query?
/// </summary>
[Alias("rcie")]
[Cmdlet(VerbsLifecycle.Register, "CimIndicationEvent", DefaultParameterSetName = CimBaseCommand.ClassNameComputerSet, HelpUri = "https://go.microsoft.com/fwlink/?LinkId=227960")]
public class RegisterCimIndicationCommand : ObjectEventRegistrationBase
{
@@ -15,6 +15,7 @@ namespace Microsoft.Management.Infrastructure.CimCmdlets
/// <summary>
/// Enables the user to remove a CimInstance.
/// </summary>
[Alias("rcim")]
[Cmdlet(
VerbsCommon.Remove,
"CimInstance",
@@ -19,7 +19,7 @@ namespace Microsoft.Management.Infrastructure.CimCmdlets
/// <summary>
/// This Cmdlet allows the to remove, or terminate, one or more CimSession(s).
/// </summary>
[Alias("rcms")]
[Cmdlet(VerbsCommon.Remove, "CimSession",
SupportsShouldProcess = true,
DefaultParameterSetName = CimSessionSet,
@@ -17,6 +17,7 @@ namespace Microsoft.Management.Infrastructure.CimCmdlets
/// CimInstance must have values of all [KEY] properties.
/// </para>
/// </summary>
[Alias("scim")]
[Cmdlet(
VerbsCommon.Set,
"CimInstance",
@@ -68,9 +68,13 @@ Describe "Verify approved aliases list" -Tags "CI" {
"Alias", "gal", "Get-Alias", $($FullCLR -or $CoreWindows -or $CoreUnix), "ReadOnly", "", ""
"Alias", "gbp", "Get-PSBreakpoint", $($FullCLR -or $CoreWindows -or $CoreUnix), "ReadOnly", "", ""
"Alias", "gc", "Get-Content", $($FullCLR -or $CoreWindows -or $CoreUnix), "ReadOnly", "", ""
"Alias", "gcai", "Get-CimAssociatedInstance", $($FullCLR -or $CoreWindows ), "", "", ""
"Alias", "gcb", "Get-Clipboard", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", ""
"Alias", "gci", "Get-ChildItem", $($FullCLR -or $CoreWindows -or $CoreUnix), "ReadOnly", "", ""
"Alias", "gcim", "Get-CimInstance", $($FullCLR -or $CoreWindows ), "", "", ""
"Alias", "gcls", "Get-CimClass", $($FullCLR -or $CoreWindows ), "", "", ""
"Alias", "gcm", "Get-Command", $($FullCLR -or $CoreWindows -or $CoreUnix), "ReadOnly", "", ""
"Alias", "gcms", "Get-CimSession", $($FullCLR -or $CoreWindows ), "", "", ""
"Alias", "gcs", "Get-PSCallStack", $($FullCLR -or $CoreWindows -or $CoreUnix), "ReadOnly", "", ""
"Alias", "gdr", "Get-PSDrive", $($FullCLR -or $CoreWindows -or $CoreUnix), "ReadOnly", "", ""
"Alias", "gerr", "Get-Error", $( $CoreWindows -or $CoreUnix), "ReadOnly", "", ""
@@ -94,6 +98,7 @@ Describe "Verify approved aliases list" -Tags "CI" {
"Alias", "gwmi", "Get-WmiObject", $($FullCLR ), "ReadOnly", "", ""
"Alias", "h", "Get-History", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", ""
"Alias", "history", "Get-History", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", ""
"Alias", "icim", "Invoke-CimMethod", $($FullCLR -or $CoreWindows ), "", "", ""
"Alias", "icm", "Invoke-Command", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", ""
"Alias", "iex", "Invoke-Expression", $($FullCLR -or $CoreWindows -or $CoreUnix), "ReadOnly", "", ""
"Alias", "ihy", "Invoke-History", $($FullCLR -or $CoreWindows -or $CoreUnix), "ReadOnly", "", ""
@@ -118,6 +123,9 @@ Describe "Verify approved aliases list" -Tags "CI" {
"Alias", "mp", "Move-ItemProperty", $($FullCLR -or $CoreWindows -or $CoreUnix), "ReadOnly", "", ""
"Alias", "mv", "Move-Item", $($FullCLR -or $CoreWindows ), "", "", ""
"Alias", "nal", "New-Alias", $($FullCLR -or $CoreWindows -or $CoreUnix), "ReadOnly", "", ""
"Alias", "ncim", "New-CimInstance", $($FullCLR -or $CoreWindows ), "", "", ""
"Alias", "ncms", "New-CimSession", $($FullCLR -or $CoreWindows ), "", "", ""
"Alias", "ncso", "New-CimSessionOption", $($FullCLR -or $CoreWindows ), "", "", ""
"Alias", "ndr", "New-PSDrive", $($FullCLR -or $CoreWindows -or $CoreUnix), "ReadOnly", "", ""
"Alias", "ni", "New-Item", $($FullCLR -or $CoreWindows -or $CoreUnix), "ReadOnly", "", ""
"Alias", "nmo", "New-Module", $($FullCLR -or $CoreWindows -or $CoreUnix), "ReadOnly", "", ""
@@ -133,7 +141,10 @@ Describe "Verify approved aliases list" -Tags "CI" {
"Alias", "pwd", "Get-Location", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", ""
"Alias", "r", "Invoke-History", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", ""
"Alias", "rbp", "Remove-PSBreakpoint", $($FullCLR -or $CoreWindows -or $CoreUnix), "ReadOnly", "", ""
"Alias", "rcie", "Register-CimIndicationEvent", $($FullCLR -or $CoreWindows ), "", "", ""
"Alias", "rcim", "Remove-CimInstance", $($FullCLR -or $CoreWindows ), "", "", ""
"Alias", "rcjb", "Receive-Job", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", ""
"Alias", "rcms", "Remove-CimSession", $($FullCLR -or $CoreWindows ), "", "", ""
"Alias", "rcsn", "Receive-PSSession", $($FullCLR -or $CoreWindows -or $CoreUnix), "ReadOnly", "", ""
"Alias", "rd", "Remove-Item", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", ""
"Alias", "rdr", "Remove-PSDrive", $($FullCLR -or $CoreWindows -or $CoreUnix), "ReadOnly", "", ""
@@ -159,6 +170,7 @@ Describe "Verify approved aliases list" -Tags "CI" {
"Alias", "sbp", "Set-PSBreakpoint", $($FullCLR -or $CoreWindows -or $CoreUnix), "ReadOnly", "", ""
"Alias", "sc", "Set-Content", $($FullCLR ), "ReadOnly", "", ""
"Alias", "scb", "Set-Clipboard", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", ""
"Alias", "scim", "Set-CimInstance", $($FullCLR -or $CoreWindows ), "", "", ""
"Alias", "select", "Select-Object", $($FullCLR -or $CoreWindows -or $CoreUnix), "ReadOnly", "AllScope", ""
"Alias", "set", "Set-Variable", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", ""
"Alias", "shcm", "Show-Command", $($FullCLR -or $CoreWindows ), "ReadOnly", "", ""
@@ -259,6 +271,10 @@ Describe "Verify approved aliases list" -Tags "CI" {
"Cmdlet", "Get-Alias", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None"
"Cmdlet", "Get-AuthenticodeSignature", "", $($FullCLR -or $CoreWindows ), "", "", "None"
"Cmdlet", "Get-ChildItem", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None"
"Cmdlet", "Get-CimAssociatedInstance", "", $($FullCLR -or $CoreWindows ), "", "", "None"
"Cmdlet", "Get-CimClass", "", $($FullCLR -or $CoreWindows ), "", "", "None"
"Cmdlet", "Get-CimInstance", "", $($FullCLR -or $CoreWindows ), "", "", "None"
"Cmdlet", "Get-CimSession", "", $($FullCLR -or $CoreWindows ), "", "", "None"
"Cmdlet", "Get-Clipboard", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None"
"Cmdlet", "Get-CmsMessage", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None"
"Cmdlet", "Get-Command", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None"
@@ -327,6 +343,7 @@ Describe "Verify approved aliases list" -Tags "CI" {
"Cmdlet", "Import-Module", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None"
"Cmdlet", "Import-PowerShellDataFile", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None"
"Cmdlet", "Import-PSSession", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None"
"Cmdlet", "Invoke-CimMethod", "", $($FullCLR -or $CoreWindows ), "", "", "Medium"
"Cmdlet", "Invoke-Command", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None"
"Cmdlet", "Invoke-Expression", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None"
"Cmdlet", "Invoke-History", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "Medium"
@@ -343,6 +360,9 @@ Describe "Verify approved aliases list" -Tags "CI" {
"Cmdlet", "Move-Item", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "Medium"
"Cmdlet", "Move-ItemProperty", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "Medium"
"Cmdlet", "New-Alias", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "Low"
"Cmdlet", "New-CimInstance", "", $($FullCLR -or $CoreWindows ), "", "", "Medium"
"Cmdlet", "New-CimSession", "", $($FullCLR -or $CoreWindows ), "", "", "None"
"Cmdlet", "New-CimSessionOption", "", $($FullCLR -or $CoreWindows ), "", "", "None"
"Cmdlet", "New-Event", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None"
"Cmdlet", "New-EventLog", "", $($FullCLR ), "", "", ""
"Cmdlet", "New-FileCatalog", "", $($FullCLR -or $CoreWindows ), "", "", "Medium"
@@ -381,11 +401,14 @@ Describe "Verify approved aliases list" -Tags "CI" {
"Cmdlet", "Receive-Job", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None"
"Cmdlet", "Receive-PSSession", "", $($FullCLR -or $CoreWindows ), "", "", "Low"
"Cmdlet", "Register-ArgumentCompleter", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None"
"Cmdlet", "Register-CimIndicationEvent", "", $($FullCLR -or $CoreWindows ), "", "", "None"
"Cmdlet", "Register-EngineEvent", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None"
"Cmdlet", "Register-ObjectEvent", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "None"
"Cmdlet", "Register-PSSessionConfiguration", "", $($FullCLR -or $CoreWindows ), "", "", "Medium"
"Cmdlet", "Register-WmiEvent", "", $($FullCLR ), "", "", ""
"Cmdlet", "Remove-Alias", "", $( $CoreWindows -or $CoreUnix), "", "", "None"
"Cmdlet", "Remove-CimInstance", "", $($FullCLR -or $CoreWindows ), "", "", "Medium"
"Cmdlet", "Remove-CimSession", "", $($FullCLR -or $CoreWindows ), "", "", "Medium"
"Cmdlet", "Remove-Computer", "", $($FullCLR ), "", "", ""
"Cmdlet", "Remove-Event", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "Medium"
"Cmdlet", "Remove-EventLog", "", $($FullCLR ), "", "", ""
@@ -420,6 +443,7 @@ Describe "Verify approved aliases list" -Tags "CI" {
"Cmdlet", "Set-Acl", "", $($FullCLR -or $CoreWindows ), "", "", "Medium"
"Cmdlet", "Set-Alias", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "Medium"
"Cmdlet", "Set-AuthenticodeSignature", "", $($FullCLR -or $CoreWindows ), "", "", "Medium"
"Cmdlet", "Set-CimInstance", "", $($FullCLR -or $CoreWindows ), "", "", "Medium"
"Cmdlet", "Set-Clipboard", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "Medium"
"Cmdlet", "Set-Content", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "Medium"
"Cmdlet", "Set-Date", "", $($FullCLR -or $CoreWindows -or $CoreUnix), "", "", "Medium"
@@ -496,7 +520,7 @@ Describe "Verify approved aliases list" -Tags "CI" {
# We control only default engine aliases (Source -eq "") and aliases from following default loaded modules
# We control only default engine Cmdlets (Source -eq "") and Cmdlets from following default loaded modules
$moduleList = @("Microsoft.PowerShell.Utility", "Microsoft.PowerShell.Management", "Microsoft.PowerShell.Security", "Microsoft.PowerShell.Host", "Microsoft.PowerShell.Diagnostics", "Microsoft.WSMan.Management", "Microsoft.PowerShell.Core")
$moduleList = @("Microsoft.PowerShell.Utility", "Microsoft.PowerShell.Management", "Microsoft.PowerShell.Security", "Microsoft.PowerShell.Host", "Microsoft.PowerShell.Diagnostics", "Microsoft.WSMan.Management", "Microsoft.PowerShell.Core", "CimCmdlets")
$getAliases = {
param($moduleList)