@@ -5,6 +5,7 @@ locale: en-us
5
5
keywords : powershell,cmdlet
6
6
title : about_Functions_Advanced_Methods
7
7
---
8
+
8
9
# About Functions Advanced Methods
9
10
10
11
## Short description
@@ -15,54 +16,64 @@ methods and properties that are available to compiled cmdlets.
15
16
## Long description
16
17
17
18
Functions that specify the ` CmdletBinding ` attribute can access a number of
18
- methods and properties through the ` $pscmdlet ` variable. These methods include
19
+ methods and properties through the ` $PSCmdlet ` variable. These methods include
19
20
the following methods:
20
21
21
22
- Input-processing methods that compiled cmdlets use to do their work.
22
- - The ` ShouldProcess ` and ` ShouldContinue ` methods that are used to get
23
- user feedback before an action is performed.
23
+ - The ` ShouldProcess ` and ` ShouldContinue ` methods that are used to get user
24
+ feedback before an action is performed.
24
25
- The ` ThrowTerminatingError ` method for generating error records.
25
- - Several Write methods that return different types of output.
26
+ - Several ` Write ` methods that return different types of output.
27
+
28
+ All the methods and properties of the ** PSCmdlet** class are available to
29
+ advanced functions. For more information, see
30
+ [ System.Management.Automation.PSCmdlet] ( /dotnet/api/system.management.automation.pscmdlet ) .
26
31
27
- All the methods and properties of the ` PSCmdlet ` class are available to
28
- advanced functions. For more information about these methods and properties,
29
- see [ ` System.Management.Automation.PSCmdlet ` ] ( http://go.microsoft.com/fwlink/?LinkId=142139 ) in the MSDN library.
32
+ For more information about the ` CmdletBinding ` attribute, see
33
+ [ about_Functions_CmdletBindingAttribute] ( about_Functions_CmdletBindingAttribute.md ) .
34
+ For the ** CmdletBindingAttribute** class, see
35
+ [ System.Management.Automation.Cmdlet.CmdletBindingAttribute] ( /dotnet/api/system.management.automation.cmdletbindingattribute ) .
30
36
31
- ### Input Processing Methods
37
+ ### Input processing methods
32
38
33
39
The methods described in this section are referred to as the input processing
34
40
methods. For functions, these three methods are represented by the ` Begin ` ,
35
- ` Process ` , and ` End ` blocks of the function. You are not required to use
36
- any of these blocks in your functions.
41
+ ` Process ` , and ` End ` blocks of the function. You aren't required to use any of
42
+ these blocks in your functions.
37
43
38
44
> [ !NOTE]
39
- > These blocks are also available to functions that do not use the
45
+ > These blocks are also available to functions that don't use the
40
46
> ` CmdletBinding ` attribute.
41
47
42
48
#### Begin
43
49
44
- This block is used to provide optional one-time preprocessing for the
45
- function. The PowerShell runtime uses the code in this block one time
46
- for each instance of the function in the pipeline.
50
+ This block is used to provide optional one-time preprocessing for the function.
51
+ The PowerShell runtime uses the code in this block once for each instance of
52
+ the function in the pipeline.
47
53
48
54
#### Process
49
55
50
- This block is used to provide record-by-record processing for the function.
51
- You can use a ` Process ` block without defining the other blocks. The number
52
- of ` Process ` block executions depends on how you use the function and what
53
- input the function receives.
54
-
55
- - Calling the function at the beginning, or outside of a pipeline, executes
56
- the ` Process ` block once.
57
- - Within a pipeline, the ` Process ` block executes once for each input
58
- object that reaches the function.
59
- - If the pipeline input that reaches the function is empty, the ` Process `
60
- block does NOT execute.
56
+ This block is used to provide record-by-record processing for the function. You
57
+ can use a ` Process ` block without defining the other blocks. The number of
58
+ ` Process ` block executions depends on how you use the function and what input
59
+ the function receives.
60
+
61
+ The automatic variable ` $_ ` or ` $PSItem ` contains the current object in the
62
+ pipeline for use in the ` Process ` block. The ` $input ` automatic variable
63
+ contains an enumerator that's only available to functions and script blocks.
64
+ For more information, see [ about_Automatic_Variables] ( about_Automatic_Variables.md ) .
65
+
66
+ - Calling the function at the beginning, or outside of a pipeline, executes the
67
+ ` Process ` block once.
68
+ - Within a pipeline, the ` Process ` block executes once for each input object
69
+ that reaches the function.
70
+ - If the pipeline input that reaches the function is empty, the ` Process ` block
71
+ ** does not** execute.
61
72
- The ` Begin ` and ` End ` blocks still execute.
62
73
63
74
> [ !IMPORTANT]
64
75
> If a function parameter is set to accept pipeline input, and a ` Process `
65
- > block is not defined, record-by-record processing will fail. In this case,
76
+ > block isn't defined, record-by-record processing will fail. In this case,
66
77
> your function will only execute once, regardless of the input.
67
78
68
79
#### End
@@ -87,128 +98,124 @@ Function Test-ScriptCmdlet
87
98
88
99
> [ !NOTE]
89
100
> Using either a ` Begin ` or ` End ` block requires that you define all three
90
- > blocks. When using all three blocks, all PowerShell code must be inside
91
- > one of the blocks.
101
+ > blocks. When using all three blocks, all PowerShell code must be inside one
102
+ > of the blocks.
92
103
93
- ### Confirmation Methods
104
+ ### Confirmation methods
94
105
95
106
#### ShouldProcess
96
107
97
- This method is called to request confirmation from the user before the
98
- function performs an action that would change the system. The function can
99
- continue based on the Boolean value returned by the method. This method can
100
- only be called only from within the ` Process{} ` block of the function. The
108
+ This method is called to request confirmation from the user before the function
109
+ performs an action that would change the system. The function can continue
110
+ based on the Boolean value returned by the method. This method can only be
111
+ called only from within the ` Process{} ` block of the function. The
101
112
` CmdletBinding ` attribute must also declare that the function supports
102
113
` ShouldProcess ` (as shown in the previous example).
103
114
104
115
For more information about this method, see
105
- [ ` System.Management.Automation.Cmdlet.ShouldProcess ` ] ( http://go.microsoft.com/fwlink/?LinkId=142142 )
106
- in the MSDN library.
116
+ [ System.Management.Automation.Cmdlet.ShouldProcess] ( /dotnet/api/system.management.automation.cmdlet.shouldprocess ) .
107
117
108
118
For more information about how to request confirmation, see
109
- [ Requesting Confirmation] ( http://go.microsoft.com/fwlink/?LinkID=136658 )
110
- in the MSDN library.
119
+ [ Requesting Confirmation] ( /powershell/developer/cmdlet/requesting-confirmation ) .
111
120
112
121
#### ShouldContinue
113
122
114
123
This method is called to request a second confirmation message. It should be
115
124
called when the ` ShouldProcess ` method returns ` $true ` . For more information
116
- about this method, see ` System.Management.Automation.Cmdlet.ShouldContinue ` in
117
- the MSDN library at http://go.microsoft.com/fwlink/?LinkId=142143 .
125
+ about this method, see
126
+ [ System.Management.Automation.Cmdlet.ShouldContinue ] ( /dotnet/api/system.management.automation.cmdlet.shouldcontinue ) .
118
127
119
- ### Error Methods
128
+ ### Error methods
120
129
121
130
Functions can call two different methods when errors occur. When a
122
131
non-terminating error occurs, the function should call the ` WriteError ` method,
123
- which is described in the " Write Methods" section. When a terminating error
124
- occurs and the function cannot continue, it should call the
132
+ which is described in the ` Write ` methods section. When a terminating error
133
+ occurs and the function can't continue, it should call the
125
134
` ThrowTerminatingError ` method. You can also use the ` Throw ` statement for
126
- terminating errors and the ` Write-Error ` cmdlet for nonterminating errors.
135
+ terminating errors and the [ Write-Error] ( ../../Microsoft.PowerShell.Utility/Write-Error.md )
136
+ cmdlet for non-terminating errors.
127
137
128
138
For more information, see
129
- [ ` System.Management.Automation.Cmdlet.ThrowTerminatingError ` ] ( http://go.microsoft.com/fwlink/?LinkId=142144 )
130
- in the MSDN libray.
139
+ [ System.Management.Automation.Cmdlet.ThrowTerminatingError] ( /dotnet/api/system.management.automation.cmdlet.throwterminatingerror ) .
131
140
132
- ### Write Methods
141
+ ### Write methods
133
142
134
143
A function can call the following methods to return different types of output.
135
144
Notice that not all the output goes to the next command in the pipeline. You
136
- can also use the various Write cmdlets, such as Write-Error.
145
+ can also use the various ` Write ` cmdlets, such as ` Write-Error ` .
137
146
138
147
#### WriteCommandDetail
139
148
140
149
For information about the ` WriteCommandDetails ` method, see
141
- [ ` System.Management.Automation.Cmdlet.WriteCommandDetail ` ] ( http://go.microsoft.com/fwlink/?LinkId=142155 )
142
- in the MSDN library.
150
+ [ System.Management.Automation.Cmdlet.WriteCommandDetail] ( /dotnet/api/system.management.automation.cmdlet.writecommanddetail ) .
143
151
144
152
#### WriteDebug
145
153
146
154
To provide information that can be used to troubleshoot a function, make the
147
- function call the ` WriteDebug ` method. This displays debug messages to the
148
- user. For more information, see
149
- [ ` System.Management.Automation.Cmdlet.WriteDebug ` ] ( http://go.microsoft.com/fwlink/?LinkId=142156 )
150
- in the MSDN library.
155
+ function call the ` WriteDebug ` method. The ` WriteDebug ` method displays debug
156
+ messages to the user. For more information, see
157
+ [ System.Management.Automation.Cmdlet.WriteDebug] ( /dotnet/api/system.management.automation.cmdlet.writedebug ) .
151
158
152
159
#### WriteError
153
160
154
- Functions should call this method when nonterminating errors occur and the
161
+ Functions should call this method when non-terminating errors occur and the
155
162
function is designed to continue processing records. For more information, see
156
- [ ` System.Management.Automation.Cmdlet.WriteError ` ] ( http://go.microsoft.com/fwlink/?LinkId=142157 )
157
- in the MSDN library.
163
+ [ System.Management.Automation.Cmdlet.WriteError] ( /dotnet/api/system.management.automation.cmdlet.writeerror ) .
158
164
159
- Note: If a terminating error occurs, the function should call the
160
- ` ThrowTerminatingError ` method.
165
+ > [ !NOTE]
166
+ > If a terminating error occurs, the function should call the
167
+ > [ ThrowTerminatingError] ( /dotnet/api/system.management.automation.cmdlet.throwterminatingerror )
168
+ > method.
161
169
162
170
#### WriteObject
163
171
164
- This method allows the function to send an object to the next command in the
165
- pipeline. In most cases, this is the method to use when the function returns
166
- data. For more information, see
167
- [ ` System.Management.Automation.PSCmdlet.WriteObject ` ] ( http://go.microsoft.com/fwlink/?LinkId=142158 )
168
- in the MSDN library.
172
+ The ` WriteObject ` method allows the function to send an object to the next
173
+ command in the pipeline. In most cases, ` WriteObject ` is the method to use when
174
+ the function returns data. For more information, see
175
+ [ System.Management.Automation.PSCmdlet.WriteObject] ( /dotnet/api/system.management.automation.cmdlet.writeobject ) .
169
176
170
177
#### WriteProgress
171
178
172
- For functions whose actions take a long time to complete, this method allows
173
- the function to call the ` WriteProgress ` method so that progress information
174
- is displayed. For example, you can display the percent completed. For more
175
- information, see [ ` System.Management.Automation.PSCmdlet.WriteProgress ` ] ( http://go.microsoft.com/fwlink/?LinkId=142160 )
176
- in the MSDN library .
179
+ For functions with actions that take a long time to complete, this method
180
+ allows the function to call the ` WriteProgress ` method so that progress
181
+ information is displayed. For example, you can display the percent completed.
182
+ For more information, see
183
+ [ System.Management.Automation.PSCmdlet.WriteProgress ] ( /dotnet/api/system.management.automation.cmdlet.writeprogress ) .
177
184
178
185
#### WriteVerbose
179
186
180
187
To provide detailed information about what the function is doing, make the
181
188
function call the ` WriteVerbose ` method to display verbose messages to the
182
- user. By default, verbose messages are not displayed. For more information,
183
- see
184
- [ ` System.Management.Automation.PSCmdlet.WriteVerbose ` ] ( http://go.microsoft.com/fwlink/?LinkId=142162 )
185
- in the MSDN library.
189
+ user. By default, verbose messages aren't displayed. For more information, see
190
+ [ System.Management.Automation.PSCmdlet.WriteVerbose] ( /dotnet/api/system.management.automation.cmdlet.writeverbose ) .
186
191
187
192
#### WriteWarning
188
193
189
- To provide information about conditions that may cause unexpected results,
190
- make the function call the WriteWarning method to display warning messages to
191
- the user. By default, warning messages are displayed. For more information,
192
- see [ ` System.Management.Automation.PSCmdlet.WriteWarning ` ] ( http://go.microsoft.com/fwlink/?LinkId=142164 )
193
- in the MSDN library.
194
+ To provide information about conditions that may cause unexpected results, make
195
+ the function call the WriteWarning method to display warning messages to the
196
+ user. By default, warning messages are displayed. For more information, see
197
+ [ System.Management.Automation.PSCmdlet.WriteWarning] ( /dotnet/api/system.management.automation.cmdlet.writewarning ) .
194
198
195
- Note: You can also display warning messages by configuring the
196
- ` WarningPreference ` variable or by using the ` Verbose ` and ` Debug `
197
- command-line options.
199
+ > [ !NOTE]
200
+ > You can also display warning messages by configuring the ` $WarningPreference `
201
+ > variable or by using the ` Verbose ` and ` Debug ` command-line options. for more
202
+ > information about the ` $WarningPreference ` variable, see [ about_Preference_Variables] ( about_Preference_Variables.md ) .
203
+
204
+ ### Other methods and properties
198
205
199
- ### Other Methods and Properties
206
+ For information about the other methods and properties that can be accessed
207
+ through the ` $PSCmdlet ` variable, see
208
+ [ System.Management.Automation.PSCmdlet] ( /dotnet/api/system.management.automation.pscmdlet ) .
200
209
201
- For information about the other methods and properties that can be
202
- accessed through the ` $PSCmdlet ` variable, see
203
- [ ` System.Management.Automation.PSCmdlet ` ] ( http://go.microsoft.com/fwlink/?LinkId=142139 )
204
- in the MSDN library.
210
+ For example, the
211
+ [ ParameterSetName] ( /dotnet/api/system.management.automation.pscmdlet.parametersetname )
212
+ property allows you to see the parameter set that is being used. Parameter sets
213
+ allow you to create a function that performs different tasks based on the
214
+ parameters that are specified when the function is run.
205
215
206
- For example, the ` ParameterSetName ` property allows you to see the parameter
207
- set that is being used. Parameter sets allow you to create a function that
208
- performs different tasks based on the parameters that are specified when the
209
- function is run.
216
+ ## See also
210
217
211
- ## SEE ALSO
218
+ [ about_Automatic_Variables ] ( about_Automatic_Variables.md )
212
219
213
220
[ about_Functions] ( about_Functions.md )
214
221
@@ -218,4 +225,6 @@ function is run.
218
225
219
226
[ about_Functions_CmdletBindingAttribute] ( about_Functions_CmdletBindingAttribute.md )
220
227
221
- [ about_Functions_OutputTypeAttribute] ( about_Functions_OutputTypeAttribute.md )
228
+ [ about_Functions_OutputTypeAttribute] ( about_Functions_OutputTypeAttribute.md )
229
+
230
+ [ about_Preference_Variables] ( about_Preference_Variables.md )
0 commit comments