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

How to output the return value in real time #174

Open
Centeryuhao opened this issue May 24, 2023 · 2 comments
Open

How to output the return value in real time #174

Centeryuhao opened this issue May 24, 2023 · 2 comments

Comments

@Centeryuhao
Copy link

Centeryuhao commented May 24, 2023

I try to use ping command
Need to wait for the execution to end before returning the result
How to return in real time?
tks

   client = Client(server='xxxx', username='xxxx', password='xxxx',ssl=False, connection_timeout=2)
   output, streams, had_errors = client.execute_ps("ping github.com")
   print(streams)
   print(output)

Actually, I need to call other scripts and return values in real time.

Using the ping command as an example

@bchen290
Copy link

bchen290 commented Nov 1, 2023

Hey, I was able to put together a hacky solution for this

with wsman, RunspacePool(wsman) as pool:
    ps = PowerShell(pool)
    ps.add_script(command)
    ps.add_cmdlet("Out-String").add_parameter("Stream")
    ps.begin_invoke()  # execute process in the background
    while ps.state == PSInvocationState.RUNNING:
        current_output = [str(s) for s in ps.output]
        if current_output:
            print("\n".join(current_output))
        ps.output = []
        ps.poll_invoke()  # update the output streams

@jborean93
Copy link
Owner

Essentially the above, the old pypsrp API only allows intermediate output with poll_invoke(). The new psrp API which is still in beta is a lot more flexible and can run things in a separate thread of asyncio context to do it a bit more easily.

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

3 participants