如何在powershell脚本中使用动态参数而不是函数

如何在powershell脚本中使用动态参数而不是函数,powershell,Powershell,我正在尝试在我的powershell脚本中获得一些动态验证集,如 MyScript.ps1-参数1 dynamicattributed在runtime中定义 我找到的所有帮助都是针对PowerShell函数中的动态参数,如 MyPowerShellFunction-参数1 dynamicAttribute在运行时间内定义 如果我在脚本中创建了一个带有动态参数的函数,那么当从powershell窗口执行脚本文件时,我可以在CLI中使用这些参数吗?这里有一个关于动态验证集参数的精彩教程: 该站点的

我正在尝试在我的powershell脚本中获得一些动态验证集,如

MyScript.ps1-参数1 dynamicattributed在runtime中定义

我找到的所有帮助都是针对PowerShell函数中的动态参数,如

MyPowerShellFunction-参数1 dynamicAttribute在运行时间内定义


如果我在脚本中创建了一个带有动态参数的函数,那么当从powershell窗口执行脚本文件时,我可以在CLI中使用这些参数吗?

这里有一个关于
动态
验证集
参数的精彩教程:

该站点的一种方法是使用第二种功能:

function Get-ValidValues {
    [CmdletBinding()]
    param($Path)

    (Get-ChildItem -Path $Path -File).Name
}

function Clear-FileInCurrentLocation {
    <#
    .synopsis
        Dynamic ValidateSet from: https://vexx32.github.io/2018/11/29/Dynamic-ValidateSet/
    #>
    [CmdletBinding(SupportsShouldProcess, ConfirmImpact = 'High')]
    param(
        [Parameter(Position = 0, Mandatory)]
        [ArgumentCompleter(
            {
                param($Command, $Parameter, $WordToComplete, $CommandAst, $FakeBoundParams)
                Get-ValidValues -Path (Get-Location)
            }
        )]
        [ValidateScript(
            {
                $_ -in (Get-ValidValues -Path (Get-Location))
            }
        )]
        [string]
        $Path
    )

    Clear-Content -Path $Path
}
函数获取有效值{
[CmdletBinding()]
参数($Path)
(Get ChildItem-Path$Path-File).Name
}
函数清除文件位置{
[CmdletBinding(SupportsShouldProcess,ConfirmImpact='High')]
param(
[参数(位置=0,必填)]
[论点完成者(
{
param($Command,$Parameter,$WordToComplete,$CommandAst,$FakeBoundParams)
获取有效值-路径(获取位置)
}
)]
[验证说明](
{
$\输入(获取有效值-路径(获取位置))
}
)]
[字符串]
$Path
)
清除内容-路径$Path
}

与使用函数的过程相同-在脚本文件的根位置添加一个
dynamicparam
块,正如MathiasR所指出的那样。Jessen指出,您可以使脚本看起来像一个函数。将函数中位于
函数
行和结束
}
之间的所有内容放入文件中。然后像调用函数一样调用它。