Reputation: 2127
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"
}
Upvotes: 0
Views: 1702
Reputation: 174435
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
}
Upvotes: 2