powershell - 我需要在我的powershell脚本中添加对参数的依赖条件什么是最好的方法?
问题描述
param (
[Parameter(Mandatory=$true, HelpMessage="action")]
[ValidateSet('deploy','remove')]
[string]$action = "list",
[Parameter( Mandatory=$false, HelpMessage="zip File")]
[ValidateScript({
if(-Not ($_ | Test-Path) ){
throw "File or folder does not exist"
}
if(-Not ($_ | Test-Path -PathType Leaf) ){
throw "The Path argument must be a file. Folder paths are not allowed."
}
if($_ -notmatch "(\.zip)"){
throw "The file specified in the path argument must be a zipFile"
}
return $true
})]
[System.IO.FileInfo]$zipFile
)
这些是我的脚本中的参数,我想在此处添加一个条件,例如 zipFile 参数只有在部署操作时才应该是强制性的,我们可以添加这可能是使用动态参数吗?像这样的东西会起作用吗?
param(
[Parameter(Mandatory=$true, HelpMessage="action")]
[ValidateSet('deploy','remove')]
[string]$action = "list",
[Parameter( Mandatory=$false, HelpMessage="zip File")]
[ValidateScript({
if(-Not ($_ | Test-Path) ){
throw "File or folder does not exist"
}
if(-Not ($_ | Test-Path -PathType Leaf) ){
throw "The Path argument must be a file. Folder paths are not allowed."
}
if($_ -notmatch "(\.zip)"){
throw "The file specified in the path argument must be a zipFile"
}
return $true
})]
[System.IO.FileInfo]$zipFile
)
DynamicParam {
if ($action -eq "deploy"){
$zipFile.Mandatory = $true
}
}
process {
#Here comes script
}
解决方案
推荐阅读
- python - 计算在特定点的方向上移动一个像素
- python - CheckBoxGroup 散景 Python
- numpy - 使用 colab reutrns 进行 VGG16 训练 WARNING:root:kernel
- mysql - 编辑 my.ini 后,MySQL 无法在 Windows 中打开
- linux - 怀疑用 DB2 创建连接字符串
- wordpress - 如何使用 AWS CDK 通过 AWS Fargate 容器配置 Wordpress
- nats.io - NATS 消息传递:导出问题
- python - Python QT5 - 多进程 OpenCV 网络摄像头和 Requests.Get
- r - 为什么我收到以下错误 - gsub("^(", x[1, ]) 中的错误:缺少参数 "x",没有默认值
- android - 提高 wrap_content height recyclerview 的性能