0

I'm creating new VSTS/TFS extension in azure build pipeline. In that extension one field accepts a file path and it is optional field. In powershell script, i want to validate the field, if no input is provided then i have to ignore, otherwise i have to check whether input is path of .config file or not.

$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"
      }

I have validated like above, but when user not provided any input then also the $ConfigFileName variable filled with mapping path that is $ value. How to check if input provided to that field is empty or not?

Kalyani Reddy
  • 131
  • 10

1 Answers1

1

For filePath type input field, the default value is source directory (Build.SourcesDirectory, e.g. D:\a\1\s). So, you could check whether the value is not equal to the source directory.

For example:

{
  "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"
}

If the input type is string, then you just need to check whether the value is null or empty [string]::IsNullOrEmpty.

starian chen-MSFT
  • 33,174
  • 2
  • 29
  • 53