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.
functionkeyword associated with it