标签: invoke-build

位置参数之前的动态切换可能无法按预期工作

以下是演示此问题的代码.如果提供者是,则cmdlet Set-Location具有动态切换.ReadOnlyFileSystem

# provider that does not have the dynamic -ReadOnly
Set-Location env:

# case 1: works because we explicitly specify FileSystem
Get-ChildItem C:\ -ReadOnly

# case 2: fails even though we explicitly specify FileSystem
Get-ChildItem -ReadOnly C:\
Run Code Online (Sandbox Code Playgroud)

通常,命令中的开关参数的位置无关紧要.这不是动态切换的情况.案例2失败并出现错误:

Get-ChildItem : A parameter cannot be found that matches parameter name 'ReadOnly'.
Run Code Online (Sandbox Code Playgroud)

怎么了?我认为在动态参数创建的那一刻,还不知道ReadOnly是开关.因此,PowerShell将它作为它的参数规则的参数C:\C:\因此不被视为一个位置参数.因此,Get-ChildItem认为未指定位置并使用当前位置env:.提供程序Environment不提供动态切换ReadOnly,因此最终命令因语法不正确而失败,即使它有些正确(如果当前提供程序是相同的命令,则相同的命令FileSystem).

问题:

  • 我对问题的理解是否正确?
  • 这个功能是否记录在某处?
  • 有没有解决方法?

最后一个问题是关于使用动态参数开发的用户命令的更多信息.该问题最初被注意到并描述为Invoke-Build Issue#4.目前,这个问题只是记录在案.但我仍然对解决方法感兴趣. …

powershell invoke-build

6
推荐指数
1
解决办法
647
查看次数

标签 统计

invoke-build ×1

powershell ×1