How can I send command-line parameters to a program called from a PowerShell script?

Viewed 92

As I'm sure you will be aware after reading this question, I'm brand new to PowerShell.

I want to back up a PostgreSQL database every day automatically using the Windows task scheduler. The backup file needs to include the current date. I created a file named db_backup.bat and run it within the Task Scheduler. Here it is, broken into several lines for readability:

"C:/Program Files (x86)/PostgreSQL/9.3/bin\pg_dump.exe" 
--host localhost --port 5432 --username "postgres" 
--no-password  --format custom --verbose 
--file "c:/misc/restore_test_%DATE:~10,4%%DATE:~4,2%%DATE:~7,2%.backup" "Restore Test"

I hate the need to format the date like this. PowerShell has much cleaner ways to extract date information, so I'd like to do this from a PowerShell script.

So I created db_backup.ps1, containing this:

"C:/Program Files (x86)/PostgreSQL/9.3/bin/pg_dump.exe" --host localhost --port 5432 
--username postgres --no-password  --format custom --verbose 
--file "c:/misc/restore_test.backup" "Restore Test" >> c:/misc/restore.txt

When I run this from Powershell, I get an error complaining about an unexpected token named --host. PS thinks "host" is a parameter for PowerShell, but it's supposed to be a parameter for the pg_dump program.

I tried wrapping all of the pg_dump parameters into a single string, but then PS thinks the entire string is a parameter for itself instead of passing it as a parameter to pg_dump.

So, how do I send command-line parameters to pg_dump?

0 Answers
Related