1

I'm wondering if it's possible to get a System.Management.Automation.PowerShell object from within a PowerShell console host, or Integrated Scripting Editor (ISE). I know that you can access the Runspace object by calling $Host.Runspace, but I need direct access to a PowerShell object (if it exists).

Would the only option be to create a new PowerShell object, and then call the code inside of that, or is there a way to access a pre-created PowerShell object using the $Host variable (or similar)?

http://msdn.microsoft.com/en-us/library/system.management.automation.powershell_members(v=vs.85).aspx

2
  • May I inquire what you intend to do with the PowerShell instance? Commented Mar 6, 2014 at 1:38
  • I need to inspect the PSDataStreams property on it. Commented Mar 6, 2014 at 1:43

2 Answers 2

2

Based on this ScriptLogger blog post by Oisin that uses PSDataStreams, I would say no. You need to create a PowerShell object [powershell]::create() and execute your script with the resulting object. Then, when you tap into the PSDataStreams collection, it will contain the various output from the executed script.

Sign up to request clarification or add additional context in comments.

Comments

0

If you goal is async execution, you probably want Powershell jobs.
Look into "start-job" and "invoke-command -asJob" if your goal is to spawn new shells/threads and execute code in them, then return data from that execution. Those commands will generate a runspace- you can return data from them if you simply invoke a variable at the end of their scriptblock, then use:

$Var = get-job|receive-job

Powershell will handle runspace management brilliantly with minimal work- if you are managing runspaces manually, you are probably doing something the hard way. Note that Invoke-command will allow you to execute these jobs on a remote machine- hugely powerful and efficient given the minimal amount of code you'll have to write to do it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.