// 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 { /// /// /// Initialize the cimcmdlets. /// /// /// /// Provide a hook to the engine for startup initialization /// w.r.t compiled assembly loading. /// public sealed class CimCmdletsAssemblyInitializer : IModuleAssemblyInitializer { /// /// /// constructor /// /// public CimCmdletsAssemblyInitializer() { } /// /// PowerShell engine will call this method when the cimcmdlets module /// is loaded. /// 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 psObjects = invoker.Invoke(); DebugHelper.WriteLog(@"Invoke results {0}.", 1, psObjects.Count); } } #region readonly string /// /// /// CimCmdlet alias entry /// /// internal sealed class CimCmdletAliasEntry { /// /// /// Constructor /// /// /// /// internal CimCmdletAliasEntry(string name, string value) { this._name = name; this._value = value; } /// /// The string defining the name of this alias. /// internal string Name { get { return this._name; } } private string _name; /// /// The string defining real cmdlet name. /// internal string Value { get { return this._value; } } private string _value = string.Empty; /// /// The string defining real cmdlet name. /// internal ScopedItemOptions Options { get { return this._options; } } private ScopedItemOptions _options = ScopedItemOptions.AllScope | ScopedItemOptions.ReadOnly; } /// /// Returns a new array of alias entries everytime it's called. /// 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 } }