Powershell. Accessing variable outside a Runspace instance - wpf

I wrote a small application using WPF and Runspaces and ran into some difficulties.
$Runspace = [runspacefactory]::CreateRunspace()
$Runspace.ApartmentState = "STA"
$Runspace.ThreadOptions = "ReuseThread"
$Runspace.Open()
$code = {
#Build the GUI
[xml]$xaml = #"
$xaml_code
"#
$syncHash = [hashtable]::Synchronized(#{ })
$reader = (New-Object System.Xml.XmlNodeReader $xaml)
$syncHash.Window = [Windows.Markup.XamlReader]::Load($reader)
function RunspaceScript {
param($syncHash, $Servers, $TargetBox)
$syncHash.Host = $host
$Runspace = [runspacefactory]::CreateRunspace()
$Runspace.ApartmentState = "STA"
$Runspace.ThreadOptions = "ReuseThread"
$Runspace.Open()
$Runspace.SessionStateProxy.SetVariable("syncHash", $syncHash)
$Runspace.SessionStateProxy.SetVariable("Servers", $Servers)
$Runspace.SessionStateProxy.SetVariable("TargetBox", $TargetBox)
$code = {
Function Add-OutputBoxLine {
Param ([string]$Message,[string]$Color = "Black")
$syncHash.Window.Dispatcher.invoke(
[action]{
$RichTextRange = New-Object System.Windows.Documents.TextRange($syncHash.$TargetBox.Document.ContentEnd, $syncHash.$TargetBox.Document.ContentEnd)
$RichTextRange.Text = "$Message`r`n"
$RichTextRange.ApplyPropertyValue([System.Windows.Documents.TextElement]::ForegroundProperty, "$Color")
$syncHash.$TargetBox.ScrollToCaret()
}
)
}
$syncHash.Window.Dispatcher.invoke(
[action]{ $syncHash.$TargetBox.Clear(); $syncHash.Start_Switchover.IsEnabled = $False })
if (!$az_connection) {
$az_connection = Connect-AzAccount
}
}
$PSinstance = [powershell]::Create().AddScript($Code)
$PSinstance.Runspace = $Runspace
$PSinstance.Runspace.Name = "SwitchOver"
$job = $PSinstance.BeginInvoke()
}
# Click Actions
$syncHash.Start_Switchover.Add_Click(
{
RunspaceScript -syncHash $syncHash -Servers $syncHash.Servers.Text -TargetBox "Process_Output"
})
$syncHash.Window.ShowDialog()
$Runspace.Close()
$Runspace.Dispose()
}
$PSinstance1 = [powershell]::Create().AddScript($Code)
$PSinstance1.Runspace = $Runspace
$job = $PSinstance1.BeginInvoke()
I want to access variable $az_connection which is inside a runspace which is inside a function "RunspaceScript" from previous execution of that function.
Any ideas how it can be achieved?
P.S. I was forced to remove some lines from the script code here because of the rules, don't try to copy and run the code, it will not work.

Probably the easiest way is to add it to $syncHash - $syncHash.AzConnection = $az_connection. Otherwise you need to return it as output from your scriptblock and then pull the output once the AsyncResult completes.
function RunspaceScript {
param($syncHash, $Servers, $TargetBox)
$syncHash.Host = $host
$Runspace = [runspacefactory]::CreateRunspace()
$Runspace.ApartmentState = 'STA'
$Runspace.ThreadOptions = 'ReuseThread'
$Runspace.Open()
$Runspace.SessionStateProxy.SetVariable('syncHash', $syncHash)
$Runspace.SessionStateProxy.SetVariable('Servers', $Servers)
$Runspace.SessionStateProxy.SetVariable('TargetBox', $TargetBox)
$code = {
Function Add-OutputBoxLine {
Param ([string]$Message, [string]$Color = 'Black')
$syncHash.Window.Dispatcher.invoke(
[action] {
$RichTextRange = New-Object System.Windows.Documents.TextRange($syncHash.$TargetBox.Document.ContentEnd, $syncHash.$TargetBox.Document.ContentEnd)
$RichTextRange.Text = "$Message`r`n"
$RichTextRange.ApplyPropertyValue([System.Windows.Documents.TextElement]::ForegroundProperty, "$Color")
$syncHash.$TargetBox.ScrollToCaret()
}
)
}
$syncHash.Window.Dispatcher.invoke(
[action] { $syncHash.$TargetBox.Clear(); $syncHash.Start_Switchover.IsEnabled = $False })
if (!$az_connection) {
$az_connection = Connect-AzAccount
}
## Output the connection from your scriptblock ##
$az_connection
## OR just add it to your synchronized hashtable ##
$syncHash.AzConnection = $az_connection
}
$PSinstance = [powershell]::Create().AddScript($Code)
$PSinstance.Runspace = $Runspace
$PSinstance.Runspace.Name = 'SwitchOver'
$job = $PSinstance.BeginInvoke()
## Wait for the $code scriptblock to finish ##
While (-not $job.IsCompleted) {
Start-Sleep -Seconds 1
}
## Pass the job to EndInvoke() to receive the output ##
$az_connection = $PSinstance.EndInvoke($job)
## Or access it from your synchronized hashtable ##
$syncHash.AzConnection
}

Related

PowerShell progress Bar invocation fails in Runspace

I am creating a PowerShell script with a GUI, that copies user profiles from a selected source disk to a destination disk. I've created the GUI in XAML, with VS Community 2019.
My copy function is called from a runspace and works fine. I would like to put a progress bar inside this runspace so that it increases for each folder copied, but I can't figure out how.
Here is my runspace :
function RunspaceBackupData {
$Runspace = [runspacefactory]::CreateRunspace()
$Runspace.ApartmentState = "STA"
$Runspace.ThreadOptions = "ReuseThread"
$Runspace.Open()
$Runspace.SessionStateProxy.SetVariable("syncHash",$syncHash)
$Runspace.SessionStateProxy.SetVariable("SelectedFolders",$global:SelectedFolders)
$Runspace.SessionStateProxy.SetVariable("SelectedUser",$global:SelectedUser)
$Runspace.SessionStateProxy.SetVariable("ReturnedDiskSource",$global:ReturnedDiskSource)
$Runspace.SessionStateProxy.SetVariable("ReturnedDiskDestination",$global:ReturnedDiskDestination)
$code = {
foreach ($item in $global:SelectedFolders) {
copy-item -Path "$global:ReturnedDiskSource\Users\$global:SelectedUser\$item" -Destination "$global:ReturnedDiskDestination\Users\$global:SelectedUser\$item" -Force -Recurse
}
}
$PSinstance = [powershell]::Create().AddScript($Code)
$PSinstance.Runspace = $Runspace
$job = $PSinstance.BeginInvoke()
}
Here is my event-handler :
$var_btnStart.Add_Click( {
RunspaceBackupData -syncHash $syncHash -SelectedFolders $global:SelectedFolders -SelectedUser $global:SelectedUser -ReturnedDiskSource $global:ReturnedDiskSource -ReturnedDiskDestination $global:ReturnedDiskDestination
})
Can you please help me ?
Ok i've managed to get my progress bar working. First you gotta add it to your hashtable variable :
$syncHash.ProgressBar = $syncHash.Window.FindName("ProgressBar")
Then you can create your event-handler like this :
$var_btnStart.Add_Click( {
$Runspace = [runspacefactory]::CreateRunspace()
$Runspace.ApartmentState = "STA"
$Runspace.ThreadOptions = "ReuseThread"
$Runspace.Open()
$Runspace.SessionStateProxy.SetVariable("syncHash",$syncHash)
$Worker =
[PowerShell]::Create().AddScript({$syncHash.ProgressBar.Dispatcher.Invoke([action]
{$SyncHash.ProgressBar.IsIndeterminate = $true }, "Normal")}
##YOUR CODE HERE
$syncHash.Error = $Error
$syncHash.ProgressBar.Dispatcher.Invoke([action]{$SyncHash.ProgressBar.IsIndeterminate = $false }, "Normal")
})
$Worker.Runspace = $Runspace
$Worker.BeginInvoke()})
Note that in this example your progress bar is indeterminate, so it starts looping when you click on the button, and stops when you code ends.
Don't forget to dispose your runspace then :
$Runspace.PowerShell.Close($Runspace) $Runspace.PowerShell.Dispose($Runspace)
Inside $code you need to keep the count, so I suggest a for-loop.
Inside the loop you can call the dispatcher in the window-object:
$syncHash.Window.Dispatcher.Invoke([action]{$syncHash.Progressbar.Value = [double] <code for finished percentage>}, "Normal")
I suspect you have window and progressbar in $syncHash

