【问题标题】:Is it possible to a get a list of the currently defined functions in a powershell runspace?是否可以在 powershell 运行空间中获取当前定义的函数的列表?
【发布时间】:2015-11-10 12:18:23
【问题描述】:

我知道我可以通过以下方式获取最初定义的命令:

Runspace rs = RunspaceFactory.CreateRunspace();
rs.InitialSessionsState.Commands

或在 PowerShell 本身中:

[System.Management.Automation.Runspaces.Runspace]::DefaultRunspace.InitialSessionState.Commands

但这仅代表创建运行空间之前的状态。

如果我导入一个模块(即使通过手动创建会话状态对象并在打开运行空间之前使用sesh.ImportPSModule()),这些函数或 cmdlet 都不会显示在列表中。

如果我用.AddScript() 或其他东西定义一个新函数也是一样的。

有没有办法从运行空间外部获取运行空间的当前状态,而不仅仅是初始状态?

我想过只是从运行空间内调用Get-Command 并返回对象,但它似乎.. 出于某种原因对我来说是错误的。我觉得我在这里遗漏了一些简单的东西,应该有一种方法可以查看当前状态以及其中定义的内容。

【问题讨论】:

    标签: c# powershell runspace


    【解决方案1】:

    功能 PSProvider 是否满足您的需求?

    Get-ChildItem function:
    

    【讨论】:

    • 这与使用Get-Command 基本相同,这很好,但它必须在运行空间内运行。我正在尝试从运行空间 outside 获取当前命令列表。
    【解决方案2】:

    Runspace 具有SessionStateProxy 属性,可让您与Runspace SessionState 进行交互:

    $PowerShell = [PowerShell]::Create()
    $PowerShell.AddScript{
        function SomeFunction {}
        function SomeOtherFunction {}
    }.Invoke()
    
    $PowerShell.Runspace.SessionStateProxy.InvokeCommand.GetCommands('*','Function',$true)
    

    【讨论】:

    • 谢谢,自从我不久前停止从事这个项目以来,我还不能尝试这个。我不确定我是否/何时会重新开始,但下次我做这种事情时我一定会检查一下。
    猜你喜欢
    • 1970-01-01
    • 2011-11-12
    • 2011-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-04
    相关资源
    最近更新 更多