Reputation: 141
I do a basic powershell script with a window and a simple button,
On the add_click action i want to execute the "powershell -file $path" command to open another script
in the main the command works, but not when it is in the .add_click({ })
#main
Add-Type -AssemblyName System.Windows.Forms
$form = New-Object Windows.Forms.Form
$btn1 = New-Object Windows.Forms.Button
$btn1.Text = "Button1"
$form.Controls.Add($btn1)
$path = "C:\Users\Administrateur\Desktop\export_vers_test\test_cmd.ps1"
#powershell -file $path #Here it works
$btn1.add_Click({
write-host $path
powershell -file $path #Here it works doesn't works
})
$form.ShowDialog()
Can i have some help please?
Upvotes: 1
Views: 14381
Reputation: 2434
You need to pass the string path to your powershell function.
Use paramters for that.
Your function:
function Set-ActionOnClic{
param($path)
write-host $path
}
the call in the click event
$btn1.add_Click({
Set-ActionOnClic -path $path
#Run the script
. $Path
})
Upvotes: 1