0

I am working on converting an old massive batch file logging script to Powershell. Creating the data has not been a major issue, but I am having an issue with the final step. The final step in the old batch file converts an ugly CSV into a more pretty and easy-on-the-eyes CSV. This is done using an old AWK script and a gawk command. I am content leaving that step in place, but I want it to be executed from within the Powershell script.

The command looks something like this:

<gawk.exe> -f <path>\PrepareReport.awk <original.csv> >> <final.csv>

I have tried different ways of calling gawk and the arguments. Invoke-Expression, Invoke-Command, etc. Nothing seems to work to run this command properly. Any insight or ideas would be appreciated.

Thanks!

EDIT: Following all the comments, I have found the & is the option that works to make this happen. The Start-Process cmdlet causes a fatal error. One weird thing is still happening though. For some reason the resulting CSV file does not display normally in Excel. Even though the resulting file seems to be perfectly normal, Excel won't display it using the appropriate comma breaks. Any thoughts?

6
  • 1
    What's wrong with just invoking it exactly like you would have with the batch file? Commented Oct 25, 2017 at 14:35
  • @JeffZeitlin are you suggesting just writing the command out? Last I checked you can't write out a regular command in a Powershell script. Commented Oct 25, 2017 at 14:36
  • 4
    That's exactly what I'm suggesting; you can write out regular commands in PowerShell scripts. If the executable for gawk is in your $env:PATH, it should work just fine as you've given it above. If it's not, you should use the full path\to\gawk. We're running a PowerShell script here that has to invoke the SysInternals PSEXEC on remote computers; PSEXEC runs locally, and the command is simply the same as it would be with CMD.EXE. Commented Oct 25, 2017 at 14:40
  • 1
    @JeffZeitlin is correct - you'll either need to add it to your environments path, "dot source" the file, or use the "Start-Process" cmdlet. Check out this link here: stackoverflow.com/questions/25187048/… Commented Oct 25, 2017 at 14:50
  • 1
    @AriWinokur I don't know what you have tested, but PowerShell can usually run external commands just fine. Use the call operator if your command is a string rather than a bare word: & "gawk.exe" -f "PrepareReport.awk" "input.csv" >> "output.csv". Commented Oct 25, 2017 at 15:02

1 Answer 1

0

You could always use Start-Process.

Start-Process -NoNewWindow -Wait -PassThru -FilePath <gawk.exe> -ArgumentList "-f <path>\PrepareReport.awk <original.csv> >> <final.csv>"

It's a little wordy, but very customisable and very flexible.

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

1 Comment

thanks for the Start-Process idea, but it does not work. The script moves right on past it and does not create the file. I was getting a fatal error at first, but with some adjustment that is not happening anymore.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.