2

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?

1 Answer 1

3

Don't define your commandline as a string (unless you want to run it via Invoke-Expression, which I wouldn't recommend in most cases). Simply use the call operator (&) and put your arguments like you do in the console:

& python.exe C:\backup_win.py -p "QX+qos5eu1D36&kB" -f D:\pgbackups\torwpinf002_postgresdb_bk_20160715_1058.bak

If your arguments vary you can put them in an array and splat it when calling python.exe:

$params = 'C:\backup_win.py', '-p', "QX+qos5eu1D36&kB",
          '-f', 'D:\pgbackups\torwpinf002_postgresdb_bk_20160715_1058.bak'

& python.exe @params
Sign up to request clarification or add additional context in comments.

5 Comments

That way would work, but the problem is I have to build the command dynamically so the paths have to be put into strings.. and when I built the string with the right values it bombs out.. (almost like it can't fin the python.exe or .py script) but from the command line in the console it works without an issue..
ok, that is strange.. your way worked.. i just replaced the dynamic path and file name var like this.. $params = $backupscriptFile, '-p', "QX+qos5eu1D36&kB", '-f', $fullfileSave & python.exe @params and it worked like a charm. Now what is really driving me crazy is why the script i wrote before works on a 2008 without and issues, but I have to change it like above to run on a windows 2012... damn you windows!!
so let me ask you, should I run the script the same way on the 2008 box? What i mean, is that the preferred way to run commands like this within powershell? (standardize coding )
Yes, it's the preferred way.
Thank you for your time and assistance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.