How to run an exe with Parameters in a task in Azure Devops?
C

2

6

I would like to know if we have a way to run an exe in Azure devops and passing required parameters for that. I am using below task "PowerShell" and it gives me the error

**

"C:\windows\System32\WindowsPowerShell\v1.0\powershell.exe" -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -Command ". 'D:\a_temp\5f91c58a-381d-4c41-9969-3038116adefa.ps1'" & : The term 'MyProject.exe' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At D:\a_temp\5f91c58a-381d-4c41-9969-3038116adefa.ps1:5 char:3"

**

Below is the task created in YAML.

- task: PowerShell@2
  displayName: Execute .exe file with parameters
  inputs:
    targetType: 'inline'
    script: |
      [String]$myrev = $Env:BUILD_BUILDNUMBER
      $args = @($myrev)
      & 'MyProject.exe' $args

Thanks in advance for the responses.


Little more info, tried to access the path where the build is created. Before accessing the path to run the exe, we are building the project and is kept at the default Azure Build Directory. Still same error but with full path.

- task: MSBuild@1
  inputs:
    solution: '**/*.csproj'
    clean: true

- task: PowerShell@2
  displayName: Execute .exe file with parameters
  inputs:
    targetType: 'inline'
    script: |
      [String]$myrev = $Env:BUILD_BUILDNUMBER
      $args = @($myrev)
      & '$(Build.SourcesDirectory)\MyProject\bin\Release\MyProject.exe' $args 
Carcinoma answered 16/3, 2021 at 7:3 Comment(0)
R
4

Are you sure your application is accessible in the current path? Check your folder content:

trigger:
- master

pool:
  vmImage: windows-2019

steps:
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      & cmd /c dir . 
    

Update:

Your solution should work. The tested example:

trigger:
- master

pool:
  vmImage: windows-2019

steps:
- task: VSBuild@1
  inputs:
    solution: 'ConsoleApp\ConsoleApp.sln'
    platform: 'Any CPU'
    configuration: 'Release'
- task: PowerShell@2
  inputs:
    targetType: 'inline'
    script: |
      [String]$myrev = $(Build.BuildNumber)      
      $args = @($myrev)
      & '$(Build.SourcesDirectory)\ConsoleApp\ConsoleApp\bin\Release\ConsoleApp.exe' $args 

Build result:

enter image description here

Check your path here:

enter image description here

Roborant answered 16/3, 2021 at 9:19 Comment(4)
Before accessing the exe, I am building the project and accessing the exe using the $(Build.SourceDirectory) below are the 2 tasks: - task: MSBuild@1 inputs: solution: '*/.csproj' clean: true - task: PowerShell@2 displayName: Execute .exe file with parameters inputs: targetType: 'inline' script: | [String]$myrev = $Env:BUILD_BUILDNUMBER $args = @($myrev) & '$(Build.SourcesDirectory)\MyProject\bin\Release\MyProject.exe' $args Do we need to provide access to this Path at the Task level?Carcinoma
@PraveshPesswani, your solution should work. I`ve tested it. Check the path on the build step.Roborant
@PraveshPesswani, additionally, check the configuration parameter in msbuild task. It can use Debug by default.Roborant
You are right, I wasn't able to access the source directory, added a step to copy all files to staging and then it worked! Thanks a lot.Carcinoma
L
0

you can use Start-Process to execute the .exe file with arguments (https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/start-process?view=powershell-7.1)

In your code it seems that it cannot find the .exe file. Check first if the .exe file really exists (You can use Get-ChildItem to get a list of all files located in your current working directory)

BR

Loring answered 16/3, 2021 at 7:34 Comment(2)
Tried following this. but doesnt work in Yaml script in Azure devops. Do you have any example?Carcinoma
Example of Start-Process would be following: Start-Process -FilePath .\FILENAME.exe -ArgumentList "arg1 arg2 arg3 etc" With Get-ChildItem i meant that you list only your files, so you can see if your .exe file really exists in the correct folder. I think the user above already solved your problem :)Loring

© 2022 - 2024 — McMap. All rights reserved.