I am trying to invoke a Python script that accepts arguments to backup a PostgreSQL database in VMware.
Code is like this:
# path to where you want to save the backup to
$filepath = "D:\pgbackups\"
# filename you want to call the backup
$filename = "torwpinf002_postgresdb_bk_"
# format the date/time so you can append to the backup filename
$filedate = get-date -Format "yyyyMMdd_HHmm"
# pack it all together
$fullFileSave = $filepath + $filename + $filedate.ToString() + ".bak"
$BackupScriptFile = "C:\backup_win.py"
$executebackup = "`"$BackupScriptFile`"" + ' -p "QX+qos5eu1D36&kB" -f ' + "`"$fullFileSave`""
python $executebackup
If I throw a breakpoint I can display the variable $executebackup and it will show this:
"C:\backup_win.py " -p "QX+qos5eu1D36&kB" -f "D:\pgbackups\torwpinf002_postgresdb_bk_20160715_1050.bak"
This throws the error:
python : usage: backup_win.py [-h] -p PASSWORD -f BACKUP_FILE
At C:\Program Files\VMware\vCenter Server\python\torwpinf002_pgbackup.ps1:39 char:1
+ python $executebackup
+ ~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (usage: backup_w... -f BACKUP_FILE:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
backup_win.py: error: argument -p is required
What is funny about this is, the box I am running this one is Windows 2012 R2. This SAME script works perfect on a Windows 2008 R2.
Huh?
Well, so I think what is happening is the double quotes on the command I am passing into the python.exe is mucking up the works, so I changed it a bit:
$executebackup = $BackupScriptFile + ' -p "QX+qos5eu1D36&kB" -f ' + $fullFileSave
resulting in the output of this if I echo it to the screen (notice no quotes around the file paths):
C:\backup_win.py -p "QX+qos5eu1D36&kB" -f D:\pgbackups\torwpinf002_postgresdb_bk_20160715_1050.bak
but that returns this error:
python.exe : C:\Program Files\VMware\vCenter Server\python\python.exe: can't open file 'C:\backup_win.py -p QX+qos5eu1D36&kB -f
D:\pgbackups\torwpinf002_postgresdb_bk_20160715_1050': [Errno 22] Invalid argument
At line:1 char:1
+ python.exe $executebackup
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (C:\Program File...nvalid argument:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
I am really stuck, I am sure it is something simple that the experts can adjust in 5 seconds.. my brain is cooked and I am not sure what to try next. The part that bugs me the most is that it works fine on a Windows 2008 but not on 2012.
Here is something really funny. If I take the output of the $executebackup:
C:\backup_win.py -p "QX+qos5eu1D36&kB" -f D:\pgbackups\torwpinf002_postgresdb_bk_20160715_1058.bak
and paste it into the PowerShell console it works just fine.
PS C:\Users\Administrator> C:\backup_win.py -p "QX+qos5eu1D36&kB" -f D:\pgbackups\torwpinf002_postgresdb_bk_20160715_1058.bak Backup completed successfully.
Any thoughts on how to get this to execute from within the script?