def _exec_psrp_script(self, script, input_data=None, use_local_scope=True, arguments=None): # Check if there's a command on the current pipeline that still needs to be closed. if self._last_pipeline: # Current pypsrp versions raise an exception if the current state was not RUNNING. We manually set it so we # can call stop without any issues. self._last_pipeline.state = PSInvocationState.RUNNING self._last_pipeline.stop() self._last_pipeline = None ps = PowerShell(self.runspace) ps.add_script(script, use_local_scope=use_local_scope) if arguments: for arg in arguments: ps.add_argument(arg) ps.invoke(input=input_data) rc, stdout, stderr = self._parse_pipeline_result(ps) # We should really call .stop() on all pipelines that are run to decrement the concurrent command counter on # PSSession but that involves another round trip and is done when the runspace is closed. We instead store the # last pipeline which is closed if another command is run on the runspace. self._last_pipeline = ps return rc, stdout, stderr
def _exec_psrp_script(self, script, input_data=None, use_local_scope=True, force_stop=False, arguments=None): ps = PowerShell(self.runspace) ps.add_script(script, use_local_scope=use_local_scope) if arguments: for arg in arguments: ps.add_argument(arg) ps.invoke(input=input_data) rc, stdout, stderr = self._parse_pipeline_result(ps) if force_stop: # This is usually not needed because we close the Runspace after our exec and we skip the call to close the # pipeline manually to save on some time. Set to True when running multiple exec calls in the same runspace. # Current pypsrp versions raise an exception if the current state was not RUNNING. We manually set it so we # can call stop without any issues. ps.state = PSInvocationState.RUNNING ps.stop() return rc, stdout, stderr