Open semancik opened 7 years ago
That's a very interesting idea @semancik. Definitely will look into adopting the code. It also ties nicely with a cleanup of the API I have in the works.
Hey @semancik. It's been some time, but I finally got around to integrating your suggestion. The result is in PR #66.
I've successfully tested it with a Windows 7 machine. There's a problem when running against Windows Server 2012 though. It's not taking any input. I've been able to replicate the problem with the windows' winrs
tool, which block in the same way. Not sure why it's misbihaving like this, but looks like a server-side issue.
The functionality is really useful, but if I can't get it to work on recent windows versions, then don't think it's worth including it in the winrm4j
library.
Any ideas what could be causing the problem?
I've got my version working very successfully on Windows Server 2012R2. Tested on several servers, different configurations. Seems to work well, even with CredSSP and long running powershell processes. I haven't tested your version yet. I'm afraid that I'm no windows expert or Microsoft fan. I have implemented this piece of ... ehm ... technology purely out of desperation. Therefore I have absolutely no idea what might be wrong in your case. I'm sorry.
That's useful to know, thanks.
I can confirm that both CredSSP and Send operation work on both Win2008 and Win2012. However for CredSSP to work on Win2008 an improved CredSSP implementation in HTTP client is needed: https://github.com/Evolveum/httpclient/tree/credssp
@semancik care to share what tool you use to test the async input with? I've been using variations of type con
to echo the input to output, but would be keen on trying other approaches on win2012.
@semancik I'm testing your branch against my vagrant Windows 2012R2 box. Here's the test I'm using. Is that how the API is supposed to be used? What I'm seeing is that execution blocks on the send
command. Can you try it against your windows box?
@Test(groups="Live")
public void testCustomStreams() throws Exception {
StringBuilder inBuff = new StringBuilder();
for (int i = 0; i < 1000; i++) {
inBuff.append("line " + i + "\r\n");
}
String result;
int exitCode;
WinRmClient.Builder builder = WinRmClient.builder("http://" + VM_HOST + ":5985/wsman", AuthSchemes.NTLM);
builder.credentials(VM_USER, VM_PASSWORD);
builder.disableCertificateChecks(true);
WinRmClient client = builder.build();
try {
Command command = client.commandAsync("type con");
command.send(inBuff.toString());
exitCode = command.receive();
result = command.getLastOut();
command.release();
} finally {
client.disconnect();
}
System.out.println("Exit code = " + exitCode);
System.out.println(result);
}
If that works for you can you also try the testCustomStreams test in https://github.com/cloudsoft/winrm4j/pull/66?
Once again I'm quite tight with available time. I'm sure that the code in my fork works. However, haven't had time to try any code from master yet. I'll have to find the time to put all this together later. I'm really sorry for this mess, but I cannot find the time now.
I have implemenation of the Send WS-Man operation. The code is here: https://github.com/Evolveum/winrm4j/tree/shell This operation can be used to send input to the running process. With a proper PowerShell magic it can be used to initialize PowerShell once and then send several commands to an initialized instance. I have also added support for semi-async execution of commands, so winrm4j can continuously receive command output and send input as the command executes. This is usually needed for Exchange scripts unless you want to suffer terrible penalty of exchange snap-in intialization. The sample code is here: https://github.com/Evolveum/playground/tree/master/powerhell However, I'm not going to create a pull request now. The code is a follow-up to my CredSSP changes which are now stuck: https://github.com/cloudsoft/winrm4j/issues/49 So I'm writing this as a note to anyone who would need the implementation of Send operation. Please go ahead and use the code from my fork.