ImportPSModule故障检测 [英] ImportPSModule Failure Detection

查看:219
本文介绍了ImportPSModule故障检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用InitialSessionState.ImportPSModule以进口PowerShell的模块。

我想知道,如果导入失败模块由于某种原因(例如,未找到文件等)。把code try块不会引发故障的情况下,任何的异常和功能似乎无声地失败,继续,如果它不能够导入模块。

有没有办法在code。如果导入失败,被惊动?

我试图做类似以下内容。在下面的code中,模块TestModule1234不存在。 catch块不捕获异常。

请注意:这仅仅是原型试验code,所以请忽略任何生产code相关违规行为。

 尝试
        {
            //初始化PowerShell的运行空间
            InitialSessionState psSessionInitialState = InitialSessionState.CreateDefault();


            LOGFILE.LOG(导入模块TestModule1234);
            psSessionInitialState.ImportPSModule(新[] {TestModule1234});

            LOGFILE.LOG(创建Powershell的运行空间);
            m_PoshRunspace = RunspaceFactory.CreateRunspace(psSessionInitialState);
        }
        赶上(System.Exception的前)
        {
            LOGFILE.LOG(无法创建一个PowerShell运行空间);
            LOGFILE.LOG(ex.ToString());
            扔;
        }
 

解决方案

由于种种原因缺少模块不会被视为致命错误。但他们 还是重新codeD错误。为了解决这一问题做的:

  1. 打开的运行空间为打开()。还在为此在try块,以赶上其他异常,他们是可能的,我在实践中有过这样的情况。
  2. 获取标准错误列表( $错误),并打开特定的失踪模块错误或其他错误后分析。

下面是仅使用.NET方法,以便它直译为C#的工作实例中的PowerShell。这个脚本不会失败(异常没有被抛出),但它显示了作为变量的错误 错误

  $ psSessionInitialState = [System.Management.Automation.Runspaces.InitialSessionState] :: CreateDefault();

导入模块TestModule1234
$ psSessionInitialState.ImportPSModule(@(TestModule1234))

创建PowerShell的运行空间
$ PoshRunspace = [System.Management.Automation.Runspaces.RunspaceFactory] ​​:: CreateRunspace($ psSessionInitialState)

打开PowerShell的运行空间
$ PoshRunspace.Open()

获得运行空间错误
$ PoshRunspace.SessionStateProxy.PSVariable.GetValue(错误)
 

输出:

 导入模块TestModule1234
创建PowerShell的运行空间
打开PowerShell的运行空间
获取运行空间错误
导入模块:指定的模块TestModule1234'未加载,因为没有有效的模块文件中发现了任何模块目录。
    + CategoryInfo:ResourceUnavailable:(TestModule1234:字符串)导入模块],FileNotFoundException异常
    + FullyQualifiedErrorId:Modules_ModuleNotFound,Microsoft.P​​owerShell.Commands.ImportModuleCommand
 

I am trying to use InitialSessionState.ImportPSModule in order to import a Powershell module.

I am interested in knowing if importing of the module failed due to any reason (e.g file not found etc.). Putting the code in the try block does not raise any exception in the case of failure and the function seems to fail silently and continue if it is not able to import the module.

Is there a way to be alerted in the code if the import fails?

I am trying to do something like the following. In the code below, the module "TestModule1234" does not exist. The catch block does not catch an exception.

Note: This is just prototype test code, so please ignore any production code related irregularities.

        try
        {
            //Initializing the PowerShell runspace
            InitialSessionState psSessionInitialState =    InitialSessionState.CreateDefault();


            LogFile.Log("Importing Module TestModule1234");
            psSessionInitialState.ImportPSModule(new[] { "TestModule1234" });

            LogFile.Log("Creating Powershell Runspace");
            m_PoshRunspace = RunspaceFactory.CreateRunspace(psSessionInitialState);
        }
        catch (System.Exception ex)
        {
            LogFile.Log("Failed to create a Powershell Runspace");
            LogFile.Log(ex.ToString());
            throw;
        }

解决方案

For some reasons missing modules are not treated as fatal errors. But they are still recoded errors. In order to solve the problem do:

  1. Open your runspace by Open(). Do this still in the try block in order to catch other exceptions, they are possible, I had such cases in practice.
  2. Get the standard error list ($Error) and analyse it after opening for specific "missing module" errors or other errors.

Here is the working example in PowerShell using only .NET methods, so that it is literally translated to C#. This script does not fail (exceptions are not thrown) but it shows the error obtained as the variable Error:

$psSessionInitialState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault();

"Importing Module TestModule1234"
$psSessionInitialState.ImportPSModule(@("TestModule1234"))

"Creating PowerShell Runspace"
$PoshRunspace = [System.Management.Automation.Runspaces.RunspaceFactory]::CreateRunspace($psSessionInitialState)

"Opening PowerShell Runspace"
$PoshRunspace.Open()

"Getting Runspace Error"
$PoshRunspace.SessionStateProxy.PSVariable.GetValue('Error')

Output:

Importing Module TestModule1234
Creating Powershell Runspace
Opening Powershell Runspace
Getting Runspace Error
Import-Module : The specified module 'TestModule1234' was not loaded because no valid module file was found in any module directory.
    + CategoryInfo          : ResourceUnavailable: (TestModule1234:String) [Import-Module], FileNotFoundException
    + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand

这篇关于ImportPSModule故障检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