1
1
---
2
- ms.date : 2017-06-09
2
+ ms.date : 2018-01-03
3
3
schema : 2.0.0
4
4
locale : en-us
5
5
keywords : powershell,cmdlet
6
6
title : about_Functions_Advanced_Methods
7
7
---
8
8
9
9
# About Functions Advanced Methods
10
- ## about_Functions_Advanced_Methods
11
10
11
+ ## SHORT DESCRIPTION
12
12
13
- # SHORT DESCRIPTION
14
-
15
- Describes how functions that specify the ` CmdletBinding ` attribute can use
16
- the methods and properties that are available to compiled cmdlets.
13
+ Describes how functions that specify the ` CmdletBinding ` attribute can use the
14
+ methods and properties that are available to compiled cmdlets.
17
15
18
16
# LONG DESCRIPTION
19
17
20
18
Functions that specify the ` CmdletBinding ` attribute can access a number of
21
- methods and properties through the ` $pscmdlet ` variable. These methods
22
- include the following methods:
19
+ methods and properties through the ` $pscmdlet ` variable. These methods include
20
+ the following methods:
23
21
24
22
- Input-processing methods that compiled cmdlets use to do their work.
25
-
26
23
- The ` ShouldProcess ` and ` ShouldContinue ` methods that are used to get
27
- user feedback before an action is performed.
28
-
24
+ user feedback before an action is performed.
29
25
- The ` ThrowTerminatingError ` method for generating error records.
30
-
31
26
- Several Write methods that return different types of output.
32
27
33
28
All the methods and properties of the ` PSCmdlet ` class are available to
34
- advanced functions. For more information about these methods and
35
- properties, see ` System.Management.Automation.PSCmdlet ` in the MSDN
36
- (Microsoft Developer Network) library at
37
- http://go.microsoft.com/fwlink/?LinkId=142139 .
38
-
39
- # Input Processing Methods
40
-
41
- The methods described in this section are referred to as the input
42
- processing methods. For functions, these three methods are represented
43
- by the ` Begin ` , ` Process ` , and ` End ` blocks of the function. Each function
44
- must include one or more of these blocks. The Windows PowerShell runtime
45
- uses the code within these blocks when it is running a function. (These
46
- blocks are also available to functions that do not use the ` CmdletBinding `
47
- attribute.)
48
-
49
- ## Begin
29
+ advanced functions. For more information about these methods and properties,
30
+ see [ ` System.Management.Automation.PSCmdlet ` ] ( http://go.microsoft.com/fwlink/?LinkId=142139 ) in the MSDN library.
31
+
32
+ ### Input Processing Methods
33
+
34
+ The methods described in this section are referred to as the input processing
35
+ methods. For functions, these three methods are represented by the ` Begin ` ,
36
+ ` Process ` , and ` End ` blocks of the function. Each function must include one or
37
+ more of these blocks. The Windows PowerShell runtime uses the code within
38
+ these blocks when it is running a function. (These blocks are also available
39
+ to functions that do not use the ` CmdletBinding ` attribute.)
40
+
41
+ #### Begin
42
+
50
43
This block is used to provide optional one-time preprocessing for the
51
- function. The Windows PowerShell runtime uses the code in this block one
52
- time for each instance of the function in the pipeline.
53
-
54
- ## Process
55
- This block is used to provide record-by-record processing for the
56
- function. This block might be used any number of times, or not at all,
57
- depending on the input to the function. For example, if the function is
58
- the first command in the pipeline, the ` Process ` block will be used one
59
- time. If the function is not the first command in the pipeline, the
60
- ` Process ` block is used one time for every input that the function
61
- receives from the pipeline. If there is no pipeline input, the ` Process `
62
- block is not used.
63
-
64
- This block must be defined if a function parameter is set to accept
65
- pipeline input. If this block is not defined and the parameter accepts
66
- input from the pipeline, the function will miss the values that are
67
- passed to the function through the pipeline.
44
+ function. The Windows PowerShell runtime uses the code in this block one time
45
+ for each instance of the function in the pipeline.
46
+
47
+ #### Process
48
+
49
+ This block is used to provide record-by-record processing for the function.
50
+ This block might be used any number of times, or not at all, depending on the
51
+ input to the function. For example, if the function is the first command in
52
+ the pipeline, the ` Process ` block will be used one time. If the function is
53
+ not the first command in the pipeline, the ` Process ` block is used one time
54
+ for every input that the function receives from the pipeline. If there is no
55
+ pipeline input, the ` Process ` block is not used.
56
+
57
+ This block must be defined if a function parameter is set to accept pipeline
58
+ input. If this block is not defined and the parameter accepts input from the
59
+ pipeline, the function will miss the values that are passed to the function
60
+ through the pipeline.
68
61
69
62
Also, when the function supports confirmation requests (when the
70
63
` SupportsShouldProcess ` parameter of the Parameter attribute is set to
71
- ` $True ` ), the call to the ` ShouldProcess ` method must be made from within
72
- the Process block.
64
+ ` $True ` ), the call to the ` ShouldProcess ` method must be made from within the
65
+ Process block.
66
+
67
+ #### End
73
68
74
- ## End
75
- This block is used to provide optional one-time post-processing for
76
- the function.
69
+ This block is used to provide optional one-time post-processing for the
70
+ function.
77
71
78
- The following example shows the outline of a function that contains a
79
- ` Begin ` block for one-time preprocessing, a ` Process ` block for multiple
80
- record processing, and an ` End ` block for one-time post-processing.
72
+ The following example shows the outline of a function that contains a ` Begin `
73
+ block for one-time preprocessing, a ` Process ` block for multiple record
74
+ processing, and an ` End ` block for one-time post-processing.
81
75
82
76
``` powershell
83
77
Function Test-ScriptCmdlet
@@ -90,119 +84,125 @@ Function Test-ScriptCmdlet
90
84
}
91
85
```
92
86
93
- # Confirmation Methods
87
+ ### Confirmation Methods
88
+
89
+ #### ShouldProcess
94
90
95
- ## ShouldProcess
96
91
This method is called to request confirmation from the user before the
97
- function performs an action that would change the system. The function
98
- can continue based on the Boolean value returned by the method. This
99
- method can be called only from within the ` Process{} ` block of the
100
- function. And, the ` CmdletBinding ` attribute must declare that the
101
- function supports ` ShouldProcess ` (as shown in the previous example).
92
+ function performs an action that would change the system. The function can
93
+ continue based on the Boolean value returned by the method. This method can be
94
+ called only from within the ` Process{} ` block of the function. And, the
95
+ ` CmdletBinding ` attribute must declare that the function supports
96
+ ` ShouldProcess ` (as shown in the previous example).
102
97
103
98
For more information about this method, see
104
- ` System.Management.Automation.Cmdlet.ShouldProcess ` in the MSDN library at
105
- http://go.microsoft.com/fwlink/?LinkId=142142 .
99
+ [ ` System.Management.Automation.Cmdlet.ShouldProcess ` ] ( http://go.microsoft.com/fwlink/?LinkId=142142 )
100
+ in the MSDN library .
106
101
107
102
For more information about how to request confirmation, see
108
- "Requesting Confirmation" in the MSDN library at
109
- http://go.microsoft.com/fwlink/?LinkID=136658 .
103
+ [ Requesting Confirmation] ( http://go.microsoft.com/fwlink/?LinkID=136658 )
104
+ in the MSDN library.
105
+
106
+ #### ShouldContinue
110
107
111
- ## ShouldContinue
112
- This method is called to request a second confirmation message. It
113
- should be called when the ` ShouldProcess ` method returns ` $true ` . For more
114
- information about this method, see
115
- ` System.Management.Automation.Cmdlet.ShouldContinue ` in the MSDN library
116
- at http://go.microsoft.com/fwlink/?LinkId=142143 .
108
+ This method is called to request a second confirmation message. It should be
109
+ called when the ` ShouldProcess ` method returns ` $true ` . For more information
110
+ about this method, see ` System.Management.Automation.Cmdlet.ShouldContinue ` in
111
+ the MSDN library at http://go.microsoft.com/fwlink/?LinkId=142143 .
117
112
118
- # Error Methods
113
+ ### Error Methods
119
114
120
115
Functions can call two different methods when errors occur. When a
121
- nonterminating error occurs, the function should call the ` WriteError `
122
- method, which is described in the "Write Methods" section. When a
123
- terminating error occurs and the function cannot continue, it should call
124
- the ` ThrowTerminatingError ` method. You can also use the ` Throw ` statement for
116
+ nonterminating error occurs, the function should call the ` WriteError ` method,
117
+ which is described in the "Write Methods" section. When a terminating error
118
+ occurs and the function cannot continue, it should call the
119
+ ` ThrowTerminatingError ` method. You can also use the ` Throw ` statement for
125
120
terminating errors and the ` Write-Error ` cmdlet for nonterminating errors.
126
121
127
- For more information, see ` System.Management.Automation.Cmdlet.ThrowTerminatingError `
128
- in the MSDN libray at
129
- http://go.microsoft.com/fwlink/?LinkId=142144 .
122
+ For more information, see
123
+ [ ` System.Management.Automation.Cmdlet.ThrowTerminatingError ` ] ( http://go.microsoft.com/fwlink/?LinkId=142144 )
124
+ in the MSDN libray .
130
125
131
- # Write Methods
126
+ ### Write Methods
132
127
133
- A function can call the following methods to return different types of
134
- output. Notice that not all the output goes to the next command in the
135
- pipeline. You can also use the various Write cmdlets, such as
136
- Write-Error.
128
+ A function can call the following methods to return different types of output.
129
+ Notice that not all the output goes to the next command in the pipeline. You
130
+ can also use the various Write cmdlets, such as Write-Error.
131
+
132
+ #### WriteCommandDetail
137
133
138
- ## WriteCommandDetail
139
134
For information about the ` WriteCommandDetails ` method, see
140
- ` System.Management.Automation.Cmdlet.WriteCommandDetail ` in the MSDN
141
- library at http://go.microsoft.com/fwlink/?LinkId=142155 .
142
-
143
- ## WriteDebug
144
- To provide information that can be used to troubleshoot a function,
145
- make the function call the ` WriteDebug ` method. This displays debug
146
- messages to the user. For more information, see
147
- ` System.Management.Automation.Cmdlet.WriteDebug ` in the MSDN library
148
- at http://go.microsoft.com/fwlink/?LinkId=142156 .
149
-
150
- ## WriteError
151
- Functions should call this method when nonterminating errors occur and
152
- the function is designed to continue processing records. For more
153
- information, see ` System.Management.Automation.Cmdlet.WriteError ` in the
154
- MSDN library at http://go.microsoft.com/fwlink/?LinkId=142157 .
135
+ [ ` System.Management.Automation.Cmdlet.WriteCommandDetail ` ] ( http://go.microsoft.com/fwlink/?LinkId=142155 )
136
+ in the MSDN library.
137
+
138
+ #### WriteDebug
139
+
140
+ To provide information that can be used to troubleshoot a function, make the
141
+ function call the ` WriteDebug ` method. This displays debug messages to the
142
+ user. For more information, see
143
+ [ ` System.Management.Automation.Cmdlet.WriteDebug ` ] ( http://go.microsoft.com/fwlink/?LinkId=142156 )
144
+ in the MSDN library.
145
+
146
+ #### WriteError
147
+
148
+ Functions should call this method when nonterminating errors occur and the
149
+ function is designed to continue processing records. For more information, see
150
+ [ ` System.Management.Automation.Cmdlet.WriteError ` ] ( http://go.microsoft.com/fwlink/?LinkId=142157 )
151
+ in the MSDN library.
155
152
156
153
Note: If a terminating error occurs, the function should call the
157
154
` ThrowTerminatingError ` method.
158
155
159
- ## WriteObject
160
- This method allows the function to send an object to the next command in
161
- the pipeline. In most cases, this is the method to use when the function
162
- returns data. For more information, see
163
- ` System.Management.Automation.PSCmdlet.WriteObject ` in the MSDN library at
164
- http://go.microsoft.com/fwlink/?LinkId=142158 .
165
-
166
- ## WriteProgress
167
- For functions whose actions take a long time to complete, this method
168
- allows the function to call the ` WriteProgress ` method so that progress
169
- information is displayed. For example, you can display the percent
170
- completed. For more information, see
171
- ` System.Management.Automation.PSCmdlet.WriteProgress ` in the MSDN library
172
- at http://go.microsoft.com/fwlink/?LinkId=142160 .
173
-
174
- ## WriteVerbose
175
- To provide detailed information about what the function is doing, make
176
- the function call the ` WriteVerbose ` method to display verbose messages to
177
- the user. By default, verbose messages are not displayed. For more
178
- information, see ` System.Management.Automation.PSCmdlet.WriteVerbose `
179
- in the MSDN library at http://go.microsoft.com/fwlink/?LinkId=142162 .
180
-
181
- ## WriteWarning
182
- To provide information about conditions that may cause unexpected
183
- results, make the function call the WriteWarning method to display
184
- warning messages to the user. By default, warning messages are displayed.
185
- For more information, see
186
- ` System.Management.Automation.PSCmdlet.WriteWarning ` in the MSDN library
187
- at http://go.microsoft.com/fwlink/?LinkId=142164 .
156
+ #### WriteObject
157
+
158
+ This method allows the function to send an object to the next command in the
159
+ pipeline. In most cases, this is the method to use when the function returns
160
+ data. For more information, see
161
+ [ ` System.Management.Automation.PSCmdlet.WriteObject ` ] ( http://go.microsoft.com/fwlink/?LinkId=142158 )
162
+ in the MSDN library.
163
+
164
+ #### WriteProgress
165
+
166
+ For functions whose actions take a long time to complete, this method allows
167
+ the function to call the ` WriteProgress ` method so that progress information
168
+ is displayed. For example, you can display the percent completed. For more
169
+ information, see [ ` System.Management.Automation.PSCmdlet.WriteProgress ` ] ( http://go.microsoft.com/fwlink/?LinkId=142160 )
170
+ in the MSDN library.
171
+
172
+ #### WriteVerbose
173
+
174
+ To provide detailed information about what the function is doing, make the
175
+ function call the ` WriteVerbose ` method to display verbose messages to the
176
+ user. By default, verbose messages are not displayed. For more information,
177
+ see
178
+ [ ` System.Management.Automation.PSCmdlet.WriteVerbose ` ] ( http://go.microsoft.com/fwlink/?LinkId=142162 )
179
+ in the MSDN library.
180
+
181
+ #### WriteWarning
182
+
183
+ To provide information about conditions that may cause unexpected results,
184
+ make the function call the WriteWarning method to display warning messages to
185
+ the user. By default, warning messages are displayed. For more information,
186
+ see [ ` System.Management.Automation.PSCmdlet.WriteWarning ` ] ( http://go.microsoft.com/fwlink/?LinkId=142164 )
187
+ in the MSDN library.
188
188
189
189
Note: You can also display warning messages by configuring the
190
190
` WarningPreference ` variable or by using the ` Verbose ` and ` Debug `
191
191
command-line options.
192
192
193
- # Other Methods and Properties
193
+ ### Other Methods and Properties
194
194
195
195
For information about the other methods and properties that can be
196
196
accessed through the ` $PSCmdlet ` variable, see
197
- ` System.Management.Automation.PSCmdlet ` in the MSDN library at
198
- http://go.microsoft.com/fwlink/?LinkId=142139 .
197
+ [ ` System.Management.Automation.PSCmdlet ` ] ( http://go.microsoft.com/fwlink/?LinkId=142139 )
198
+ in the MSDN library .
199
199
200
200
For example, the ` ParameterSetName ` property allows you to see the parameter
201
201
set that is being used. Parameter sets allow you to create a function that
202
- performs different tasks based on the parameters that are specified when
203
- the function is run.
202
+ performs different tasks based on the parameters that are specified when the
203
+ function is run.
204
204
205
- # SEE ALSO
205
+ ## SEE ALSO
206
206
207
207
[ about_Functions] ( about_Functions.md )
208
208
@@ -213,4 +213,3 @@ the function is run.
213
213
[ about_Functions_CmdletBindingAttribute] ( about_Functions_CmdletBindingAttribute.md )
214
214
215
215
[ about_Functions_OutputTypeAttribute] ( about_Functions_OutputTypeAttribute.md )
216
-
0 commit comments