1

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?

1 Answer 1

1

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
})
Sign up to request clarification or add additional context in comments.

5 Comments

It is not the problem, without passing the parameter i can write-host the path, but what i want is to execute "powershell" command
try . $Path for the execution of the script.
ah but now i can't pass parameters . $path [-parameters]
It's still possible to use parameters. Take a look at this
Just use $script:Path in the initil script and it should work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.