Fail Jenkins build if condition occured in Powershell script

Viewed 2079

I have a code, which checks free space on disk, and if space is not enought - stops and fails build:

$freespace = Get-PSDrive D

$DiskDSpace = ($freespace.Free) 

If ($DiskDSpace -lt 214748364809999999999) { 
echo "Free space on disk D is less than 20 GB" 
exit 1 
exit $LastExitCode 
}

But it only skip all other actions in current Powershell script and continues to execute build. My question is how to fail jenkins build inside PS script, when condition not met?

1 Answers

You're not setting the $LastExitCode to anything.

Change:

exit 1 
exit $LastExitCode 

To:

$LastExitCode = 1
exit $LastExitCode 
Related