How to make a label of GUI PowerShell has same location in any display executed?

I made a GUI contain some label. Once I execute this code in other computer or notebook, the location of those label changed. How do I make the location can be same no matter the display that I execute the code?
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object System.Windows.Forms.Form
$Form.ClientSize = '578,400'
$Form.text = "Form"
$Form.BackColor = "#c1daf7"
$Form.TopMost = $false
$Form.WindowState = 'Maximized'
$Label1 = New-Object System.Windows.Forms.Label
$Label1.text = "UNDER PROCESS"
$Label1.AutoSize = $true
$Label1.Location = New-Object System.Drawing.Point(600, 300)
$Label1.Font = 'Microsoft Sans Serif,30,style=Bold,Underline'
$Label1.ForeColor = "#d0021b"
$Label2 = New-Object System.Windows.Forms.Label
$Label2.text = "WAITING"
$Label2.AutoSize = $true
$Label2.Location = New-Object System.Drawing.Point(770, 500)
$Label2.Font = 'Microsoft Sans Serif,20,style=Bold'
$Label2.ForeColor = "#fb0505"
$Form.controls.AddRange(#($Label1, $Label2))
[void]$Form.ShowDialog()
UPDATED
I updated my code with a full code.
I tried this but it return me error:
Exception calling "ShowDialog" with "0" argument(s): "Form that is already visible cannot be displayed as a modal dialog box. Set the form's visible property to false before calling showDialog."
Param (
[string]$Path = '*.*',
[string]$MaxAttempts = 5
)
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
# set things up for the timer
$script:nAttempts = 0
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 1000 # 1 second
$timer.Add_Tick({
$global:Result = $null
$script:nAttempts++
$file = Get-Item -Path $Path
if ($file) {
$global:Result = [PSCustomObject]#{
Exists = $true
FileName = $file.FullName
Attempts = $script:nAttempts
}
$timer.Dispose()
$Form.Close()
}
elseif ($script:nAttempts -ge $MaxAttempts) {
$global:Result = [PSCustomObject]#{
Exists = $false
FileName = ''
Attempts = $script:nAttempts
}
$timer.Dispose()
$Form.Close()
}
})
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '617,418'
$Form.text = "Auto"
$Form.BackColor = "#8b572a"
$Form.TopMost = $false
$Form.WindowState = 'Maximized'
$Label1 = New-Object system.Windows.Forms.Label
$Label1.text = "UNDER AUTOMATION PROCESS"
$Label1.AutoSize = $true
$Label1.width = 25
$Label1.height = 10
$Label1.Anchor = 'top,right,bottom,left'
$Label1.ForeColor = "#ffffff"
$Label1.Anchor = "None"
$Label1.TextAlign = "MiddleCenter"
$Label2 = New-Object system.Windows.Forms.Label
$Label2.text = "Waiting for the job..."
$Label2.AutoSize = $true
$Label2.width = 25
$Label2.height = 10
$Label2.ForeColor = "#ffffff"
$Label2.Anchor = "None"
$Label2.TextAlign = "MiddleCenter"
$Form.controls.AddRange(#($Label1,$Label2))
[void]$Form.Show()
Write-Host $Form.Height
Write-Host $Form.Width
$Label1.location = New-Object System.Drawing.Point(($Form.Width*0.35), ($Form.Height*0.4))
$Label2.location = New-Object System.Drawing.Point(($form.Width*0.43), ($Form.Height*0.5))
$L_S = (($Form.Width/2) - ($Form.Height / 2)) / 15
$L_S
$Label1.Font = "Microsoft Sans Serif, $L_S, style=Bold"
$Label2.Font = "Microsoft Sans Serif, $L_S, style=Bold"
$Form.controls.AddRange(#($Label1,$Label2))
# start the timer as soon as the dialog is visible
$Form.Add_Shown({ $timer.Start() })
[void]$Form.ShowDialog()
# clean up when done
$Form.Dispose()
I updated my code, I tried this, but it still return me an error. Anyone can help me to fix it please. Thanks
Updated 2nd
Param (
[string]$Path = '*.*',
[string]$MaxAttempts = 5
)
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
# set things up for the timer
$script:nAttempts = 0
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 1000 # 1 second
$timer.Add_Tick({
$global:Result = $null
$script:nAttempts++
$file = Get-Item -Path $Path
if ($file) {
$global:Result = [PSCustomObject]#{
Exists = $true
FileName = $file.FullName
Attempts = $script:nAttempts
}
$timer.Dispose()
$Form.Close()
}
elseif ($script:nAttempts -ge $MaxAttempts) {
$global:Result = [PSCustomObject]#{
Exists = $false
FileName = ''
Attempts = $script:nAttempts
}
$timer.Dispose()
$Form.Close()
}
})
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
# $Form.ClientSize = '617,418'
$Form.text = "AutoGM"
$Form.BackColor = "#9b9b9b"
$Form.TopMost = $false
$Form.Width = [System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Width
$Form.Height = [System.Windows.Forms.SystemInformation]::PrimaryMonitorSize.Height
$FontSize = ($Form.Width / 100) + ($Form.Height/100) + 5
$Label1 = New-Object system.Windows.Forms.Label
$Label1.text = "UNDER PROCESS"
$Label1.AutoSize = $true
# $Label1.width = 25
# $Label1.height = 10
$Label1.Anchor = "None"
$Label1.Location = New-Object System.Drawing.Point(($form.Width*0.3), ($Form.Height*0.3))
$Label1.Font = "Microsoft Sans Serif,$FontSize,style=Bold"
$Label1.ForeColor = "#000000"
$Label2 = New-Object system.Windows.Forms.Label
$Label2.text = "Waiting..."
$Label2.AutoSize = $true
# $Label2.width = 25
# $Label2.height = 10
$Label2.Location = New-Object System.Drawing.Point(($form.Width*0.4), ($Form.Height*0.4))
$Label2.Anchor = "None"
$Label2.Font = "Microsoft Sans Serif,$FontSize"
$Label2.ForeColor = "#000000"
$img = [System.Drawing.Image]::Fromfile(".\img.png")
$pictureBox = new-object Windows.Forms.PictureBox
$pictureBox.Location = New-Object System.Drawing.Point(($form.Width*0.45), ($Form.Height*0.5))
$pictureBox.Width = $Form.Size.Width / 5
$pictureBox.Height = $Form.Size.Height / 5
$pictureBox.Image = $img
$form.controls.add($pictureBox)
$Form.controls.AddRange(#($Label1,$Label2))
# Write-Host $Form.Height
# Write-Host $Form.Width
# $Label1.location = New-Object System.Drawing.Point(($Form.Width*0.35), ($Form.Height*0.4))
# $Label2.location = New-Object System.Drawing.Point(($form.Width*0.43), ($Form.Height*0.5))
# $L_S = (($Form.Width/2) - ($Form.Height / 2)) / 15
# $Label1.Font = "Microsoft Sans Serif, $L_S, style=Bold"
# $Label2.Font = "Microsoft Sans Serif, $L_S, style=Bold"
# $Form.controls.AddRange(#($Label1,$Label2))
# start the timer as soon as the dialog is visible
$Form.Add_Shown({ $timer.Start() })
[void]$Form.ShowDialog()
# clean up when done
$Form.Dispose()
As stated by Niraj it depends on the resolution of the monitor being used so instead ask yourself where do you want it to show? in mid-mid? mid-left? this can be achieved using some math, see below for a simple example, it uses the $form width en height to calculate the correct position for the labels.
$Label1.Location = New-Object System.Drawing.Point(($form.Width*0.5), ($Form.Height*0.5))
$Label2.Location = New-Object System.Drawing.Point(($form.Width*0.5), ($Form.Height*0.4))

Manual Checkbox enable

I have a ComboBox control on my form. What I want is when I change an item in the ComboBox from one to another, the event was handled. It is important that when changing and not when choosing the same element. All this time I used ComboBox.Add_SelectionChangeCommitted($function), but soon I realized that the block that is also executed by the handler when the same (selected) item is selected from the list. A little digging in ComboBox events I am completely confused. Having tried several events (SelectedItemChanged, SelectedIndexChanged) I could never able to achieve the desired result.
An example of what I want to do and what should not be done several times when choosing the same element. When the block of code for Manual is executed several times, the contents of all TextBox are cleared, but I don’t want to.
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
$Form = New-Object system.Windows.Forms.Form
$Form.ClientSize = '420,240'
$Form.TopMost = $false
$Form.FormBorderStyle = 'Fixed3D'
$Form.MaximizeBox = $false
$ComboBox = New-Object system.Windows.Forms.ComboBox
$ComboBox.width = 391
$ComboBox.height = 47
#('Automatic (DHCP)','Manual Input') | ForEach-Object {[void] $ComboBox.Items.Add($_)}
$ComboBox.location = New-Object System.Drawing.Point(13,57)
$ComboBox.DropDownStyle = 'DropDownList'
$ComboBox.DrawMode = 'OwnerDrawFixed'
$Button = New-Object system.Windows.Forms.Button
$Button.text = "Set"
$Button.width = 60
$Button.height = 30
$Button.visible = $false
$Button.enabled = $false
$Button.location = New-Object System.Drawing.Point(336,126)
$CheckBox = New-Object system.Windows.Forms.CheckBox
$CheckBox.width = 65
$CheckBox.height = 15
$CheckBox.visible = $false
$CheckBox.Checked = $false
$CheckBox.enabled = $true
$CheckBox.location = New-Object System.Drawing.Point(16,210)
$TextBox1 = New-Object system.Windows.Forms.TextBox
$TextBox1.Name = "TextBox1"
$TextBox1.multiline = $false
$TextBox1.width = 40
$TextBox1.height = 20
$TextBox1.visible = $false
$TextBox1.enabled = $true
$TextBox1.location = New-Object System.Drawing.Point(81,100)
$TextBox2 = New-Object system.Windows.Forms.TextBox
$TextBox2.Name = "TextBox2"
$TextBox2.multiline = $false
$TextBox2.width = 40
$TextBox2.height = 20
$TextBox2.visible = $false
$TextBox2.enabled = $false
$TextBox2.location = New-Object System.Drawing.Point(81,208)
$Form.controls.AddRange(#($TextBox1,$TextBox2,$Button,$ComboBox,$CheckBox))
$global:ManualChecked = $null
$global:AutomaticChecked = $null
$ComboBox.Add_SelectionChangeCommitted($methodSelection)
$netwValues = New-Object 'System.Collections.Hashtable'
$methodSelection =
{
switch($ComboBox.Text)
{
"Manual Input"
{
$CheckBox.Visible = $Button.Visible = $true
$Button.Enabled = $false
ForEach ($control in $Form.controls)
{
if ($control -is [System.Windows.Forms.TextBox] )
{
$control.Visible = $control.Enabled = $true
$control.Clear()
if($netwValues.Count -gt 0)
{
$control.Text = $netwValues.Item($control.Name)
$netwValues.Remove($control.Name)
}
}
}
if($global:ManualChecked -eq 1)
{
$CheckBox.Checked = $true
$TextBox2.Enabled = $true
}
else
{
$CheckBox.Checked = $false
$TextBox2.Enabled = $false
}
}
"Automatic (DHCP)"
{
ForEach($control in $Form.controls)
{
if($control -is [System.Windows.Forms.TextBox] -or $control -is [System.Windows.Forms.CheckBox] -or $control -is [System.Windows.Forms.Button])
{
$control.Visible = $control.Enabled = $true
if($control -is [System.Windows.Forms.Label] -or $control -is [System.Windows.Forms.TextBox])
{
$control.Enabled = $false
if($control -is [System.Windows.Forms.TextBox])
{
if($control.Text.Length)
{
$netwValues.Add($control.Name,$control.Text)
$control.Clear()
}
}
}
}
}
if($global:AutomaticChecked -eq 1)
{
$CheckBox.Checked = $true
}
else
{
$CheckBox.Checked = $false
}
}
}
}
You can implement this logic yourself.
# Simple class for using as item for combobox instead of just string.
class CbItem
{
CbItem([string] $value)
{
$this.Value = $value
}
[string] $Value
[string] ToString()
{
return $this.Value
}
}
# Add instances of this class into combobox.
$ComboBox.Items.Add([CbItem]::new('Automatic (DHCP)'))
$ComboBox.Items.Add([CbItem]::new('Manual Input'))
# Create a variable for storing current combobox's item.
$global:CurrentCbItem = $null
# Add logic at the begining of handler for exiting if selected item is the same as the last one.
$methodSelection =
{
if ([Object]::ReferenceEquals($global:CurrentCbItem, $ComboBox.SelectedItem))
{
return
}
$global:CurrentCbItem = $ComboBox.SelectedItem
...
}

Powershell $button.add_click executes the whole code inside the add_click block

I am new to WPF and event-handling using powershell, what I want to achieve is on click of a button, the progress bar should be shown from 0-100. But when I am running the following piece of code, it computes the whole code inside the add_click block and it only shows the last iteration in the loop.
I know maybe it is a silly solution, but I do need some help in this.
$syncHash = [hashtable]::Synchronized(#{})
$newRunspace =[runspacefactory]::CreateRunspace()
$newRunspace.ApartmentState = "STA"
$newRunspace.ThreadOptions = "ReuseThread"
$newRunspace.Open()
$newRunspace.SessionStateProxy.SetVariable("syncHash",$syncHash)
$psCmd = [PowerShell]::Create().AddScript({
$XamlPath="C:\Forms\MyFormsv1.xaml"
$inputXML = Get-Content -Path $XamlPath
[xml]$Global:xmlWPF = $inputXML -replace 'mc:Ignorable="d"','' -replace "x:N",'N' -replace '^<Win.*', '<Window'
#Add WPF and Windows Forms assemblies
try{
Add-Type -AssemblyName PresentationCore,PresentationFramework,WindowsBase,system.windows.forms
} catch {
Throw “Failed to load Windows Presentation Framework assemblies.”
}
$syncHash.Window=[Windows.Markup.XamlReader]::Load((new-object System.Xml.XmlNodeReader $xmlWPF))
[xml]$XAML = $xmlWPF
$xmlWPF.SelectNodes("//*[#*[contains(translate(name(.),'n','N'),'Name')]]") | %{
$Global:synchash.Add($_.Name,$synchash.Window.FindName($_.Name) )}
function tool1_progress{
$var1=50
for($index=1 ; $index -le $var1; $index++)
{
[int]$tmpProgNum= ($index/$var1) * 100
$syncHash.tool1_pb.Value= $tmpProgNum
$syncHash.consoleOutput.Text=$index
Start-Sleep -Milliseconds 250
}
}
$syncHash.myButton.add_click({tool1_progress})
$syncHash.Window.ShowDialog() | out-null
})
$psCmd.Runspace = $newRunspace
$data = $psCmd.BeginInvoke()
$tool1 is a label. $tool1_pb is a progress bar. $consoleOutput is a
text box.
I integrate WPF and PowerShell all the time. This website helped me immensely. You can't update a UI that's running on the same thread, so you need to invoke a new runspace using BeginInvoke()
$psCmd = [PowerShell]::Create().AddScript({
$Global:uiHash.Error = $Error
Add-Type -AssemblyName PresentationFramework,PresentationCore,WindowsBase
$xaml = #"YOURXAMLHERE"
$Global:uiHash.Window=[Windows.Markup.XamlReader]::Parse($xaml )
[xml]$XAML = $xaml
$xaml.SelectNodes("//*[#*[contains(translate(name(.),'n','N'),'Name')]]") | %{
$Global:uihash.Add($_.Name,$uihash.Window.FindName($_.Name) )}
$Global:uiHash.Window.ShowDialog() | out-null
})
$psCmd.Runspace = $newRunspace
$handle = $psCmd.BeginInvoke()
Then to update whatever it is you need, you would use Window.Dispatcher.Invoke
$Global:uiHash.Window.Dispatcher.Invoke([action]{$Global:uiHash.Window.Title = "MyWindowTitle"},"Normal")
EDIT
$Global:uiHash.Button1.Add_Click(tool1_progress)
EDIT
Throw in your XML and this will work. You have to create another runspace on top of the other in order to keep updating the textbox and progress bar. Note that synchash MUST be $Global:synchash. Functions courtesy of this post.
$Global:syncHash = [hashtable]::Synchronized(#{})
$newRunspace =[runspacefactory]::CreateRunspace()
$newRunspace.ApartmentState = "STA"
$newRunspace.ThreadOptions = "ReuseThread"
$newRunspace.Open()
$newRunspace.SessionStateProxy.SetVariable("syncHash",$syncHash)
$psCmd = [PowerShell]::Create().AddScript({
$XamlPath="C:\Forms\MyFormsv1.xaml"
$inputXML = #"YOURXMLHERE"#
[xml]$Global:xmlWPF = $inputXML
#Add WPF and Windows Forms assemblies
try{
Add-Type -AssemblyName PresentationCore,PresentationFramework,WindowsBase,system.windows.forms
} catch {
Throw “Failed to load Windows Presentation Framework assemblies.”
}
$Global:syncHash.Window=[Windows.Markup.XamlReader]::Load((new-object System.Xml.XmlNodeReader $xmlWPF))
[xml]$XAML = $xmlWPF
$xmlWPF.SelectNodes("//*[#*[contains(translate(name(.),'n','N'),'Name')]]") | %{
$Global:synchash.Add($_.Name,$Global:syncHash.Window.FindName($_.Name) )}
function Start-Runspace{
param($scriptblock)
$newRunspace =[runspacefactory]::CreateRunspace()
$newRunspace.ApartmentState = "STA"
$newRunspace.ThreadOptions = "ReuseThread"
$newRunspace.Open()
$newRunspace.SessionStateProxy.SetVariable("SyncHash",$global:synchash)
$psCmd = [PowerShell]::Create().AddScript($ScriptBlock)
$psCmd.Runspace = $newRunspace
$psCMD.BeginInvoke()
}
$SB = {
$var1=50
for($index=1 ; $index -le $var1; $index++)
{
[int]$tmpProgNum= ($index/$var1) * 100
$Global:syncHash.Window.Dispatcher.Invoke([action]{$Global:Synchash.tool1_pb.value = "$tmpprognum"},"Normal")
$Global:syncHash.Window.Dispatcher.Invoke([action]{$Global:Synchash.consoleoutput.text = "$index"},"Normal")
Start-Sleep -Milliseconds 250
}
}
$Global:syncHash.myButton.add_click({Start-Runspace $SB})
$Global:syncHash.Window.ShowDialog() | out-null
})
$psCmd.Runspace = $newRunspace
$data = $psCmd.BeginInvoke()
start-sleep -Milliseconds 300

Switch statement produces incorrect result

I'm creating a script for our support department and interns. I have a working script but wanted to use a GUI to it a bit easier for our interns. At this moment I'm pulling my hair out right now, I've copied the function below and and would like to have the value of $task1_exec. But when I try to put the return $Result in a variable the $Task1 variable is printed not the $Task1_exec one.
$FormTitle = "Formtitle"
$ScriptPath = $(get-location).Path;
$task1 = "Task 1"
$task1_exec = "Perform task 1"
$task2 = "Task 2"
$task2_exec = "Perform task 2"
$task3 = "Task 3"
$task3_exec = "Perform task 3"
$task4 = "Task 4"
$task4_exec = "Perform task 4"
function Prompt-SelectionList
{
Param(
[Parameter(Position=0,Mandatory=$true)][string]$Description,
[Parameter(Position=1,Mandatory=$true)][string[]]$List
)
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[void] [System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")
$Form = New-Object System.Windows.Forms.Form
$Form.Text = $FormTitle
$Form.Size = New-Object System.Drawing.Size(500,450) #(300,200)
$Form.StartPosition = "CenterScreen"
$btnOK = New-Object System.Windows.Forms.Button
$btnOK.Location = New-Object System.Drawing.Size(125,375)
$btnOK.Size = New-Object System.Drawing.Size(125,25)
$btnOK.Text = "Run..."
$Form.Controls.Add($btnOK)
$btnCancel = New-Object System.Windows.Forms.Button
$btnCancel.Location = New-Object System.Drawing.Size(250,375)
$btnCancel.Size = New-Object System.Drawing.Size(125,25)
$btnCancel.Text = "Exit"
$btnCancel.Add_Click({ $script:Result = $null; $Form.Close() })
$Form.Controls.Add($btnCancel)
$Label = New-Object System.Windows.Forms.Label
$Label.Location = New-Object System.Drawing.Size(10,20)
$Label.Size = New-Object System.Drawing.Size(480,20)
$Label.Text = $Description
$Form.Controls.Add($Label)
$ListBox = New-Object System.Windows.Forms.ListBox
$ListBox.Location = New-Object System.Drawing.Size(10,40)
$ListBox.Size = New-Object System.Drawing.Size(460,330)
$List | ForEach-Object { [void] $ListBox.Items.Add($_) }
$Form.Controls.Add($ListBox)
$Form.KeyPreview = $True
$Form.Add_KeyDown({ if ($_.KeyCode -eq "Enter") { $script:Result = $ListBox.SelectedItem; $Form.Close() }})
$Form.Add_KeyDown({ if ($_.KeyCode -eq "Escape") { $script:Result = $null; $Form.Close() }})
$btnOK.Add_Click({ $script:Result = $ListBox.SelectedItem; $Form.Close() })
$Form.Topmost = $True
$Form.Add_Shown({ $Form.Activate() })
[void] $Form.ShowDialog()
return $Result
}
function Do-PromptTask
{
switch -wildcard (Prompt-SelectionList "Chose task to run:" #($task1,$task2,$task3,$task4))
{
$task1{$task1_exec}
$task2{$task2_exec}
$Task3{$task3_exec}
$Task4{$task4_exec}
""{Exit} #End Selection (cancel selected)
# "*"{Do-PromptTask} #Keep prompting until "cancel" selected
}
}
Do-PromptTask
My wish is that I could return $task1_exec to a variable so when Do-PromptTask is finished the return can be used to call another script
. ".\Bin\$Result.ps1"
It looks like you're on the right track.
Let's pretend I run Do-PromptTask, chose Task 1, and click Run...
The $result variable from Prompt-SelectionList is passed back to the Switch statement and is out of scope at this point. The switch is doing its job and matching the conditional which then prints $task1_exec, but $result is of no use at this point. If you want to store $task1_exec as a variable, you'd need to set something equal to it. However, I will say you could just act on it in the switch statement without storing it in a variable, if that makes sense.
Less code approach...
function Do-PromptTask
{
switch -wildcard (Prompt-SelectionList "Chose task to run:" #($task1,$task2,$task3,$task4))
{
$task1{Invoke-Expression $task1_exec}
$task2{Invoke-Expression $task2_exec}
$Task3{Invoke-Expression $task3_exec}
$Task4{Invoke-Expression $task4_exec}
""{Exit} #End Selection (cancel selected)
# "*"{Do-PromptTask} #Keep prompting until "cancel" selected
}
}
If you're set on storing the variable, you could do something like this...
function Do-PromptTask
{
switch -wildcard (Prompt-SelectionList "Chose task to run:" #($task1,$task2,$task3,$task4))
{
$task1{$task_result = $task1_exec}
$task2{$task_result = $task2_exec}
$Task3{$task_result = $task3_exec}
$Task4{$task_result = $task4_exec}
""{Exit} #End Selection (cancel selected)
# "*"{Do-PromptTask} #Keep prompting until "cancel" selected
}
Invoke-Expression $task_result
}

Resources