試圖更好地理解Dynamic Parameters,但有些事情一直困擾著我。我在外部網路(internet)上找到的所有示例,始終定義/包含引數屬性。
這是我正在使用的內容:
Function Test-DynamicParameters {
[cmdletbinding()]
Param (
[System.IO.DirectoryInfo]$Path
)
DynamicParam
{
if ($Path.Extension) {
$parameterAttribute = [System.Management.Automation.ParameterAttribute]@{
Mandatory = $false
}
$attributeCollection = [System.Collections.ObjectModel.Collection[System.Attribute]]::new()
$attributeCollection.Add($parameterAttribute)
$dynParam1 = [System.Management.Automation.RuntimeDefinedParameter]::new(
'TestSwitch', [switch], $attributeCollection
)
$paramDictionary = [System.Management.Automation.RuntimeDefinedParameterDictionary]::new()
$paramDictionary.Add('TestSwitch', $dynParam1)
$paramDictionary
}
}
Begin { }
Process
{
$PSBoundParameters
}
End { }
}
. . .currently 已經嘗試了多種洗掉某些行/代碼的組合,以查看什么會使我的動態引數顯示,但如果沒有宣告屬性,則沒有任何效果。這是必要的嗎?
問題:我需要它作業的最簡單的動態引數宣告形式是什么?
例如 - 它可以縮短到我只定義名稱的地方嗎?或者,PowerShell 會堅持指定型別而不是默認為[object]?就像是:
$paramDictionary = [System.Management.Automation.RuntimeDefinedParameterDictionary]::new()
$paramDictionary.Add('TestSwitch')
$paramDictionary
uj5u.com熱心網友回復:
你在尋找什么樣的行為?因為通常不需要動態引數,即使自動完成是動態的。
例如你可以做
- 自動完成模塊名稱
Get-Command -Module - 或按檔案型別搜索檔案,僅在該目錄中存在完整檔案型別時
Windows PowerShell
您可以在 WindowsPowershell 中使用DynamicValidateSet模式
使用 DynamicValidateSet 的 gif

PowerShell 中的新功能
6 增加建議
新:[IValidateSetValuesGenerator]和[ArgumentCompletions]
它像 一樣自動完成[ValidateSet],但用戶仍然可以自由輸入其他人。
注意,[ArgumentCompletions]和[ArgumentCompleter]是兩個不同的類。
[Parameter(Mandatory)]
[ArgumentCompletions('Fruits', 'Vegetables')]
[string]$Type,
7.2 通用完成器
帶有介面的新[ArgumentCompleter]屬性[IArgumentCompleterFactory]
從檔案:
[DirectoryCompleter(ContainingFile="pswh.exe", Depth=2)]
[DateCompleter(WeekDay='Monday', From="LastYear")]
[GitCommits(Branch='release')]
uj5u.com熱心網友回復:
您不能作為System.Management.Automation.RuntimeDefinedParameterDictionary型別的函式添加需要提供一個鍵和值,該鍵和值恰好是一個字串(鍵)和一個System::Management::Automation::RuntimeDefinedParameter作為該值。有關更多資訊,您可以在此處查看課程檔案https://docs.microsoft.com/en-us/dotnet/api/system.management.automation.runtimedefinedparameterdictionary?view=powershellsdk-7.0.0。我已經在我這邊進行了測驗,您至少需要將 ParameterAttribute Mandatory設定為 true 才能使其運行,以及RuntimeDefinedParameter 中使用的鍵/名稱添加時必須與字典中使用的鍵匹配。所以你需要的最少代碼應該是這樣的:
Function Test-DynamicParameters {
[cmdletbinding()]
Param (
[System.IO.FileSystemInfo ]$Path
)
DynamicParam
{
if ($Path.Extension) {
$parameterAttribute = [System.Management.Automation.ParameterAttribute]@{
Mandatory = $true
}
$attributeCollection = [System.Collections.ObjectModel.Collection[System.Attribute]]::new()
$attributeCollection.Add($parameterAttribute)
$dynParam1 = [System.Management.Automation.RuntimeDefinedParameter]::new("TestSwitch", [string], $attributeCollection)
$paramDictionary = [System.Management.Automation.RuntimeDefinedParameterDictionary]::new()
$paramDictionary.Add('TestSwitch', $dynParam1)
$paramDictionary
}
}
Begin { }
Process
{
$PSBoundParameters
}
End { }
}
$FileNamePath = "C:\tmp\stackoverflow\test.txt";
$path = (Get-Item $FileNamePath )
Test-DynamicParameters -Path $path
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/401123.html
