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

[Question] How can i execute a powershell command and handle an output? #54

Open
AlexandrLisitsa opened this issue Jan 31, 2024 · 1 comment

Comments

@AlexandrLisitsa
Copy link

` public static String executePowerShellCommand(String remoteComputer, String username, String password, String command) {
ComThread.InitSTA(); // Initialize the COM threading model

    try {
        // Create the WMI locator object
        ActiveXComponent wmiLocator = getWmiConnection(remoteComputer,username,password);
        Dispatch wmiObject = wmiLocator.getObject();

        // Execute the PowerShell command using Win32_Process
        String processCommand = "powershell.exe -Command ping google.com";
        // Execute the PowerShell command using Win32_Process
        Dispatch process = Dispatch.call(wmiObject, "Get", "Win32_Process").toDispatch();
        Variant create = Dispatch.call(process, "Create", processCommand);
        
        // How can i handle the output of this command?

        return "result";
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    } finally {
        ComThread.Release(); // Release the COM threading model
    }

}`

Hi everyone! I'm trying to handle the output of this Variant create = Dispatch.call(process, "Create", processCommand); line of code.
How can i do this?

@EJP286CRSKW
Copy link

EJP286CRSKW commented Mar 19, 2024

Win32_Process.Create() only returns the process ID. It doesn't give you the output of the process it started.

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