Using a script in an automated build process

I have a Powershell script written that will be used in an automatic build process.

Do I need to write exit code 0 when the script takes the correct path (for example, is not included in if blocks that indicate an error state)?

Also, what is the difference between output (with code) and $ host.SetShouldExit ()?

+3
source share
1 answer

In such a scenario, I rely on exceptions (throw) and other types of terminating errors: in this case, the PowerShells exit code is 1. If the scripts end (even with non-ending errors), then the PowerShells exit code is 0, we do not need to call exit 0. If we need something but 0 and 1, then we really should use exitor SetShouldExit(but see Notes below).

Let's look at the scripts.

test1.ps1

'before2'
.\test2
'after2'

'before3'
.\test3
'after3'

'before4'
.\test4
'after4'

test2.ps1

'inner before 2'
exit 2
'inner after 2'

test3.ps1

'inner before 3'
$host.SetShouldExit(3)
'inner after 3'

test4.ps1

throw 'Oops'

Output test1.ps1:

before2
inner before 2
after2
before3
inner before 3
inner after 3
after3
before4
Oops
At ...
+ throw <<<<  'Oops'

In this test scenario, the types of work test4.ps1 and the types test2.ps1 and test3.ps1 do not work (if it means "work" for work and exits the session immediately).

The output indicates that exitthe current script completes and SetShouldExitdoes not.

powershell.exe .\test1 3 - $host.SetShouldExit(3). , , exit 2 2. , , 1 - test4.

. script PowerShell, $host.SetShouldExit script, . exit .

. $host.SetShouldExit . , ( ), SetShouldExit .

+4

Source: https://habr.com/ru/post/1780643/


All Articles