2017-03-06 12 views
2

私は、PowerShellモジュールのArgumentListが何であるかを検出する必要のあるスクリプトを作成しようとしています。これを見つける方法はありますか?PowerShellモジュールのArgumentListはどのようにして見つけられますか?

最後のゲームでは、モジュールをロードするための単純なDIコンテナを作成することができます。

+1

関連情報[Windows PowerShellモジュールについて](https://msdn.microsoft.com/en-us/library/dd878324(v=vs.85)asp)および/または[Get-Command PowerShellを使用するコマンドレットを検索するコマンドレット_](https://blogs.technet.microsoft.com/heyscriptingguy/2012/05/16/use-the-get-command-powershell-cmdlet-to-find-parameter-set-information/ ) – JosefZ

+1

"PowerShellモジュールの引数リスト"とは正確に何と考えていますか?モジュールには通常パラメータはなく、エクスポーズするコマンドレットだけが行います。 –

+1

はい、通常は持っていませんが、持っています。これは、psm1ファイルにparamセクションが必要です。コマンドと関数を使用すると、Get-Commandからparametersプロパティを調べて、多くの情報を取得できます。モジュールのパラメータに相当するものはないようです –

答えて

1

ASTパーサを使用して、モジュールファイルのparam()ブロックを表示できます。たぶん、Get-Moduleを使ってモジュールファイルがどこにあるかについての情報を見つけ出し、それらを解析し、ASTを歩いてあなたが後の情報を取得するかもしれません。これは役に立ちそうなもののようですか?

function Get-ModuleParameterList { 
    [CmdletBinding()] 
    param(
     [string] $ModuleName 
    ) 

    $GetModParams = @{ 
     Name = $ModuleName 
    } 

    # Files need -ListAvailable 
    if (Test-Path $ModuleName -ErrorAction SilentlyContinue) { 
     $GetModParams.ListAvailable = $true 
    } 

    $ModuleInfo = Get-Module @GetModParams | select -First 1 # You'll have to work out what to do if more than one module is found 

    if ($null -eq $ModuleInfo) { 
     Write-Error "Unable to find information for '${ModuleName}' module" 
     return 
    } 

    $ParseErrors = $null 
    $Ast = if ($ModuleInfo.RootModule) { 
     $RootModule = '{0}\{1}' -f $ModuleInfo.ModuleBase, (Split-Path $ModuleInfo.RootModule -Leaf) 

     if (-not (Test-Path $RootModule)) { 
      Write-Error "Unable to determine RootModule for '${ModuleName}' module" 
      return 
     } 

     [System.Management.Automation.Language.Parser]::ParseFile($RootModule, [ref] $null, [ref] $ParseErrors) 
    } 
    elseif ($ModuleInfo.Definition) { 
     [System.Management.Automation.Language.Parser]::ParseInput($ModuleInfo.Definition, [ref] $null, [ref] $ParseErrors) 
    } 
    else { 
     Write-Error "Unable to figure out module source for '${ModuleName}' module" 
     return 
    } 

    if ($ParseErrors.Count -ne 0) { 
     Write-Error "Parsing errors detected when reading RootModule: ${RootModule}" 
     return 
    } 

    $ParamBlockAst = $Ast.Find({ $args[0] -is [System.Management.Automation.Language.ParamBlockAst] }, $false) 

    $ParamDictionary = [ordered] @{} 
    if ($ParamBlockAst) { 
     foreach ($CurrentParam in $ParamBlockAst.Parameters) { 
      $CurrentParamName = $CurrentParam.Name.VariablePath.UserPath 
      $ParamDictionary[$CurrentParamName] = New-Object System.Management.Automation.ParameterMetadata (
       $CurrentParamName, 
       $CurrentParam.StaticType 
      ) 

      # At this point, you can add attributes to the ParameterMetaData instance based on the Attribute 

     } 
    } 
    $ParamDictionary 
} 

モジュール名またはモジュールへのパスを指定する必要があります。それはほとんどテストされていないので、恐らく動作しない場合があります。現在、Get-Commandから返された 'Parameters'プロパティを表示するような辞書を返します。属性情報が必要な場合は、それぞれを構築するために少しの作業を行う必要があります。

関連する問題