コード例 #1
0
ファイル: ImportModuleCommand.cs プロジェクト: 40a/PowerShell
        private void ImportModule_ViaLocalModuleInfo(ImportModuleOptions importModuleOptions, PSModuleInfo module)
        {
            try
            {
                PSModuleInfo alreadyLoadedModule = null;
                Context.Modules.ModuleTable.TryGetValue(module.Path, out alreadyLoadedModule);
                if (!BaseForce && IsModuleAlreadyLoaded(alreadyLoadedModule))
                {
                    AddModuleToModuleTables(this.Context, this.TargetSessionState.Internal, alreadyLoadedModule);

                    // Even if the module has been loaded, import the specified members...
                    ImportModuleMembers(alreadyLoadedModule, this.BasePrefix, importModuleOptions);

                    if (BaseAsCustomObject)
                    {
                        if (alreadyLoadedModule.ModuleType != ModuleType.Script)
                        {
                            string message = StringUtil.Format(Modules.CantUseAsCustomObjectWithBinaryModule, alreadyLoadedModule.Path);
                            InvalidOperationException invalidOp = new InvalidOperationException(message);
                            ErrorRecord er = new ErrorRecord(invalidOp, "Modules_CantUseAsCustomObjectWithBinaryModule",
                                                             ErrorCategory.PermissionDenied, null);
                            WriteError(er);
                        }
                        else
                        {
                            WriteObject(alreadyLoadedModule.AsCustomObject());
                        }
                    }
                    else if (BasePassThru)
                    {
                        WriteObject(alreadyLoadedModule);
                    }
                }
                else
                {
                    PSModuleInfo moduleToRemove;
                    if (Context.Modules.ModuleTable.TryGetValue(module.Path, out moduleToRemove))
                    {
                        Dbg.Assert(BaseForce, "We should only remove and reload if -Force was specified");
                        RemoveModule(moduleToRemove);
                    }

                    PSModuleInfo moduleToProcess = module;
                    try
                    {
                        // If we're passing in a dynamic module, then the session state will not be
                        // null and we want to just add the module to the module table. Otherwise, it's
                        // a module info from Get-Module -list so we need to read the actual module file.
                        if (module.SessionState == null)
                        {
                            if (File.Exists(module.Path))
                            {
                                bool found;
                                moduleToProcess = LoadModule(module.Path, null, this.BasePrefix, /*SessionState*/ null,
                                                             ref importModuleOptions,
                                                             ManifestProcessingFlags.LoadElements | ManifestProcessingFlags.WriteErrors | ManifestProcessingFlags.NullOnFirstError,
                                                             out found);
                                Dbg.Assert(found, "Module should be found when referenced by its absolute path");
                            }
                        }
                        else if (!string.IsNullOrEmpty(module.Name))
                        {
                            // It has a session state and a name but it's not in the module
                            // table so it's ok to add it

                            // Add it to the all module tables
                            AddModuleToModuleTables(this.Context, this.TargetSessionState.Internal, moduleToProcess);

                            if (moduleToProcess.SessionState != null)
                            {
                                ImportModuleMembers(moduleToProcess, this.BasePrefix, importModuleOptions);
                            }

                            if (BaseAsCustomObject && moduleToProcess.SessionState != null)
                            {
                                WriteObject(module.AsCustomObject());
                            }
                            else if (BasePassThru)
                            {
                                WriteObject(moduleToProcess);
                            }
                        }
                    }
                    catch (IOException)
                    {
                        ;
                    }
                }
            }
            catch (PSInvalidOperationException e)
            {
                ErrorRecord er = new ErrorRecord(e.ErrorRecord, e);
                WriteError(er);
            }
        }
コード例 #2
0
ファイル: ImportModuleCommand.cs プロジェクト: nickchal/pash
 private void ImportModule_ViaLocalModuleInfo(ModuleCmdletBase.ImportModuleOptions importModuleOptions, PSModuleInfo module)
 {
     try
     {
         PSModuleInfo info = null;
         base.Context.Modules.ModuleTable.TryGetValue(module.Path, out info);
         if (!base.BaseForce && base.IsModuleAlreadyLoaded(info))
         {
             ModuleCmdletBase.AddModuleToModuleTables(base.Context, base.TargetSessionState.Internal, info);
             base.ImportModuleMembers(info, base.BasePrefix, importModuleOptions);
             if (base.BaseAsCustomObject)
             {
                 if (info.ModuleType != ModuleType.Script)
                 {
                     InvalidOperationException exception = new InvalidOperationException(StringUtil.Format(Modules.CantUseAsCustomObjectWithBinaryModule, info.Path));
                     ErrorRecord errorRecord = new ErrorRecord(exception, "Modules_CantUseAsCustomObjectWithBinaryModule", ErrorCategory.PermissionDenied, null);
                     base.WriteError(errorRecord);
                 }
                 else
                 {
                     base.WriteObject(info.AsCustomObject());
                 }
             }
             else if (base.BasePassThru)
             {
                 base.WriteObject(info);
             }
         }
         else
         {
             PSModuleInfo info2;
             if (base.Context.Modules.ModuleTable.TryGetValue(module.Path, out info2))
             {
                 base.RemoveModule(info2);
             }
             PSModuleInfo info3 = module;
             try
             {
                 if (module.SessionState == null)
                 {
                     if (File.Exists(module.Path))
                     {
                         bool flag;
                         info3 = base.LoadModule(module.Path, null, base.BasePrefix, null, ref importModuleOptions, ModuleCmdletBase.ManifestProcessingFlags.LoadElements | ModuleCmdletBase.ManifestProcessingFlags.NullOnFirstError | ModuleCmdletBase.ManifestProcessingFlags.WriteErrors, out flag);
                     }
                 }
                 else if (!string.IsNullOrEmpty(module.Name))
                 {
                     ModuleCmdletBase.AddModuleToModuleTables(base.Context, base.TargetSessionState.Internal, info3);
                     if (info3.SessionState != null)
                     {
                         base.ImportModuleMembers(info3, base.BasePrefix, importModuleOptions);
                     }
                     if (base.BaseAsCustomObject && (info3.SessionState != null))
                     {
                         base.WriteObject(module.AsCustomObject());
                     }
                     else if (base.BasePassThru)
                     {
                         base.WriteObject(info3);
                     }
                 }
             }
             catch (IOException)
             {
             }
         }
     }
     catch (PSInvalidOperationException exception2)
     {
         ErrorRecord record2 = new ErrorRecord(exception2.ErrorRecord, exception2);
         base.WriteError(record2);
     }
 }