How to run a line in Powershell in Python 2.7? -


i have line of powershell script runs fine when enter in powershell's command line. in python application run powershell, trying send line of script powershell.

powershell -command ' & {. ./uploadimagetobigcommerce.ps1; process-image '765377' '.jpg' 'c:\images' 'w:\product_images\import'}' 

i know script works because i've been able implement on own powershell command line. however, haven't been able python send line shell without getting "non-zero exit status 1."

import subprocess  product = "765377" scriptpath = "./uploadimagetobigcommerce.ps1"  def process_image(sku, filetype, searchdirectory, destinationpath, scriptpath):     psargs = "process-image '"+sku+"' '"+filetype+"' '"+searchdirectory+"' '"+destinationpath+"'"     subprocess.check_call([create_psscript_call(scriptpath, psargs)], shell=true)  def create_psscript_call(scriptpath, args):     line = "powershell -command ' & {. "+scriptpath+"; "+args+"}'"     print(line)     return line  process_image(product, ".jpg", "c:\images", "c:\webdav", scriptpath) 

does have ideas help? i've tried:

  • subprocess.check_call()
  • subprocess.call()
  • subprocess.popen()

and maybe syntax issue, haven't been able find enough documentation confirm that.

using single quotes inside single quoted string breaks string. use double quotes outside , single qoutes inside or vice versa avoid that. statement:

powershell -command '& {. ./uploadimagetobigcommerce.ps1; process-image '765377' '.jpg' 'c:\images' 'w:\product_images\import'}' 

should rather this:

powershell -command "& {. ./uploadimagetobigcommerce.ps1; process-image '765377' '.jpg' 'c:\images' 'w:\product_images\import'}" 

also, i'd use subprocess.call (and quoting function), this:

import subprocess  product    = '765377' scriptpath = './uploadimagetobigcommerce.ps1'  def qq(s):     return "'%s'" % s  def process_image(sku, filetype, searchdirectory, destinationpath, scriptpath):     pscode = '. ' + scriptpath + '; process-image ' + qq(filetype) + ' ' + \              qq(searchdirectory) + ' ' + qq(destinationpath)     subprocess.call(['powershell', '-command', '& {'+pscode+'}'], shell=true)  process_image(product, '.jpg', 'c:\images', 'c:\webdav', scriptpath) 

Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -