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?

  • 1
    What's wrong with just invoking it exactly like you would have with the batch file? – Jeff Zeitlin Oct 25 '17 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. – Ari Winokur Oct 25 '17 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`. – Jeff Zeitlin Oct 25 '17 at 14:40
  • I'll give it a try and report back. – Ari Winokur Oct 25 '17 at 14:42
  • 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: https://stackoverflow.com/questions/25187048/run-executable-from-powershell-script-with-parameters – Bryce McDonald Oct 25 '17 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"`. – Ansgar Wiechers Oct 25 '17 at 15:02

1 Answers1

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.

Rain
  • 153
  • 5
  • 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. – Ari Winokur Nov 07 '17 at 18:32