MegaBites-AI/Windows-powershell
0372
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System;5using System.Collections.Generic;6using System.Diagnostics.CodeAnalysis;7using System.Management.Automation;8using System.Management.Automation.Internal;9using System.Management.Automation.Security;10 11//12// Now define the set of commands for manipulating modules.13//14 15namespace Microsoft.PowerShell.Commands16{17 #region Export-ModuleMember18 /// <summary>19 /// Implements a cmdlet that loads a module.20 /// </summary>21 [Cmdlet(VerbsData.Export, "ModuleMember", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=2096578")]22 public sealed class ExportModuleMemberCommand : PSCmdlet23 {24 /// <summary>25 /// This parameter specifies the functions to import from the module...26 /// </summary>27 [Parameter(ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0)]28 [AllowEmptyCollection]29 [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays", Justification = "Cmdlets use arrays for parameters.")]30 public string[] Function31 {32 get33 {34 return _functionList;35 }36 37 set38 {39 _functionList = value;40 // Create the list of patterns to match at parameter bind time41 // so errors will be reported before loading the module...42 _functionPatterns = new List<WildcardPattern>();43 if (_functionList != null)44 {45 foreach (string pattern in _functionList)46 {47 _functionPatterns.Add(WildcardPattern.Get(pattern, WildcardOptions.IgnoreCase));48 }49 }50 }51 }52 53 private string[] _functionList;54 private List<WildcardPattern> _functionPatterns;55 56 /// <summary>57 /// This parameter specifies the functions to import from the module...58 /// </summary>59 [Parameter(ValueFromPipelineByPropertyName = true)]60 [AllowEmptyCollection]61 [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays", Justification = "Cmdlets use arrays for parameters.")]62 public string[] Cmdlet63 {64 get65 {66 return _cmdletList;67 }68 69 set70 {71 _cmdletList = value;72 // Create the list of patterns to match at parameter bind time73 // so errors will be reported before loading the module...74 _cmdletPatterns = new List<WildcardPattern>();75 if (_cmdletList != null)76 {77 foreach (string pattern in _cmdletList)78 {79 _cmdletPatterns.Add(WildcardPattern.Get(pattern, WildcardOptions.IgnoreCase));80 }81 }82 }83 }84 85 private string[] _cmdletList;86 private List<WildcardPattern> _cmdletPatterns;87 88 /// <summary>89 /// This parameter specifies the variables to import from the module...90 /// </summary>91 [Parameter(ValueFromPipelineByPropertyName = true)]92 [ValidateNotNull]93 [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays", Justification = "Cmdlets use arrays for parameters.")]94 public string[] Variable95 {96 get97 {98 return _variableExportList;99 }100 101 set102 {103 _variableExportList = value;104 // Create the list of patterns to match at parameter bind time105 // so errors will be reported before loading the module...106 _variablePatterns = new List<WildcardPattern>();107 if (_variableExportList != null)108 {109 foreach (string pattern in _variableExportList)110 {111 _variablePatterns.Add(WildcardPattern.Get(pattern, WildcardOptions.IgnoreCase));112 }113 }114 }115 }116 117 private string[] _variableExportList;118 private List<WildcardPattern> _variablePatterns;119 120 /// <summary>121 /// This parameter specifies the aliases to import from the module...122 /// </summary>123 [Parameter(ValueFromPipelineByPropertyName = true)]124 [ValidateNotNull]125 [SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays", Justification = "Cmdlets use arrays for parameters.")]126 public string[] Alias127 {128 get129 {130 return _aliasExportList;131 }132 133 set134 {135 _aliasExportList = value;136 // Create the list of patterns to match at parameter bind time137 // so errors will be reported before loading the module...138 _aliasPatterns = new List<WildcardPattern>();139 if (_aliasExportList != null)140 {141 foreach (string pattern in _aliasExportList)142 {143 _aliasPatterns.Add(WildcardPattern.Get(pattern, WildcardOptions.IgnoreCase));144 }145 }146 }147 }148 149 private string[] _aliasExportList;150 private List<WildcardPattern> _aliasPatterns;151 152 /// <summary>153 /// Export the specified functions...154 /// </summary>155 protected override void ProcessRecord()156 {157 if (Context.EngineSessionState == Context.TopLevelSessionState)158 {159 string message = StringUtil.Format(Modules.CanOnlyBeUsedFromWithinAModule);160 InvalidOperationException invalidOp = new InvalidOperationException(message);161 ErrorRecord er = new ErrorRecord(invalidOp, "Modules_CanOnlyExecuteExportModuleMemberInsideAModule",162 ErrorCategory.PermissionDenied, null);163 ThrowTerminatingError(er);164 }165 166 // Prevent script injection attack by disallowing ExportModuleMemberCommand to export module members across167 // language boundaries. This will prevent injected untrusted script from exporting private trusted module functions.168 if (Context.EngineSessionState.Module?.LanguageMode != null &&169 Context.LanguageMode != Context.EngineSessionState.Module.LanguageMode)170 {171 if (SystemPolicy.GetSystemLockdownPolicy() != SystemEnforcementMode.Audit)172 {173 var se = new PSSecurityException(Modules.CannotExportMembersAccrossLanguageBoundaries);174 var er = new ErrorRecord(se, "Modules_CannotExportMembersAccrossLanguageBoundaries", ErrorCategory.SecurityError, this);175 ThrowTerminatingError(er);176 }177 178 SystemPolicy.LogWDACAuditMessage(179 context: Context,180 title: Modules.WDACExportModuleCommandLogTitle,181 message: StringUtil.Format(Modules.WDACExportModuleCommandLogMessage, Context.EngineSessionState.Module.Name, Context.EngineSessionState.Module.LanguageMode, Context.LanguageMode),182 fqid: "ExportModuleMemberCmdletNotAllowed",183 dropIntoDebugger: true);184 }185 186 ModuleIntrinsics.ExportModuleMembers(this,187 this.Context.EngineSessionState,188 _functionPatterns, _cmdletPatterns, _aliasPatterns, _variablePatterns, null);189 }190 }191 #endregion Export-ModuleMember192}193 