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:
Or
Copy Code code as follows:
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:
Or
Copy Code code as follows:
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.