powershell - 如何在 powershell 脚本中使用动态参数,而不是函数
问题描述
我正在尝试在我的 powershell 脚本中获取一些动态验证集,例如
MyScript.ps1 -Parameter1 DynamicAttributeDefinedWithinRuntime
我找到的所有帮助都是针对 PowerShell 函数中的动态参数,例如
MyPowerShellFunction -Parameter1 DynamicAttributeDefinedWithinRuntime
如果我在脚本中使用动态参数创建一个函数,当从 powershell 窗口执行脚本文件时,我可以在 CLI 中使用这些函数吗?
解决方案
这是一个很棒的参数教程Dynamic
ValidateSet
:
https ://vexx32.github.io/2018/11/29/Dynamic-ValidateSet/
该站点的一种方法是使用第二个功能:
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
}
推荐阅读
- macos - 如何在没有为活动/非活动窗口自定义主题的情况下使 TMUX 中的所有前景色变暗?
- android - 阅读短信中的特定单词后打开活动
- java - java.sql.SQLException:无法使用 jdbc.queryForObject 在位置 1(值为 1)设置参数
- java - 如何从可用作自定义实体 ID 生成器中实体 ID 的对象属性生成 UID?
- python - 在 OpenLDAP 中将用户添加到 POSIX 或“memberOf”组
- linux - 如何在不附加的情况下将文本写入 HDFS 中的文件
- powershell - 如何将多行批处理文件语句转换为 Powershell?
- google-analytics - 在 GTM 创建虚拟页面并将其发送到 GA 的问题
- python-3.6 - 为什么 -1*List 对象返回一个空列表?
- sql-server - 签名存储过程和应用程序角色安全冲突