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

.net 从托管powershell中的脚本或命令检索参数,.net,powershell,clr,.net,Powershell,Clr,我正在VB.net应用程序中托管PowerShell。我希望能够在运行时计算出我正在运行的任何随机脚本都需要哪些参数。例如,给定代码: Using powerShellObject As PowerShell = PowerShell.Create powerShellObject.AddScript("get-process") powerShellObject.AddParameter("FileVersionInfo", "") Dim output As Ne

我正在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
使用powerShellObject作为PowerShell=PowerShell.Create
powerShellObject.AddScript(“获取进程”)
powerShellObject.AddParameter(“FileVersionInfo”和“”)
作为新PSDataCollection(PSObject的)的Dim输出()
AddHandler output.DataAdded,AddressOf output\u DataAdded
AddHandler powerShellObject.InvocationStateChanged,Powershell\u InvocationStateChanged的地址
'异步调用管道。
Dim asyncResult作为IAsyncResult=powerShellObject.BeginInvoke(属于PSObject,PSObject)(无,输出)
而powerShellObject.InvocationStateInfo.State PSInvocationState.Completed
System.Threading.Thread.Sleep(0)
结束时
终端使用
我希望能够确定通过“AddScript”添加的脚本需要哪些参数(我知道get进程可以使用这些参数)

Get-Process [-ComputerName <String[]>] [-FileVersionInfo] [-Module] -Id <Int32[]> [<CommonParameters>]
获取进程[-ComputerName][-FileVersionInfo][-Module]-Id[]
但我希望我的用户能够提交任何随机脚本,并且我希望能够在运行时弄清楚这些参数是什么

我该怎么做

Get-Command -Syntax MyScript.ps1
将返回语法。我假设这仅限于处理脚本中的
param
语句(执行更多操作将非常困难)

这也适用于cmdlet(并且作为一种比阅读帮助、查找参数名更快的方法非常有用)

要获取每个参数的数据,请执行以下操作:

(Get-Command MyScript).Parameters

是一个由参数名键入的散列,其中包含各种有趣的信息。

谢谢你,Richard,效果很好。我没有想到调用其他powershell cmd来获取信息,我戴着护目镜,希望有一种管理方式。