0

I have SharePoint Powershell Script in .ps1 format and it includes 4 commands. When I run this script, it does not stop if any error occurs in middle.

For example, if error occurs at 2nd command it displays error message and starts executing the 3rd command, but I want it to stop at 2nd command and it should not execute any following commands.

1

Press Ctrl + X to cancel the script execution.

0

You can specify -erroraction parameter value to stop. If you want same behavior for all commands in your script, set $ erroractionpreference value to stop before executing any commands.

  • i tried as below but same issue: the 1st command is : New-SPWebApplication -Name $name -ApplicationPool $applicationpool -ApplicationPoolAccount $applicationpoolaccount -Port $port -AuthenticationProvider $both -HostHeader $hostHeader -AllowAnonymousAccess -erroraction stop and the 2nd command is :New-SPSite -Url $URL -OwnerAlias $creden -Template $template -erroraction stop when there is error in firsty command it should not execute 2nd one. – ARD Nov 21 '11 at 5:43
  • still waiting for solutions..... – ARD Nov 24 '11 at 8:00
0

You might consider using try/catch blocks. An example on how to use this is in the first answer at: https://stackoverflow.com/questions/4456863/powershell-try-catch-rethrow-not-propagating-error-powershell-2-0.

0

You have to set ErrorAction Parameter to stop. To see which values are valid for the ErrorAction parameter you should use

Get-Help about_commonparameters

try-catch-finally is used as in many other programming languages

try
{
   DoSomethingThatMayFail;
}
catch
{
   LogError;
}
finally
{
   Cleanup;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.