.net - 从托管 powershell 中的脚本或命令检索参数

标签 .net powershell clr

我在 VB.net 应用程序中托管 PowerShell。我希望能够在运行时弄清楚我正在运行的任何随机脚本采用哪些参数。例如,给定代码:

 Using powerShellObject As PowerShell = PowerShell.Create

    powerShellObject.AddScript("get-process")
    powerShellObject.AddParameter("FileVersionInfo", "")
    Dim output As New PSDataCollection(Of PSObject)()
    AddHandler output.DataAdded, AddressOf Output_DataAdded
    AddHandler powerShellObject.InvocationStateChanged, AddressOf Powershell_InvocationStateChanged

    ' Invoke the pipeline asynchronously.
    Dim asyncResult As IAsyncResult = powerShellObject.BeginInvoke(Of PSObject, PSObject)(Nothing, output)

    While powerShellObject.InvocationStateInfo.State <> PSInvocationState.Completed
        System.Threading.Thread.Sleep(0)
    End While

End Using

我希望能够确定通过“AddScript”添加的脚本采用哪些参数(我知道 get-process 可以采用)

Get-Process [-ComputerName <String[]>] [-FileVersionInfo] [-Module] -Id <Int32[]> [<CommonParameters>]

但我希望我的用户能够提交任何随机脚本,并且我希望能够在运行时弄清楚这些参数是什么。

我该怎么做?

最佳答案

Get-Command -Syntax MyScript.ps1

将返回语法。我认为这仅限于处理脚本中的 param 语句(做更多的事情会相当困难)。

这也适用于 cmdlet(并且非常有用,比阅读帮助、查找参数名称更快)。

获取每个参数数据:

(Get-Command MyScript).Parameters

是一个以 System.Management.Automation.ParameterMetadata 参数名称为键的哈希值其中包含各种有趣的信息。

关于.net - 从托管 powershell 中的脚本或命令检索参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22205440/

相关文章:

c# - 如何计算字符串中的Enter?

.net - 观察添加到 RX 列表中的项目

.net - 如何防止 ReadDirectoryChangesW 丢失文件更改

powershell - 跟踪/调试 PowerShell 运算符

Powershell脚本 “on exit”事件?

.net - 具有多个图标的 TreeView

windows - Windows Powershell脚本中的服务解释问题

clr - 可以让 WinDBG 在符号存储中找到 mscordacwks.dll 吗?

c# - 无法添加对 WinRT C++ DLL 项目的引用

c++ - 查找 DLL 的 CLR 版本