You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/document/Powershell/docs/Language/Function.md
+82-2Lines changed: 82 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -51,6 +51,20 @@ function Foo {
51
51
>
52
52
> Default type of a parameter is `System.Object`.
53
53
54
+
### Implicit Parameter
55
+
56
+
If no parameter name was set, all value passed in will be captured by `$args`, an `object[]`.
57
+
58
+
The following example makes an alias for Neovim to mimic the original Vim cli.
59
+
60
+
```ps1
61
+
function vim {
62
+
nvim --clean -c 'source ~/.vimrc' @args
63
+
}
64
+
65
+
vim ./foo.txt
66
+
```
67
+
54
68
### Positional Parameter
55
69
56
70
Positional parameters allows passing values with explicit names.
@@ -69,6 +83,24 @@ Foo -Foo foo -Bar bar
69
83
Foo foo bar # it's the same # [!code highlight]
70
84
```
71
85
86
+
Or use a explicit position argument on attribute.
87
+
88
+
```ps1
89
+
function Foo {
90
+
param (
91
+
[Parameter(Position=1)] # [!code highlight]
92
+
[string] $Bar
93
+
[Parameter(Position=0)] # [!code highlight]
94
+
[string] $Foo,
95
+
)
96
+
97
+
Write-Output "$Foo $Bar"
98
+
}
99
+
100
+
Foo -Foo foo -Bar bar
101
+
Foo foo bar # it's the same # [!code highlight]
102
+
```
103
+
72
104
### Default Parameter
73
105
74
106
```ps1
@@ -97,6 +129,12 @@ function Foo {
97
129
Foo -Foo -Bar $true # [!code highlight]
98
130
```
99
131
132
+
Manual assignment is also available:
133
+
134
+
```ps1
135
+
Foo -f:$false -b $true
136
+
```
137
+
100
138
### Required Parameter
101
139
102
140
All parameters are optional by default. Use `[Parameter(Mandatory=$true)]` to mark it as required.
@@ -108,17 +146,26 @@ param (
108
146
)
109
147
```
110
148
149
+
> [!NOTE]
150
+
> You can omit assignment for boolean attribute parameter.
151
+
>```ps1
152
+
>param (
153
+
> [Parameter(Mandatory)] # Mandatory is true now # [!code highlight]
154
+
> [string]$RequiredName
155
+
>)
156
+
>```
157
+
111
158
### Parameter Alias
112
159
113
160
Parameters can have aliases. It's not needed for most of time though since pwsh can distinguish option by leading string.
114
161
115
162
```ps1
116
163
function Person {
117
164
param (
118
-
[Alias("n")] # [!code highlight]
165
+
[Alias('n')] # [!code highlight]
119
166
[string]$Name,
120
167
121
-
[Alias("a")] # [!code highlight]
168
+
[Alias('a', 'yearsold')] # can have multiple aliases! # [!code highlight]
122
169
[int]$Age
123
170
)
124
171
Write-Host "Name: $Name, Age: $Age"
@@ -127,6 +174,39 @@ function Person {
127
174
Person -n "Alice" -a 30 # [!code highlight]
128
175
```
129
176
177
+
### Parameter Validation
178
+
179
+
Pass a validation logic as script block to `ValidateScript` attribute, `$_` represents singular value of the parameter or current item of a collection.
180
+
Will throw an error if any parameter does not satisfies the condition.
181
+
182
+
```ps1
183
+
param (
184
+
[ValidateScript({ ($_ % 2) -ne 0 })]
185
+
[int[]]$Odd
186
+
[ValidateScript({ $_.Length < 5 })]
187
+
[string]$Name
188
+
)
189
+
```
190
+
191
+
## Named Blocks
192
+
193
+
In a simple function where there's only one series of parameters being taken, we don't have to use any complex logic.
194
+
But things will explode when we're dealing with a pipeline input which might bring multiple objects.
195
+
196
+
The pipeline mechanism is essentially based on the `Enumerator` so if we collect all items into a new collection as parameter value, it can be a huge performance issue.
197
+
So named blocks are essentially to defined a shared process logic for each object in the pipeline input, and other logic like initializationa and finalization.
198
+
199
+
> [!NOTE]
200
+
> When no named block were specified, `end` block is used to represent the whole logic of a simple function.
Copy file name to clipboardExpand all lines: docs/document/Powershell/docs/Understanding Pipeline.md
+29-4Lines changed: 29 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -9,23 +9,48 @@ Overview of pipeline in powershell:
9
9
## Pipeline Parameter Binding
10
10
11
11
12
-
## How Cmdlet Accept a Pipeline Input
12
+
## How Cmdlet Accept Pipeline Input
13
13
14
14
There's two solution when a pipeline input comes in as a fallback:
15
15
16
-
- ByValue: accepts when the coming object can be cast to the target type of the parameter.
16
+
- ByValue: the default strategy. Accepts when the coming object can be cast or converted to the target type of the parameter.
17
17
- ByPropertyName: accepts when the coming object has property name matched to any parameter name of the cmdlet.
18
18
19
+
### By Value
20
+
21
+
22
+
23
+
### By PropertyName
19
24
20
25
```ps1
21
26
spps -Name (gci -File | foreach Name)
22
-
# is equivalent to
27
+
# is equivalent to the following
23
28
# because FileInfo has Name which matches to -Name parameter of spps cmdlet
24
29
gci -File | spps
25
30
```
26
31
32
+
27
33
> [!WARNING]
28
34
> If multiple matches exist on ByPropertyName solution, powershell throws an error since these paramters might not be allowed to be used together.
29
35
30
-
By value is always tried first, and then use ByPropertyName, or it finally throws.
36
+
ByValue is always tried first, and then use ByPropertyName, or it finally throws.
31
37
A parameter accepts pipeline input does not necessarily have both solutions, it can have at least one of them.
38
+
39
+
## How PowerShell Enumerate Pipeline Input
40
+
41
+
As we know, PowerShell can handle multiple objects from an enumerator from object that implements `IEnumerable` or `IEnumerable<T>`, or even duck typed with `GetEnumerator`.
42
+
43
+
While for types that are not linear collection, manually invoking `GetEnumerator` is required when being passed as pipeline input.
44
+
45
+
-`IDictionary<,>` and `IDictionary`
46
+
- HashTable has dynamic typing so we can't presume a uniformed calculation for our cmdlet
47
+
-`string` is `IEnumerable` but we surely don't expect the auto enumeration.
0 commit comments