Isn't Msf::Post::Common.cmd_exec having into account meterpreter vs shell sessions? Is there something wrong with Msf::Post::Common.cmd_exec so you need to make it by yourself here?