How to error capture a function?

I have researched this and found the following method works when detecting if a file has been created successfully or not.

Function createexceldoc {
$exceldoc = new-item -path C:\Holding -name ExcelTest3.xlsx -type file #-ea SilentlyContinue
if($? -eq 1 )
{
Write-Host “”
Write-Host " File Succesfully Created "
Write-Host “---------------------------------------------------------------”
}
Else
{
Write-Host “”
Write-Host " Function Failed to Create File"
Write-Host “--------------------------------------------------------------”
}
}
createexceldoc

I am trying to apply the same principle to a different function i have created. However i seem to be getting a false positive. Rather than giving me the appropriate message depending on if the script ran successfully or not, the error capture is returning whatever value i am checking the variable is equal to.

Write message if script has ran succesfully or not.

if($? -eq 0 )
{
Write-Host “”
Write-Host -BackgroundColor Yellow -ForegroundColor Black " Users Succesfully Created "
Write-Host “---------------------------------------------------------------”
}
Else
{
Write-Host “”
Write-Host -BackgroundColor red -ForegroundColor Black " Users Failed to be Created"
Write-Host “--------------------------------------------------------------”
}

I think the $? special variable isn’t passing the success/fail status of the function like i thought it would.

I have also tried placing the function in a variable and then passing that to the if statement wit the $LastExitCode to check the error status. This gives slightly different results in that i get the “Users Failed to be Created” message every time. Even when my script is run and ther users are created.

Any help greatly appreciated as always.

The $? variable only contains whether or not the previous command SAID it failed or completed successfully. It isn’t really “error handling.” Typically, I think checking $error[0] would be best to see the most recent error; execute $error.clear() ahead of time and then check $error.count after performing an operation. If $error.count is zero, there was no error; if it’s nonzero, then $error[0] is the error record.

You can also, frankly, use Try/Catch constructs a lot more effectively than what you’re doing here, I think.

Thanks Don, using the $error,clear() method and then checking the $error.count has provided me with the functionality i wanted.

$error.clear()

Function createexceldoc {

$exceldoc = new-item -path C:\Holding -name ExcelTest2.xlsx -type file #-ea SilentlyContinue
if($error.count -eq 0 )
{
Write-Host “”
Write-Host " File Succesfully Created "
Write-Host “---------------------------------------------------------------”
}
Else
{
Write-Host “”
Write-Host " Function Failed to Create File"
Write-Host “--------------------------------------------------------------”
}
}

createexceldoc

The more structural way to do this would be something like:

try {
  $exceldoc = new-item -path C:\Holding -name ExcelTest2.xlsx -type file -ea stop
} catch {
  # didn't work, see $error[0]
}

Using catch will suppress the error, but let you do something; if the catch block doesn’t execute then there was no error.