In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Reference article:
Don Jones https://technet.microsoft.com/en-us/library/ff677563.aspx
Powershell help documentation:
Help about_functions_advanced_parameters
Help about_common_parameters
After reading all these three parts, you should also have a general idea of what the CmdletBinding () in the function is for. The official documents are all in English, and they generally look big. The following is my description in popular language, which is easier to understand.
The introduction of this feature began with Powershell Version 2, which is called the advanced feature of the function. After the declaration of the function is enabled, what we call general parameters can be called:
-Verbose
-Debug
-ErrorAction
-WarningAction
-ErrorVariable
-WarningVariable
-OutVariable
-OutBuffer
-PipeLineVariable
Access to these common parameters can be easily enabled by placing them at the beginning of the function. As follows:
[CmdletBinding ()]
Param ()
After enabling this advanced property, you can easily add some command-line features to the function and call the general parameters of the command, just like using the command line. Here is the simplest example. Open an ISE and copy the following:
Function Go-Advanced {
[cmdletbinding ()] param ()
}
The keyword param () is still required, even if you don't define any parameters in the function.
Try to run this function, and then hit the middle line, how about, you can see these general parameters that can be called.
Here are a few examples to call these general parameters, starting with the use of Verbose.
Or enter the following code in ISE
Function Go-Verbose {
[CmdletBinding ()] Param ()
Write-Verbose "Alright, you prefer talkative functions. First of all, I appreciate your wish to learn more about the common parameter-Verbose. Secondly, blah blah.."
Write-Host "This is self-explanatory, anyway."
}
Then perform the following to see if the following results, through this example, you will understand how verbose comes out, the word is very difficult to translate, only through the example to figure out what it means.
Another example is to call the ShouldContinue method, which is still very important when writing a program, for example, if you want to do some destructive operations, if the program is robust, you must jump out of a box and ask to execute it. That kind of SilientContinue practice is not recommended.
The following is still the code:
This code still needs to add a-confirm parameter when the function is executed. If you want to jump out of the box without adding this parameter, an additional operation is required. Set the environment variable $ConfirmPreferece to low. Look at the following code and execution effect.
Function Remove-ByForce {
[CmdletBinding (SupportsShouldProcess)]
Param ([String] $File)
$ConfirmPreference = "Low"
If ($PSCmdlet.ShouldContinue ("Are you sure that you know what you are doing?", "Delete with-Force parameter!")) {
Remove-Item $File-Force
} Else {
"Mission aborted!"
}
}
Remove-ByForce test
These are two basic examples, which will be discussed in the following article
The influence of [parameter ()] in param () on the variable.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.