Questions tagged [runspace]

In the context of using PowerShell commands with managed code a runspace is the operating environment for the commands invoked by the host application.

This environment includes the commands and data that are currently present, and any language restrictions that currently apply.Host applications can use the default runspace that is provided by Windows PowerShell, or they can create their own runspaces by using the RunspaceFactory class. By using that class, The host application can create individual runspaces or they can create a pool of runspaces. Once a runspace is created and opened, the host application can invoke commands synchronously or asynchronously by using a PowerShell object. For more information see Writing a Windows PowerShell Host Application

283 questions
3
votes
1 answer

Runspace for button event in powershell

Not sure if this is a duplicate, checked online, and worked with what I found, Working with Boe Prox's solutions, which from another StackOverflow article references (https://stackoverflow.com/a/15502286/1546559), but in his, he is updating from a…
user1546559
  • 49
  • 10
3
votes
1 answer

Unable to execute PowerShell script from inside App Directory C#/.net Core

I'm not quite sure why, but if I execute a script from a local folder outside my solution things run fine. When I call the file inside my project I get the following error: System.Management.Automation.PSSecurityException: 'AuthorizationManager…
nkasco
  • 326
  • 1
  • 2
  • 13
3
votes
1 answer

Powershell scripts invoked by C# application

I have this powershell script (test1.ps1) calling another powershell script(test2.ps1) to do the job. Both scriptfiles are in the same folder test1.ps1 echo "from test1.ps1" .\test2.ps1 test2.ps1 echo "from test2.ps1" When I invoke test1.ps1 in…
kannan
  • 41
  • 2
3
votes
0 answers

How to get current runspace of RunspacePool

I have multiple commands run in parallel in separate runspaces, managed by a RunspacePool. How can I determine the runspace each command is running in? param ( $MaxRunspaces = 4, $JobCount = 20 ) try { $pool =…
marsze
  • 15,079
  • 5
  • 45
  • 61
3
votes
3 answers

C# Powershell in Runspace - How can i get "Format-List" to work?

Im currently writing a Contact Manager for our Exchange Online Tenant in C# using the Powershell-Commands and Runspaces from "System.Management.Automation" and "System.Management.Automation.Runspaces" respectively. Its working fine for adding…
Steffen
  • 33
  • 3
3
votes
1 answer

Error running PS in .NetCore app - Cannot perform operation because the runspace is not in the 'Opened' state. Current state of runspace is 'Broken'

I'm trying to implement a .NetCore 3.1 console app which needs to run PowerShell scripts. However, when I try run it, I get the following error - Cannot perform operation because the runspace is not in the 'Opened' state. Current state of runspace…
Siva
  • 43
  • 4
3
votes
1 answer

The RunSpace and its closure

While working with a script that uses a RunSpace, I found that it takes up more and more system memory. As far as I understand, this is due to the fact that open RunSpace do not close when completed. They remain in memory, accumulating…
3
votes
0 answers

Configure launch.json for VSCode to execute PowerShell commands in the debug session

Is it possible to create an entry in the launch.json, which executes a PowerShell script or scriptblock, in the same runspace as the to-be-launched and debugged script? The reason is, I would like to reset the runspace (remove variables + background…
Rob
  • 472
  • 4
  • 17
3
votes
0 answers

Do I need additional runspace clean up after killing parent process?

I have a very limited understanding of how resource management/garbage collection and similar things work in powershell. My question is: is it okay to kill a process that has a running runspace? start.ps1 $moduleOnePath = Join-Path $cwd…
manidos
  • 3,244
  • 4
  • 29
  • 65
3
votes
1 answer

Register-ObjectEvent for runspace in an arraylist

I am creating a runspace pool that will have multiple runspaces starting an stopping at different times. To keep track of this all of my runspaces go into a collection. I am trying to register an object event for each one so that I can return…
Lister
  • 1,098
  • 1
  • 9
  • 19
3
votes
2 answers

Expand variable with scriptblock inside variable in a loop with runspaces

$RunspaceCollection = @() $RunspacePool = [RunspaceFactory]::CreateRunspacePool(1,5) $RunspacePool.Open() $code = @({'somecode'},{'someothercode'}) Foreach ($test in $case) { $finalcode= { Invoke-Command -ScriptBlock…
4c74356b41
  • 69,186
  • 6
  • 100
  • 141
3
votes
1 answer

SessionStateProxy Variable with Runspace Pools

I wanted to use a Runspace Pool in PowerShell to perform Background actions. But I need to access the WPF Window Variable from the Main Thread. Normal Runspaces have the option: $runspace.SessionStateProxy.SetVariable('xamGUI',$xamGUI) But how do I…
MicroScripter
  • 148
  • 2
  • 9
3
votes
0 answers

Impersonating in C# not working for runspace (powershell)

I have a problem with impersonating a user in C#: I impersonate with the known funtion used in thousands of examples: using(Impersonator impClass = new Impersonator(_domain, _userName, _password)) ... After impersonation I run…
3
votes
2 answers

Is it possible to a get a list of the currently defined functions in a powershell runspace?

I know that I can get the commands that were defined initially by using something like: Runspace rs = RunspaceFactory.CreateRunspace(); rs.InitialSessionsState.Commands or in PowerShell…
briantist
  • 45,546
  • 6
  • 82
  • 127
3
votes
2 answers

C# PowerShell with RunspacePool - How to import Exchange Cmdlets like Get-Mailbox?

I am currently building a Windows-Service that will execute PowerShell Commands wenn some request comes in. There will be lots of requests comming in so I adopted the code from: Using Powershell RunspacePool multithreaded to remote server from C# I…
Gope
  • 1,672
  • 1
  • 13
  • 19
1 2
3
18 19