PowerShell Introductory tutorial functions, scripts, Scope introduction _powershell

Source: Internet
Author: User
Tags anonymous

The script embodies the PowerShell programming features and is the foundation of task automation. A function is a code reuse unit that is more granular than a script, and can be defined either on the command line or in a script. Scopes are the scopes of variables and functions, the partitioning of execution contexts.

Function

A function is a named list of commands that has the same scope as a function concept in a general programming language. Not only can there be simple commands in a function, but also commands that control the flow, such as if, while, switch, and so on. Functions can have anonymous parameters or named argument lists. A list of command arguments can be defined with curly braces or param keywords. Anonymous functions can be accessed using the $args variable. Functions can also receive objects from pipes as input, and pipe objects can be accessed through the $input variable class.

Functions defined in the script can be defined anywhere after the #require command and the PARAM keyword, but are defined before the call. Also, custom functions do not run automatically and need to be explicitly invoked. You can use filter or function-defined functions, and the function defined by the filter keyword is simpler, and functions defined with the Function keyword can have more complex functionality.

Examples of simple function definitions are as follows:

Copy Code code as follows:

function SayHello
{
"Hello"
}

The function call method is similar to using the Cmdlet method, input SayHello, and enter. The result is hello.

Script

The script stores some commands in a file file and sets the extension of the text file to. ps1. In addition to using a common cmdlet to control the commands of a process, you can also define and invoke custom functions in your scripts, calling methods similar to calling a Cmdlet method.

The script can also have parameters, either named or anonymous parameters. In the use of parameters, scripts are very similar to functions.

In addition, PowerShell script execution policy does not allow execution of any script files by default, and modifying execution policies can perform the following command: Set-executionpolicy remotesigned. Keep in mind that modifying the execution strategy poses a security risk, and think twice before modifying the execution policy.

Simple Script D:\greet.ps1 example below

Copy Code code as follows:

Param ([String] somebody)

function Greet ([String] name)
{
"Hello $name"
}

echo "Call function Greet ..."
Greet $somebody

The script calls the following method:
Copy Code code as follows:

D:\greet.ps1 "Luke"

Or
Copy Code code as follows:

. \greet.ps1 "Luke"

The result of the above script is "Hello Luke".

Scope

Divided by type, there are two scopes: global (globally scoped) and script (scripting scope). When the PowerShell command line is started, all command line commands run in the global scope. The script context runs in the script scope, and the variables and functions defined in the script are not visible when the run ends. This is because the variables and functions defined in the script are default in the script scope. Of course, you can also display scopes that define variables and functions, such as function global:fun1 () {...}. Thus, after the script has been executed, the FUN1 can also be executed in the global scope.

By axis relationship, you can have a parent scope, a local scope (the current scope), and a child scope. These are not the new scope types, but the relative relationships between scopes. The child scope can also have child scopes, which can be very deep.

In addition to defining variables and functions in a script in the global scope, you can also use the point "." Gets the source, executes a normal script in the local scope, and after it has finished exiting the script, all variables and functions defined in the script continue to be available in the local scope.

Use point "." Get the source example as follows:

Copy Code code as follows:

. D:\greet.ps1 "Luke"

Or
Copy Code code as follows:

. . \greet.ps1 "Luke"

That is, the dot ".", the space, and then the general script execution mode.

Conclusion

Functions, scripts, and scopes, each concept has a lot to say in detail. This is simply a description of their concepts, their relationships, and their simple usage. Let the reader have a general impression, to be able to bring it to use.

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.