1

I have a short PowerShell script which saves me typing time ... maybe in the future when this snippets works ...

The first Expression starts an executable with some parameters (an assembler). If this tool exits with no errors, a Python script is launched with the help of the Python launcher py.exe. In addition to this, I collect all formatted assembler files (*.fmt) and delete them.

When I run this script, the Python script is executed first and after that the assembler runs.

What am I missing here?

Invoke-Expression "..\asm\KCPSM6.exe -c4096 main_KC705.psm"

if ($LastExitCode -ne 0) {
    Write-Host "ERROR: KCPSM6.exe return with an error code." -ForegroundColor Red
    exit 1
} else {
    Invoke-Expression "py.exe -3 ..\py\psmProcessor.py -v main_KC705.log"

    $fileList = Get-ChildItem -Path ..\psm -Recurse -Filter *.fmt
    $fileList += Get-ChildItem -Path ..\lib -Recurse -Filter *.fmt
    $fileCount = $fileList.Count
    Write-Host "Deleting $fileCount temporary files."
    if ($fileCount -gt 0) {
        $fileList | Remove-Item
    }
}
1
  • 1
    why do you even use invoke expression and dont run it directly? regarding the different execution times: powershell just starts the job and doesnt wait until its finished, it just carries on so your second command will be called before the assembler has finished its work i guess. also for a possible solution see my answer below Commented Oct 19, 2014 at 23:46

1 Answer 1

3

Try to start it with Start-Process like this:

Start-Process X:\asm\KCPSM6.exe -argumentlist @("-c4096", "main_KC705.psm") -wait

For a greater reference on how to start executables look here:

Technet

Sign up to request clarification or add additional context in comments.

4 Comments

you can redirect in and outputs with start-process, take a look at the parameters. Also for getting in and outputs in your powershell window add the -NoNewWindow parameter.
I removed my first comment because it's working now... Very strange ... I was sure that I tested all combinations before I posted this question to SF! Thanks for the -NoNewWindows parameter hint :)
Sure, no problem :) For the next time you can allways fall back to Get-Help cmdletname -full. The powershell help is very comprehensive and most commands have more options than could be described in a single answer.
There seams to be an issue with Start-Process. Start-Process with -Wait does not always return. KCPSM6.exe is already terminated (removed from taskmanager's process list).

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.