I need your help to solve my problem. I want to launch a powershell command using subprocess. However the command used will be very long, so it will be bigger than the size of the buffer. I have done many tests and right now I'm not able to find a good solution.
I would like to split my big buffer (it's a base64 content) into small buffers but I don't know how to do using subprocess.
For example, here is my subprocess call:
a = "a" * 32716
command = ["powershell.exe", "/c", "$base64='%s'\n" % a, "Write-Host $base64"]
process = subprocess.Popen(command, stderr=subprocess.STDOUT, stdin=subprocess.PIPE, universal_newlines=True)
command = ["powershell.exe", "/c", "$base64=" + small_buffer1, "$base64=$base64+" small_buffer2, etc.]
subprocess.Popen(["powershell.exe", "/c", "$base64='%s'\n" % a], stderr=subprocess.STDOUT, stdin=subprocess.PIPE)
# $base64 will be empty
subprocess.Popen(["powershell.exe", "/c", "Write-Host $base64"], stderr=subprocess.STDOUT, stdin=subprocess.PIPE)
The solution that I've found is easy to implement. The goal is to keep the subprocess open and to write the buffer in stdin. Here is an example:
# keep the pipe open p = subprocess.Popen(cmd, stdout=PIPE, stderr=PIPE, stdin=PIPE, shell=True) # initialize the powershell value p.stdin.write("$data=\"\""+"\n") # split the buffer to small buffers size = 25000 tab = [veryLongLine[i:i+size] for i in range(0, len(veryLongLine), size)] for t in tab: # concatenate small buffers to obtain the "veryLongLine" value p.stdin.write("$data+=\"%s\"\n" % t) # flush the buffer every time (the $data value is not flushed) p.stdin.flush() # write the output p.stdin.write("Write-Host $data\n")
Hope it will help !