我在 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/