powershellazure-pipelinescommand

Pipeline input to executable with PowerShell with Error � character is added to the command when executing the AzurePipeline powershell Task


I am trying to run the below line in the Azure pipeline Powershell script.

- task: PowerShell@2
  displayName: 'Run test sequence'
  inputs:
    targetType: 'inline'
    script: |
            Write-Host "Running test sequence..."      
       
            $AppConfig = "$(System.DefaultWorkingDirectory)\myapplicationfile.xml"
            $RunMcsProduct = "$(System.DefaultWorkingDirectory)\myapplicationFolder"
            $SimulatedQuickDeviceConfig = "$(System.DefaultWorkingDirectory)\myDevicefile.xml"

            # Run Product exe with arguments
            $exeArguments = $AppConfig , $SimulatedQuickDeviceConfig
            Set-Location -Path $RunMcsProduct
            $appCmd = ".\myapplication.exe"
            
            "init-instrument" | & $appCmd $exeArguments           

            Write-Host "Complete."

And when the pipeline runs I get the following error. (Sorry I cannot show the entire log due to some sensitive information in the log.

enter image description here

However, the same script I can execute successfully on my local computer.


Solution

  • The solution from here helped me. link

    There are two solutions mentioned in the above link. The one that worked for me is below.

            # Create txt tempFileand add commands to it
            new-item -path $tempFile.txt  -type file
            "command1" | add-content -path $tempFile
            "command2" | add-content -path $tempFile 
    
    
            Start-Process -Wait -NoNewWindow -RedirectStandardInput $tempFile -FilePath findstr.exe -ArgumentList hi 
    
            # Cleanup temp file
            $tempFile | Remove-Item