Search code examples
httpazure-devopsyamlazure-pipelinesnetsh

Can you use "netsh http add urlacl" conditionally in an Azure pipeline YAML file?


We need to register the HTTP listeren on our build servers and want our pipeline steps to all be "green" when everything's in order. So for our code to run, we have to run the following code:

netsh http add urlacl url=http://+:8080/

However when running this twice on a build server, we get the following error:

Url reservation add failed, Error: 183 Cannot create a file when that file already exists.

So far so good. However if we'd ever change Build servers or add/remove some etc, our code might not work anymore. And if that's months/years later, who's going to remember adding the http listener...

So, is there a way to conditionally add the urlacl code?

Something like:

netsh http show urlacl url=http://+:8080/ 
    {If No results, only then run the following line of code, otherwise do nothing else:}
netsh http add urlacl url=http://+:8080/

The workaround would be to document this somewhere and if we'd ever run into issues again, remember where it was documented. Or execute this step always but add a "condition: succeededOrFailed()" to the next YAML step? However these options are not preferable.


Solution

  • So, is there a way to conditionally add the urlacl code?

    You can use the PowerShell task to run the following script to conditionally add the urlacl code.

    - task: PowerShell@2
      inputs:
        targetType: 'inline'
        script: |
          $urlacl = netsh http show urlacl url=http://+:8080/
          if ($urlacl -like "*Reserved URL            : http://+:8080/*") {
              Write-Host "URL reservation already exists, no action taken."
          } else {
              netsh http add urlacl url=http://+:8080/ user=DOMAIN\user
              netsh http show urlacl url=http://+:8080/
          }
    

    Here is my test result:

    result