How to Retrieve Results from Invoke-Command with Start-Process in PowerShell?

0
17
Asked By TechSquirrel82 On

I'm trying to troubleshoot a PowerShell script that uses `Invoke-Command` with `Start-Process`, but I'm having trouble retrieving the results from the script block. I have the following functions in my script to set up a server and client for network latency testing using ntttcp. Despite validating that the `ntttcp.exe` is running on the server, I can't seem to capture the output back to the client. Any guidance on how to enhance my script to get the expected results would be greatly appreciated!

3 Answers

Answered By CodingNinja33 On

It looks like your script block isn't set up to wait for the process to finish. You should add the `-Wait` parameter to the `Start-Process` command. This way, it will hold off on proceeding until the `ntttcp.exe` execution is complete, which might resolve the output issues you're facing.

TechSquirrel82 -

Right, I see what you're saying. The `Start-ntttcpServer` function indeed needs the `-Wait` flag on the `Start-Process` for it to finish properly before moving to the client function. Thanks for the tip!

Answered By PowerShellMaster On

Just a heads up, your pipeline inputs in the Begin block might lead to inconsistent behavior, depending on how you call those functions. It’s a good idea to rethink how you manage those sessions, either by closing them properly or keeping them alive for the duration you need to minimize overhead. Also, consider avoiding `Write-Host` and instead output PowerShell objects for better logging and handling later.

TechSquirrel82 -

Thanks for the feedback! I'm aiming to automate a manual process and will definitely re-evaluate how I'm managing sessions and outputs in my script.

Answered By NetworkGuru88 On

You're missing a return value from the `Invoke-Command`. When you run the `Invoke-Command`, try assigning it to a variable. This will allow you to capture any output data you might need later. You might want to check out some resources on collecting return values from `Invoke-Command` for better results.

TechSquirrel82 -

That makes sense! I didn't think about assigning that output to a variable. I’ll give it a try and see if that helps.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.