runspace

ImportPSModule Failure Detection

依然范特西╮ 提交于 2020-01-11 09:21:06
问题 I am trying to use InitialSessionState.ImportPSModule in order to import a Powershell module. I am interested in knowing if importing of the module failed due to any reason (e.g file not found etc.). Putting such code in the try block does not raise an exception in the case of failure and the function seems to fail silently and continue if it is not able to import the module. Is there a way to be alerted in the code if the import fails? I am trying to do something like the following. In the

How to access a different powershell runspace without WPF-object

瘦欲@ 提交于 2020-01-06 12:49:27
问题 I worked quite a bit with PowerShell runspaces and I know it's possible to invoke commands in another runspace by using a dispatcher from WPF-objects. I create my runspaces like this: $global:A = [hashtable]::Synchronized(@{}) $global:initialSessionState = [System.Management.Automation.Runspaces.InitialSessionState]::CreateDefault() # Add some functions to the ISS $GuiRunspace =[runspacefactory]::CreateRunspace($initialSessionState) $GuiRunspace.ApartmentState = "STA" $GuiRunspace.Open()

Run a Remote Powershell Script on a Different Remote Server in a C# Runspace

跟風遠走 提交于 2019-12-24 15:45:01
问题 I need to be able to execute a PS1 script that resides on a remote machine against another remote machine through a C# runspace. To be clear what I mean by this: The service I'm creating resides on server A. It creates a remote runspace to server B using the method below. Through the runspace I'm trying to call a script residing on server C against server B. If it helps, currently server A IS server C, but it's not guaranteed that will always be the case. Here's the method I'm using to make

How to call outside defined function in runspace scriptblock

送分小仙女□ 提交于 2019-12-24 09:10:59
问题 I have a complex PowerShell function which I would like to run another threads. However, If I'm right, the function cannot be accessed in the scriptblock. I want to avoid to copy every related function next to it. Is there any way or method to call the function in the scriptblock? function Function1 { Param() Process { $param1 = "something" $pool = [RunspaceFactory]::CreateRunspacePool(1, [int]$env:NUMBER_OF_PROCESSORS + 1) $pool.ApartmentState = "MTA" $pool.Open() $runspaces = @()

Expand variable with scriptblock inside variable in a loop with runspaces

一曲冷凌霜 提交于 2019-12-23 23:05:24
问题 $RunspaceCollection = @() $RunspacePool = [RunspaceFactory]::CreateRunspacePool(1,5) $RunspacePool.Open() $code = @({'somecode'},{'someothercode'}) Foreach ($test in $case) { $finalcode= { Invoke-Command -ScriptBlock [scriptblock]::create($code[$test]) }.GetNewClosure() $Powershell = [PowerShell]::Create().AddScript($finalcode) $Powershell.RunspacePool = $RunspacePool [Collections.Arraylist]$RunspaceCollection += New-Object -TypeName PSObject -Property @{ Runspace = $PowerShell.BeginInvoke()

C# Runspace Powershell (Interactive)

匆匆过客 提交于 2019-12-22 06:46:37
问题 I am trying with C# to execute a Powershell file with paramters in a runspace. Unfortunately i get the following output: A command that prompts the user failed because the host program or the command type does not support user interaction. Try a host program that supports user interaction, such as the Windows PowerShell Console or Windows PowerShell ISE, and remove prompt-related commands from command types that do not support user interaction, such as Windows PowerShell workflows. What could

Write PowerShell Output (as it happens) to WPF UI Control

一世执手 提交于 2019-12-18 09:23:40
问题 I've been reading blogs about writing to the UI from different runspaces (http://learn-powershell.net/2012/10/14/powershell-and-wpf-writing-data-to-a-ui-from-a-different-runspace/). I'm basically trying to make it so I can click a button in the UI and run a PowerShell script and capture the output of that script as it happens and update the WPF UI control without freezing up the UI. I've tried a basic example of just writing some output directly, but it seems to hang the UI. I'm using

Piping ListView items in separate runspace

烈酒焚心 提交于 2019-12-17 21:27:28
问题 I have a form written in PowerShell (as it uses PS to run commands over hundreds of servers) but this bit had me stumped for awhile: $listview.Invoke([System.Action[String]]{ Param ( [String]$data ) write-warning "1" $LVitem = ($LVresults.Items | Where { $_.Name -eq "Test Account" }) write-warning "2" $LVitem.Tag.Output += $data + "`n" }, "Testing") It's in a separate runspace that runs an Invoke-Command to a specific server and pipes the output to this code block. Now if I run the Where

Import a Powershell Module on a Remote Server in a C# Runspace

情到浓时终转凉″ 提交于 2019-12-14 02:38:09
问题 I need to be able to import a module that resides on a remote machine through a C# runspace. To be clear what I mean by this: The service I'm creating resides on server A. It creates a remote runspace to server B using the method below. Through the runspace I'm trying to import a module on server B. Here's the method I'm using to make the remote call: internal Collection<PSObject> RunRemoteScript(string remoteScript, string remoteServer, string scriptName, out bool scriptSuccessful) { bool

SessionStateProxy Variable with Runspace Pools

一笑奈何 提交于 2019-12-13 02:39:24
问题 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 do the same with a RunspacePool? 回答1: It is a little more involved to add a variable to a runspacepool, but still definitely doable. You will need to create an InitialSessionState object and then create a SessionStateVariableEntry object that