0

In Windows Powershell, I want to make a script that runs a bunch of commands in succession.

Like so:

mongo a.js > aout.txt
mongo b.js > bout.txt
...

I'm using PowerShell to get access to the linux like > command. But I can't figure out how to easily write this script. I assume it's trivial. I tried writing a batch script but that doesn't work, due to > isn't supported.

How would you do it?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Snæbjørn
  • 10,322
  • 14
  • 65
  • 124
  • `1..1000 | % {mongo "$_.js" | Out-File -Path "C:\data\$_.txt"}` Not Tested. Do you have a file naming scheme? I thought for sure '>','>>','2>','2>>',... were supported in bat files. I even have bat scripts that use them. Check the Mongo exe to see if it supports file output. – E.V.I.L. May 23 '13 at 14:43
  • 1
    Redirection (>) most definitely is supported in batch files. – EBGreen May 23 '13 at 14:46
  • Odd I did `START /WAIT mongo a.js > a.txt` newline `START /WAIT mongo b.js > b.txt` which didn't work :( and yes mongo support output, it works when I do it manually – Snæbjørn May 23 '13 at 15:09
  • 1
    lose the start /wait. Executables don't need start and command line executables already block until the exe is finished – Lars Truijens May 23 '13 at 20:00

1 Answers1

1

Create a file with a .ps1 extension. Put this in the file:

param($path = $pwd)
mongo $path\a.js > $path\aout.txt
mongo $path\b.js > $path\bout.txt

The above assumes mongo is in your path. Now make sure you have execution policy set to allow scripts to run. Execute this in PowerShell (running as admin):

Set-ExecutionPolicy RemoteSigned # you can also use Unrestricted if you'd like

Then execute your script e.g.:

C:\> C:\mymongo\mongo-it.ps1 C:\mymongo
Keith Hill
  • 194,368
  • 42
  • 353
  • 369