Although Windows PowerShell can benefit administrators in multiple ways, its scalability may be its most important attribute. This includes PowerShell functions. These small and simple task tools are composed of commands in the function box. Using functions makes debugging easier, and enables you to switch functions from one script to another to write less code.
Recommended topics: script technology in Windows-Windows Powershell
When Will PowerShell functions be most useful? The following are three basic guidelines you need to remember:
Generally speaking, it is a good idea to consider reuse when writing code, especially when functions are designed for reuse. Therefore, in addition to defaults, it is important to consider how functions are used and where they can be used to create their expected parameters.
When considering reuse, it is best to fully consider and avoid hard coding as much as possible. In addition, all data should pass the parameter when writing the function. Although it makes sense to use the default value for the parameter, you should allow the function caller to specify other options without modifying the function. This is also useful for black box testing sooner or later. It can determine the availability of a function in different environments. In this regard, it is important to consider all the changes to the original function and how these changes affect the script as a whole. For example, in PowerShell V1, I often try to use my own converter to execute verbose and whatif commands. In V2, this problem has been solved.
When designing a function, you should also consider the loop and processing logic. For example, if you have the logic to process the server, you should maintain the logic outside the function. There is no need to execute it for all function calls. On the other hand, if your logic is obviously a function field, you don't need to remove it, as long as the application calls the script.
What are the features of good PowerShell functions?
Good functions are generated and discarded for specific needs, but all good functions share some commonalities. The following are some of the features:
Excellent PowerShell function feature 1: well-defined parameters
A function needs to be very clear about what data it expects to return. You can use the parameters specified by the application to complete the process. If you have a specified value for processing, make sure it is clear in the function. A good way to complete this process is to assign the parameter default value to the required $ ThisParam.
Excellent PowerShell function feature 2: consistency and expected output
You don't have to guess what data will be in the function. This is critical. You would rather return the expected data. The design function says it returns one or more simple data types, such as linear, datetime, or Boolean ). However, do not use the unexpected data written in the output or the data that is not captured in the variables to pollute the data stream.
Excellent PowerShell function feature 3: Independence
This function should not depend on any variables from the script. If this function needs to be output from the external, convert it into a parameter.
Excellent PowerShell function feature 4: portability
The simplest and most important function of a function is portability. If you do not plan to reuse the code, you can also write Inline code. The key to portability is to ensure that your variable name does not conflict with the call function. In this case, you can use $ my or $ func to order them.