Skip to content

Commit

Permalink
Adds config to handle remote commands. See issues #10 and #11
Browse files Browse the repository at this point in the history
  • Loading branch information
Javier Garcia authored and Javier Garcia committed Oct 12, 2016
1 parent bd5ad47 commit 74b10eb
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/main/java/com/profesorfalken/jpowershell/PowerShell.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public class PowerShell {
private int maxThreads = 3;
private int waitPause = 10;
private long maxWait = 10000;
private boolean remoteMode = false;

//Variables for script mode
private boolean scriptMode = false;
Expand Down Expand Up @@ -95,6 +96,7 @@ public PowerShell configuration(Map<String, String> config) {
this.maxThreads = Integer.valueOf((config != null && config.get("maxThreads") != null) ? config.get("maxThreads") : PowerShellConfig.getConfig().getProperty("maxThreads"));
this.waitPause = Integer.valueOf((config != null && config.get("waitPause") != null) ? config.get("waitPause") : PowerShellConfig.getConfig().getProperty("waitPause"));
this.maxWait = Long.valueOf((config != null && config.get("maxWait") != null) ? config.get("maxWait") : PowerShellConfig.getConfig().getProperty("maxWait"));
this.remoteMode = Boolean.valueOf((config != null && config.get("remoteMode") != null) ? config.get("remoteMode") : PowerShellConfig.getConfig().getProperty("remoteMode"));
} catch (NumberFormatException nfe) {
Logger.getLogger(PowerShell.class.getName()).log(Level.SEVERE, "Could not read configuration. Use default values.", nfe);
}
Expand Down Expand Up @@ -157,6 +159,10 @@ public PowerShellResponse executeCommand(String command) {

Future<String> result = threadpool.submit(commandProcessor);
Future<String> resultError = threadpool.submit(commandProcessorError);

if (this.remoteMode) {
command = completeRemoteCommand(command);
}

//Launch command
commandWriter.println(command);
Expand Down Expand Up @@ -325,4 +331,8 @@ private void waitUntilClose(Future<String> task) throws InterruptedException {
closingTime += this.waitPause;
}
}

private String completeRemoteCommand(String command) {
return command + ";Write-Host \"\"";
}
}
3 changes: 2 additions & 1 deletion src/main/resources/jpowershell.properties
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@

maxThreads=3
waitPause=10
maxWait=10000
maxWait=10000
remoteMode=false
19 changes: 19 additions & 0 deletions src/test/java/com/profesorfalken/jpowershell/PowerShellTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,25 @@ public void testTimeout() throws Exception {

}
}

//@Test
public void testRemote() throws Exception {
System.out.println("testRemote");
if (OSDetector.isWindows()) {
PowerShell powerShell = PowerShell.openSession();
Map<String, String> config = new HashMap<String, String>();
config.put("remoteMode", "true");
PowerShellResponse response = powerShell
.configuration(config)
.executeCommand("Invoke-command -ComputerName leon {(Get-Service W32Time).WaitForStatus('Running','02:00:00')}");

System.out.println("Output:" + response.getCommandOutput());

Assert.assertFalse(response.isError());

powerShell.close();
}
}

@Test
public void testLongScript() throws Exception {
Expand Down

0 comments on commit 74b10eb

Please sign in to comment.