-
Notifications
You must be signed in to change notification settings - Fork 2
/
chapter_16.ps1
47 lines (31 loc) · 1019 Bytes
/
chapter_16.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#region Don’t Use the Pipeline
$array = 0..1000000
Measure-Command -Expression {$null = $array.foreach({$_})}
Measure-Command -Expression {$array | foreach-object { $_ }}
#endregion
#region Use the foreach Statement in PowerShell Core
$array = 0..1000000
Measure-Command -Expression { $null = foreach ($item in $array) { $item }}
Measure-Command -Expression { $null = $array.foreach({$_})}
#endregion
#region Use Parallel Processing
## servers.txt
SRV1
SRV2
SRV3
$servers = Get-Content -Path C:\Servers.txt
Invoke-Command -ComputerName $servers -ScriptBlock {
## Do something on the server here
}
$servers = Get-Content -Path C:\Servers.txt
Invoke-Command -ComputerName $servers -AsJob -ScriptBlock {
## Do something on the server here
}
#endregion
#region Use the .NET StreamReader Class When Reading Large Text Files
Get-Content -Path C:\MyHugeFile.txt
$sr = New-Object -Type System.IO.StreamReader -ArgumentList 'C:\MyHugeFile.txt'
while ($sr.Peek() -ge 0) {
$sr.ReadLine()
}
#endregion