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
> Always use indexer to access value of a HashTable. `.` will prefer Extended Property that might be unexpected.
63
71
72
+
> [!NOTE]
73
+
> Key in HashTable can be any type. So if the key is not a singular type, you'll have to extract the key from `Keys` first and then access the value by the key.
Copy file name to clipboardExpand all lines: docs/document/Powershell/docs/Language/Script Block.md
+13-1Lines changed: 13 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,6 +3,8 @@
3
3
**Script Block** is a special object in powerhsell, it looks like a syntax that creates a new scope in the flow, but itself can be stored in a variable.
4
4
So it's more like a anonymous function, you may call it as lambda expression.
5
5
6
+
## Creation
7
+
6
8
```ps1
7
9
$action = {
8
10
echo 'hello from script block'
@@ -14,6 +16,13 @@ $func = {
14
16
}
15
17
```
16
18
19
+
### From String
20
+
21
+
```ps1
22
+
$script = [scriptblock]::Create('echo hello')
23
+
& $script # hello
24
+
```
25
+
17
26
## Invoke a Script Block
18
27
19
28
```ps1
@@ -24,11 +33,14 @@ $func = {
24
33
& { param($a, $b) $a, $b } 'a' 'b'
25
34
# a
26
35
# b
36
+
37
+
# store result to variable
38
+
$arr = & { param($a, $b) $a, $b } 'a' 'b'
27
39
```
28
40
29
41
## Script Block as Lambda
30
42
31
-
As a replacement for lambda expression, Script Block is being used to work with LINQ api.
0 commit comments