1

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.

2
  • 2
    Can you provide the detailed YAML of your pipeline and the detailed logs of the failed step? when you run the script on your local machine, what version of PowerShell you were using, Windows PowerShell or PowerShell 7? What is the actual command of command1 to run? Commented Jul 16, 2024 at 3:20
  • @BrightRan-MSFT In my local, I am using Windows Powershell. The actual command is ` "init-instrument" | & $appCmd $exeArguments ` I tried to give just any value before | (pipe), but it added the diamond question mark. I also tried to do -Encoding UTF8 and still see the same issue. Commented Jul 16, 2024 at 18:07

1 Answer 1

0

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
Sign up to request clarification or add additional context in comments.

6 Comments

I'm glad the linked answer helped. What I meant to say earlier about the symptoms you saw being mystifying is that it would still be good to know why your original attempt didn't work, given that it seems that it should, and given that it is a simpler solution.
Until now the reason is unknown to me as well. I read this post, perhaps we should get the input to pipe can be taken through a function like this? I could not try this because my scenarios were different. link
A function shouldn't be necessary, because PowerShell automatically relays (stringified on demand) pipeline input to an external program's stdin stream line by line. It looks like .\myapplication.exe is receiving the input line, but there may be a hidden control character in the input string (there is none in the YAML you posted).
I test with just the random inputs like ` "123" | & $appCmd $exeArguments 123 | & $appCmd $exeArguments ` all of them have a ? (prefixed with a diamond question mark). I pasted the error in Notepad++ (with encoding ANSI), and then I can see the ? in the error. If I paste the error message with UTF8 encoding in the notepad++ I do NOT see the question mark. I tried adding encoding to pipeline input still had the same issue.
@Tom, Glad that the issue can be resolved, and thanks for sharing the solution. Maybe, you can accept this answer by yourself. It could be also helpful for other people who are looking for a solution of the similar issues.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.