0

I have a PowerShell script that I'm working on that runs just fine when run from within ISE and whenever I paste it into a PowerShell window and hit enter. However, whenever I right click it and click Run With PowerShell I get an exception. The exception gets thrown when a function is called.

Here is the function:

function GUITitleTextBox_TextChanged
{
    [CmdletBinding()]
    param
    (
        [parameter(Mandatory=$True, Position=0)][System.Object]    $Sender,
        [parameter(Mandatory=$True, Position=1)][System.EventArgs] $EventArguments,
        [parameter(Mandatory=$True, Position=2)][String]           $Text,
        [parameter(Mandatory=$False, Position=3)][System.Object]   $Control1ToUpdate,
        [parameter(Mandatory=$False, Position=4)][System.Object]   $Control2ToUpdate
    )
    
    $Control1ToUpdate.Text = $Text
    $Control2ToUpdate.Text = $Text
}

and here are the lines of code that call the function:

$GUITitleTextBox_Handler = {GUITitleTextBox_TextChanged -Sender $GUITitleTextBox -EventArguments $_ -Text $GUITitleTextBox.Text -Control1ToUpdate $DefaultFormTab -Control2ToUpdate $FormLabel}.GetNewClosure()

$GUITitleTextBox.Add_TextChanged($GUITitleTextBox_Handler)

And this is the exception that gets thrown (only from right click Run with PowerShell, not when running in ISE or pasting it into a PowerShell window. (Path removed since it happens on multiple computers in different paths).

GUITitleTextBox_TextChanged : The term 'GUITitleTextBox_TextChanged' is not recognized as the name of a cmdlet,
function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the
path is correct and try again.
At <Path>\PowerShell-GUI.ps1:899 char:33
+     $GUITitleTextBox_Handler = {GUITitleTextBox_TextChanged -Sender $ ...
+                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (GUITitleTextBox_TextChanged:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

What is causing this exception and how can I prevent it? This is the only function that causes an exception in the script that I've seen so far.

5
  • Make sure all of your function definitions are "on top" of the rest of the script - they won't be available for invocation until the interpreter actually reaches the function keyword associated with it Commented Nov 6, 2023 at 17:10
  • The function is above the call. There are no issues when I run it from ISE or pasting it into a PowerShell window. Only when running it with right click. Commented Nov 6, 2023 at 17:18
  • Can you post the full script? Commented Nov 6, 2023 at 17:18
  • It's a lengthy script (900+ lines) that won't fit in a comment. The relevant code is what I originally shared. I'm mostly looking for reasons ISE and copy-pasting it works just fine but running it through right click fails to successfully call that one function. It seems to be related to the GetNewClosure() function that allows me to pass objects to the function successfully. Commented Nov 6, 2023 at 19:30
  • This answer (for a different question) gives a workaround, but doesn't explain why it happens or if there are any other workaround options: stackoverflow.com/a/49443377/22859752 Commented Nov 6, 2023 at 19:51

0

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.