我正在azure构建管道中创建新的VSTS / TFS扩展。 在该扩展名中,一个字段接受文件路径,它是可选字段。 在Powershell脚本中,我想验证该字段,如果未提供任何输入,则必须忽略,否则必须检查输入是否为.config文件的路径。
$ConfigFileName = Get-VstsInput -Name 'ConfigFilePath'
if (!(Test-Path $ConfigFileName))
{
Write-Host "Configuration file doesn't exist."
"##vso[task.complete result=Failed]"
throw "Configuration file doesn't exist."
}
if([IO.Path]::GetExtension($ConfigFileName) -ne '.config')
{
Write-Host "Invalid configuration file.File type must be of .config"
"##vso[task.complete result=Failed]"
throw "Invalid configuration file.File type must be of .config"
}
我已经如上所述进行了验证,但是当用户未提供任何输入时,$ ConfigFileName变量也将填充为$值的映射路径。 如何检查提供给该字段的输入是否为空?
答案 0 :(得分:1)
对于filePath类型输入字段,默认值为源目录(Build.SourcesDirectory,例如D:\ a \ 1 \ s)。因此,您可以检查该值是否不等于源目录。
例如:
{
"name": "filePathSelect",
"type": "filePath",
"label": "test select Path",
"required": false,
"defaultValue": "",
"helpMarkDown": "test select path"
}
PowerShell:
Write-Host "get select file path value"
$fileSelectPathValue = Get-VstsInput -Name filePathSelect
Write-Host "select file path value is $fileSelectPathValue"
Write-Host "default path $env:Build_SourcesDirectory"
if([string]::IsNullOrEmpty($fileSelectPathValue)){
Write-Host "Selected File path is empty"
}
elseif(($fileSelectPathValue -eq $env:Build_SourcesDirectory) -or !(Test-Path $fileSelectPathValue))
{
Write-Host "select path is invalid"
}
else
{
Write-Host "select path is valid"
}
如果输入类型为字符串,则只需要检查值是否为空[string]::IsNullOrEmpty
即可。