/// <summary> /// Export the specified functions... /// </summary> protected override void ProcessRecord() { if (Context.EngineSessionState == Context.TopLevelSessionState) { string message = StringUtil.Format(Modules.CanOnlyBeUsedFromWithinAModule); InvalidOperationException invalidOp = new InvalidOperationException(message); ErrorRecord er = new ErrorRecord(invalidOp, "Modules_CanOnlyExecuteExportModuleMemberInsideAModule", ErrorCategory.PermissionDenied, null); ThrowTerminatingError(er); } // Prevent script injection attack by disallowing ExportModuleMemberCommand to export module members across // language boundaries. This will prevent injected untrusted script from exporting private trusted module functions. if (Context.EngineSessionState.Module?.LanguageMode != null && Context.LanguageMode != Context.EngineSessionState.Module.LanguageMode) { var se = new PSSecurityException(Modules.CannotExportMembersAccrossLanguageBoundaries); var er = new ErrorRecord(se, "Modules_CannotExportMembersAccrossLanguageBoundaries", ErrorCategory.SecurityError, this); ThrowTerminatingError(er); } ModuleIntrinsics.ExportModuleMembers(this, this.Context.EngineSessionState, _functionPatterns, _cmdletPatterns, _aliasPatterns, _variablePatterns, null); }
protected override void ProcessRecord() { if (base.Context.EngineSessionState == base.Context.TopLevelSessionState) { InvalidOperationException exception = new InvalidOperationException(StringUtil.Format(Modules.CanOnlyBeUsedFromWithinAModule, new object[0])); ErrorRecord errorRecord = new ErrorRecord(exception, "Modules_CanOnlyExecuteExportModuleMemberInsideAModule", ErrorCategory.PermissionDenied, null); base.ThrowTerminatingError(errorRecord); } ModuleIntrinsics.ExportModuleMembers(this, base.Context.EngineSessionState, this._functionPatterns, this._cmdletPatterns, this._aliasPatterns, this._variablePatterns, null); }
protected override void EndProcessing() { if (this._scriptBlock != null) { string path = Guid.NewGuid().ToString(); if (string.IsNullOrEmpty(this._name)) { this._name = "__DynamicModule_" + path; } try { base.Context.Modules.IncrementModuleNestingDepth(this, this._name); ArrayList results = null; PSModuleInfo sourceModule = null; try { sourceModule = base.Context.Modules.CreateModule(this._name, path, this._scriptBlock, null, out results, this._arguments); if (!sourceModule.SessionState.Internal.UseExportList) { List <WildcardPattern> cmdletPatterns = (base.BaseCmdletPatterns != null) ? base.BaseCmdletPatterns : base.MatchAll; List <WildcardPattern> functionPatterns = (base.BaseFunctionPatterns != null) ? base.BaseFunctionPatterns : base.MatchAll; ModuleIntrinsics.ExportModuleMembers(this, sourceModule.SessionState.Internal, functionPatterns, cmdletPatterns, base.BaseAliasPatterns, base.BaseVariablePatterns, null); } } catch (RuntimeException exception) { exception.ErrorRecord.PreserveInvocationInfoOnce = true; base.WriteError(exception.ErrorRecord); } if (sourceModule != null) { if (this._returnResult) { base.ImportModuleMembers(sourceModule, string.Empty); base.WriteObject(results, true); } else if (this._asCustomObject) { base.WriteObject(sourceModule.AsCustomObject()); } else { base.ImportModuleMembers(sourceModule, string.Empty); base.WriteObject(sourceModule); } } } finally { base.Context.Modules.DecrementModuleNestingCount(); } } }
/// <summary> /// Export the specified functions... /// </summary> protected override void ProcessRecord() { if (Context.EngineSessionState == Context.TopLevelSessionState) { string message = StringUtil.Format(Modules.CanOnlyBeUsedFromWithinAModule); InvalidOperationException invalidOp = new InvalidOperationException(message); ErrorRecord er = new ErrorRecord(invalidOp, "Modules_CanOnlyExecuteExportModuleMemberInsideAModule", ErrorCategory.PermissionDenied, null); ThrowTerminatingError(er); } ModuleIntrinsics.ExportModuleMembers(this, this.Context.EngineSessionState, _functionPatterns, _cmdletPatterns, _aliasPatterns, _variablePatterns, null); }
/// <summary> /// Create the new module... /// </summary> protected override void EndProcessing() { // Create a module from a scriptblock... if (_scriptBlock != null) { // Check ScriptBlock language mode. If it is different than the context language mode // then throw error since private trusted script functions may be exposed. if (Context.LanguageMode == PSLanguageMode.ConstrainedLanguage && _scriptBlock.LanguageMode == PSLanguageMode.FullLanguage) { this.ThrowTerminatingError( new ErrorRecord( new PSSecurityException(Modules.CannotCreateModuleWithScriptBlock), "Modules_CannotCreateModuleWithFullLanguageScriptBlock", ErrorCategory.SecurityError, null)); } string gs = System.Guid.NewGuid().ToString(); if (string.IsNullOrEmpty(_name)) { _name = PSModuleInfo.DynamicModulePrefixString + gs; } try { Context.Modules.IncrementModuleNestingDepth(this, _name); List <object> results = null; PSModuleInfo localModule = null; try { // The path for a "dynamic" module will be a GUID so it's unique. localModule = Context.Modules.CreateModule(_name, gs, _scriptBlock, null, out results, _arguments); // Export all functions and variables if no exports were specified... if (!localModule.SessionState.Internal.UseExportList) { List <WildcardPattern> cmdletPatterns = BaseCmdletPatterns ?? MatchAll; List <WildcardPattern> functionPatterns = BaseFunctionPatterns ?? MatchAll; ModuleIntrinsics.ExportModuleMembers(this, localModule.SessionState.Internal, functionPatterns, cmdletPatterns, BaseAliasPatterns, BaseVariablePatterns, null); } } catch (RuntimeException e) { // Preserve the inner module invocation info... e.ErrorRecord.PreserveInvocationInfoOnce = true; WriteError(e.ErrorRecord); } // If the module was created successfully, then process the result... if (localModule != null) { if (_returnResult) { // import the specified members... ImportModuleMembers(localModule, string.Empty /* no -Prefix for New-Module cmdlet */); WriteObject(results, true); } else if (_asCustomObject) { WriteObject(localModule.AsCustomObject()); } else { // import the specified members... ImportModuleMembers(localModule, string.Empty /* no -Prefix for New-Module cmdlet */); WriteObject(localModule); } } } finally { Context.Modules.DecrementModuleNestingCount(); } return; } }
/// <summary> /// Create the new module... /// </summary> protected override void EndProcessing() { // Create a module from a scriptblock... if (_scriptBlock != null) { string gs = System.Guid.NewGuid().ToString(); if (String.IsNullOrEmpty(_name)) { _name = PSModuleInfo.DynamicModulePrefixString + gs; } try { Context.Modules.IncrementModuleNestingDepth(this, _name); List <object> results = null; PSModuleInfo localModule = null; try { // The path for a "dynamic" module will be a GUID so it's unique. localModule = Context.Modules.CreateModule(_name, gs, _scriptBlock, null, out results, _arguments); // Export all functions and variables if no exports were specified... if (!localModule.SessionState.Internal.UseExportList) { List <WildcardPattern> cmdletPatterns = BaseCmdletPatterns ?? MatchAll; List <WildcardPattern> functionPatterns = BaseFunctionPatterns ?? MatchAll; ModuleIntrinsics.ExportModuleMembers(this, localModule.SessionState.Internal, functionPatterns, cmdletPatterns, BaseAliasPatterns, BaseVariablePatterns, null); } } catch (RuntimeException e) { // Preserve the inner module invocation info... e.ErrorRecord.PreserveInvocationInfoOnce = true; WriteError(e.ErrorRecord); } // If the module was created successfully, then process the result... if (localModule != null) { if (_returnResult) { // import the specified members... ImportModuleMembers(localModule, string.Empty /* no -Prefix for New-Module cmdlet */); WriteObject(results, true); } else if (_asCustomObject) { WriteObject(localModule.AsCustomObject()); } else { // import the specified members... ImportModuleMembers(localModule, string.Empty /* no -Prefix for New-Module cmdlet */); WriteObject(localModule); } } } finally { Context.Modules.DecrementModuleNestingCount(); } return; } }