0

I am getting the below error when i call the method in PowerShell. Any help would be thankful very much.

Error:

Error in The term 'Test' 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.

Code

Try
{   
    Test
}
Catch
{
    $ErrorMessage = $_.Exception.Message
    Write-Host "Error in" 
    Write-Host $ErrorMessage
}


function Test()
{
  Write-Host "Test Method Called"
}

1 Answer 1

2

The reason your call to Test fails is that PowerShell scripts are not pre-compiled, but evaluated from top to bottom.

Since the Test function is only declared at the end of your script, it does not "exist" when the Try-Catch block is executed.

Simply swap the order:

function Test()
{
  Write-Host "Test Method Called"
}

Try
{   
    Test
}
Catch
{
    $ErrorMessage = $_.Exception.Message
    Write-Host "Error in" 
    Write-Host $ErrorMessage
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi Jessen, Please see my updated "Error" part in the description. I am using "." only to call the script.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.