/** * Executes a command on a remote shell by communicating with the WinRM server from the remote host. * Method creates a shell, runs a command on the shell, waits for the command execution to finnish, retrieves the result then deletes the shell. * * @param wsManRequestInputs * @return a map with the result of the command and the exit code of the command execution. * @throws RuntimeException * @throws IOException * @throws InterruptedException * @throws ParserConfigurationException * @throws TransformerException * @throws XPathExpressionException * @throws TimeoutException * @throws URISyntaxException * @throws SAXException */ public Map<String, String> runCommand(WSManRequestInputs wsManRequestInputs) throws RuntimeException, IOException, InterruptedException, ParserConfigurationException, TransformerException, XPathExpressionException, TimeoutException, URISyntaxException, SAXException { HttpClientService csHttpClient = new HttpClientService(); HttpClientInputs httpClientInputs = new HttpClientInputs(); URL url = buildURL(wsManRequestInputs, WSMAN_RESOURCE_URI); httpClientInputs = setCommonHttpInputs(httpClientInputs, url, wsManRequestInputs); String shellId = createShell(csHttpClient, httpClientInputs, wsManRequestInputs); WSManUtils.validateUUID(shellId, SHELL_ID); String commandStr = WSManUtils.constructCommand(wsManRequestInputs); String commandId = executeCommand(csHttpClient, httpClientInputs, shellId, wsManRequestInputs, commandStr); WSManUtils.validateUUID(commandId, COMMAND_ID); Map<String, String> scriptResults = receiveCommandResult(csHttpClient, httpClientInputs, shellId, commandId, wsManRequestInputs); deleteShell(csHttpClient, httpClientInputs, shellId, wsManRequestInputs); return scriptResults; }
/** * Executes a command on a remote shell by communicating with the WinRM server from the remote host. * Method creates a shell, runs a command on the shell, waits for the command execution to finnish, retrieves the result then deletes the shell. * * @param wsManRequestInputs * @return a map with the result of the command and the exit code of the command execution. * @throws RuntimeException * @throws IOException * @throws InterruptedException * @throws ParserConfigurationException * @throws TransformerException * @throws XPathExpressionException * @throws TimeoutException * @throws URISyntaxException * @throws SAXException */ public Map<String, String> runCommand(WSManRequestInputs wsManRequestInputs) throws RuntimeException, IOException, InterruptedException, ParserConfigurationException, TransformerException, XPathExpressionException, TimeoutException, URISyntaxException, SAXException { CSHttpClient csHttpClient = new CSHttpClient(); HttpClientInputs httpClientInputs = new HttpClientInputs(); URL url = buildURL(wsManRequestInputs, WSMAN_RESOURCE_URI); httpClientInputs = setCommonHttpInputs(httpClientInputs, url, wsManRequestInputs); String shellId = createShell(csHttpClient, httpClientInputs, wsManRequestInputs); WSManUtils.validateUUID(shellId, SHELL_ID); String commandStr = POWERSHELL_SCRIPT_PREFIX + " " + EncoderDecoder.encodeStringInBase64(wsManRequestInputs.getScript(), Charsets.UTF_16LE); String commandId = executeCommand(csHttpClient, httpClientInputs, shellId, wsManRequestInputs, commandStr); WSManUtils.validateUUID(commandId, COMMAND_ID); Map<String, String> scriptResults = receiveCommandResult(csHttpClient, httpClientInputs, shellId, commandId, wsManRequestInputs); deleteShell(csHttpClient, httpClientInputs, shellId, wsManRequestInputs); return scriptResults; }