Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overthere - WinRmClient - Multiple Commands together #173

Open
sejpalsaurabh opened this issue May 2, 2016 · 2 comments
Open

Overthere - WinRmClient - Multiple Commands together #173

sejpalsaurabh opened this issue May 2, 2016 · 2 comments

Comments

@sejpalsaurabh
Copy link

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"

Command Line Execution
exchange

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();
            }
        }
    }

}
@vpartington
Copy link
Contributor

Are you running the same version of powershell.exe in both cases? E.g. 32-bit vs. 64-bit?

@sejpalsaurabh
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants