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