Running same powershell script multiple asynchronous times with separate runspace
By : Josh
Date : March 29 2020, 07:55 AM
will be helpful for those in need I have absolutely no clue what you're trying to do, and to be honest, I don't even understand the question. However, if you're trying to run several instances of the same script asynchronously in different runspaces (for reasons only you know best, sorry) then I can point you to a script I wrote some time ago. This is for v2 ctp3 but works fine in v2 RTM (which is current): http://www.nivot.org/2009/01/22/CTP3TheRunspaceFactoryAndPowerShellAccelerators.aspx
|
Using Dispatcher Invoke with a Runspace in Powershell
By : Jaydeep
Date : March 29 2020, 07:55 AM
wish help you to fix your issue There are a couple of problems with your code that are probably causing the erratic error. Firstly, are you running the code from the Powershell_ISE or from the powershell console? Also, are you running the script in two parts with the dispatcher calls being made from the console after the window is open or as a single script including the dispatcher calls? If you are running the code as as single script then the problem is that the "BeginInvoke" runs the script within its own runspace in a separate thread. Before the window is properly created by this thread the main thread is already trying to set the value of title and the textbox. If you were to split the code in two parts, ie call everything up to begininvoke in one script and then make the dispatcher calls in the main script the code will also have problems as you need to make the hashtable global. code :
$Global:uiHash = [hashtable]::Synchronized(@{})
$newRunspace =[runspacefactory]::CreateRunspace()
$newRunspace.ApartmentState = "STA"
$newRunspace.ThreadOptions = "ReuseThread"
$newRunspace.Open()
$newRunspace.SessionStateProxy.SetVariable("uiHash",$Global:uiHash)
$psCmd = [PowerShell]::Create().AddScript({
$Global:uiHash.Error = $Error
Add-Type -AssemblyName PresentationFramework,PresentationCore,WindowsBase
$xaml = @"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="Window" Title="Initial Window" WindowStartupLocation = "CenterScreen"
Width = "650" Height = "800" ShowInTaskbar = "True">
<Grid>
<TextBox x:Name = "textbox" Height = "400" Width = "600" TextWrapping="Wrap"/>
</Grid>
</Window>
"@
# $reader=(New-Object System.Xml.XmlNodeReader $xaml)
$Global:uiHash.Window=[Windows.Markup.XamlReader]::Parse($xaml )
$Global:uiHash.TextBox = $Global:uiHash.window.FindName("textbox")
$Global:uiHash.TextBox.Text = "Window Creation Thread Id: $([System.Threading.Thread]::CurrentThread.ManagedThreadId.ToString()) Time: $([System.Diagnostics.Stopwatch]::GetTimestamp()) `r`n"
$Global:uiHash.Window.ShowDialog() | out-null
})
$psCmd.Runspace = $newRunspace
$time1 = " Time before beginInvoke: $([System.Diagnostics.Stopwatch]::GetTimestamp()) `r`n"
$handle = $psCmd.BeginInvoke()
#-----------------------------------------
$time2 = " Time after beginInvoke: $([System.Diagnostics.Stopwatch]::GetTimestamp()) `r`n"
#Using the Dispatcher to send data from another thread to UI thread
Start-Sleep -Milliseconds 100
#Update-Window -Title ("Services on {0}" -f $Env:Computername)
$threadId = " Dispatcher Call Thread Id: $([System.Threading.Thread]::CurrentThread.ManagedThreadId.ToString()) Time: $([System.Diagnostics.Stopwatch]::GetTimestamp())`r`n "
$Global:uiHash.Window.Dispatcher.Invoke([action]{$Global:uiHash.TextBox.AppendText($time1)},"Normal")
$Global:uiHash.Window.Dispatcher.Invoke([action]{$Global:uiHash.TextBox.AppendText($time2)},"Normal")
$Global:uiHash.Window.Dispatcher.Invoke([action]{$Global:uiHash.TextBox.AppendText($threadId)},"Normal")
$Global:uiHash.Window.Dispatcher.Invoke([action]{$Global:uiHash.Window.Title = "$($env:ComputerName)"},"Normal")
|
Multi-Runspace PowerShell/XAML script crashing on button click
By : Sergi C
Date : March 29 2020, 07:55 AM
|
Powershell invoke script in new process and within the same runspace
By : Clint Ast
Date : March 29 2020, 07:55 AM
this will help What you describe is impossible - a single runspace cannot span multiple processes. Allow me to demonstrate the relevant hierarchical relationships with this state of the art graphic I whipped up in mspaint:
|
How does a C# runspace/pipeline execute a powershell script?
By : Ramesh Dandu
Date : March 29 2020, 07:55 AM
|