Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question

4 Answers 4

Press Ctrl + X to cancel the script execution.

share|improve this answer

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.

share|improve this answer
    
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

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

share|improve this answer

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;
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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