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
}