Learn about the Exit function in PowerShell _powershell

Source: Internet
Author: User

PowerShell supports the "EXIT" keyword, which is scoped and may be different from the way you think it works.

Let's test this function:

Copy Code code as follows:
function test
{
A
Exit
B
}

When you save and invoke the script, you will get:
Copy Code code as follows:

Ps> C:\USERS\TOBIAS\DOCUMENTS\POWERSHELL\TEST12343.PS1
A

At this point "exit" prematurely ended this function. But when you don't save the current script or when you call an interaction function, your entire PowerShell host will shut down.

"Exit" ends the currently invoked script, not just a function. If you save it to a script like the following, you might find something unexpected:

Copy Code code as follows:

function test
{
A
Exit
B
}

' Start '
Test
' Stop '

Now it turns out like this:
Copy Code code as follows:

Ps<> C:\USERS\TOBIAS\DOCUMENTS\POWERSHELL\TEST12343.PS1
Start
A

Note that the code "Stop" is not executing. "Exit" ends its own function and stops subsequent execution of the scope. If calling an interaction function why not close PowerShell? (This is because, you invoke the scope host is itself).

So how do you use "exit"? You can set a number of error levels while the script is working. This rank number can be obtained from the call. So if you create a PowerShell scheduled task, or execute a batch file by Powershell.exe, then the different numbers you specify after "Exit" will become the exit code outside your script as you would in the batch of%errorlevel%.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.