2
PowerShell 모듈의 ArgumentList가 무엇인지 검색해야하는 스크립트를 작성하려고합니다. 이것을 알아낼 수있는 방법이 있습니까?PowerShell 모듈의 ArgumentList는 어떻게 찾습니까?
마지막 게임은 모듈을로드하기위한 간단한 DI 컨테이너를 만드는 데 사용할 수 있습니다.
PowerShell 모듈의 ArgumentList가 무엇인지 검색해야하는 스크립트를 작성하려고합니다. 이것을 알아낼 수있는 방법이 있습니까?PowerShell 모듈의 ArgumentList는 어떻게 찾습니까?
마지막 게임은 모듈을로드하기위한 간단한 DI 컨테이너를 만드는 데 사용할 수 있습니다.
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'속성을 보는 것과 같은 사전을 반환합니다. 속성 정보를 원하면 각 속성을 구축하기 위해 약간의 작업을 수행해야합니다.
관련 항목 : [Windows PowerShell 모듈 이해] (https://msdn.microsoft.com/en-us/library/dd878324(v=vs.85).aspx) 및/또는 [Get-Command PowerShell cmdlet을 사용하여 매개 변수 집합 정보 찾기 _] (https://blogs.technet.microsoft.com/heyscriptingguy/2012/05/16/use-the-get-command-powershell-cmdlet-to-find-parameter-set-information/) – JosefZ
정확히 "PowerShell 모듈의 인수 목록"은 무엇이라고 생각하십니까? 모듈에는 일반적으로 매개 변수가 없으며 노출시키는 cmdlet 만 매개 변수를 갖습니다. –
예 그들은 보통 가지고 있지 않지만 가질 수 있습니다. psm1 파일에는 param 섹션 만 있으면됩니다. 명령과 함수를 사용하면 Get-Command에서 매개 변수 속성을 검사하여 많은 정보를 얻을 수 있습니다. 모듈 매개 변수에는 해당 사항이 없습니다. –