Overthere - WinRmClient - Multiple Commands together
sejpalsaurabh opened this issue · comments
Saurabh Sejpal commented
I am trying to execute multiple commands using WinRmClient, it is giving an error but when I execute that command using cmd it works.
The command is
powershell -command "Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010"; "get-exchangeserver | select-object -property AdminDisplayVersion | format-list"
I have created a example simulation for the same, please check the same.
package com.xebialabs.overthere;
import com.xebialabs.overthere.cifs.winrm.WinRmClient;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URL;
import static com.xebialabs.overthere.cifs.CifsConnectionBuilder.*;
public class ExecutePowerShell {
public static void main(String[] args) throws IOException {
WinRmClient winRmClient = null;
try {
winRmClient = new WinRmClient("administrator", "password", new URL("http", "host", 5985, "/wsman"), null, 0);
winRmClient.setConnectionTimeout(60 * 1000);
winRmClient.setWinRmTimeout(DEFAULT_WINRM_TIMEOUT);
winRmClient.setWinRmEnvelopSize(WINRM_ENVELOP_SIZE_DEFAULT);
winRmClient.setWinRmLocale(WINRM_LOCALE_DEFAULT);
winRmClient.setHttpsCertTrustStrategy(WINRM_HTTPS_CERTIFICATE_TRUST_STRATEGY_DEFAULT);
winRmClient.setHttpsHostnameVerifyStrategy(WINRM_HTTPS_HOSTNAME_VERIFICATION_STRATEGY_DEFAULT);
winRmClient.setKerberosDebug(WINRM_KERBEROS_DEBUG_DEFAULT);
winRmClient.setKerberosTicketCache(WINRM_KERBEROS_TICKET_CACHE_DEFAULT);
winRmClient.setSoTimeout(60 * 1000);
winRmClient.createShell();
winRmClient.executeCommand("powershell -command \"Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010\";\"get-exchangeserver | select-object -property AdminDisplayVersion | format-list\"");
ByteArrayOutputStream out = new ByteArrayOutputStream();
ByteArrayOutputStream error = new ByteArrayOutputStream();
winRmClient.receiveOutput(out, error);
byte[] outputBytes = out.toByteArray();
byte[] errorBytes = error.toByteArray();
if (outputBytes != null && outputBytes.length > 0)
{
String output = new String(outputBytes, "UTF-8");
System.out.println(output);
}
else
{
String errorOutput = new String(errorBytes, "UTF-8");
System.out.println(errorOutput);
}
out.close();
error.close();
} finally {
if (null != winRmClient) {
winRmClient.deleteShell();
}
}
}
}
Vincent Partington commented
Are you running the same version of powershell.exe
in both cases? E.g. 32-bit vs. 64-bit?
Saurabh Sejpal commented
No I am executing powershell command using command prompt, not powershell, but I have checked that it is working for 64 bit Powershell and not working for 32 bit.