-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscripting-secrets-demo.ps1
310 lines (220 loc) · 7.17 KB
/
scripting-secrets-demo.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<#
In this session longtime PowerShell MVP and sensei Jeff Hicks will share some of
his scripting secrets. Some of them are simple tips that might make your code
more manageable. Others might yield great performance benefits. Whether you are
just beginning with PowerShell or a veteran scripter there should be a secret
worth knowing for everybody.
It is assumed you will be using the PowerShell ISE to run these demos.
#>
#region use a PowerShell-aware editor
notepad .\sample.ps1
psedit .\sample.ps1
#assumes you have VS Code installed
code .\sample.ps1
#endregion
#region use requires statements
psedit .\sample-requires.ps1
help about_requires -ShowWindow
#endregion
#region long pipeline vs several steps
measure-command {
dir ~\Documents -Directory | foreach { $stats = dir $_.fullname -Recurse -File | Measure-Object length -sum ; $_ | Select-Object Fullname,@{Name="Size";Expression={$stats.sum}},@{Name="Files";Expression={$stats.count}}} | Sort Size
}
#you don't always need a one line command
Measure-command {
$folders = dir ~\Documents -Directory
$data = $folders | foreach {
$stats = dir $_.fullname -Recurse -File | Measure-Object length -sum
$_ | Select-Object Fullname,@{Name="Size";Expression={$stats.sum}},
@{Name="Files";Expression={$stats.count}}
}
$data | Sort Size
}
#or try a variation.
#breaking it up also allows for more options
Measure-Command {
$folders = dir ~\Documents -Directory
Write-Verbose "Found $($folders.count) top level folders"
$data = foreach ($item in $folders) {
Write-Verbose "Analyzing $($item.fullname)"
$stats = dir $item.fullname -Recurse -file | measure-object length -sum
Write-Verbose "Found $($stats.count) files"
[pscustomobject]@{
Path = $item.fullname
Size = $stats.Sum
Files = $stats.count
}
}
$data | sort Size
}
#or think outside the box
#not necessarily faster
measure-command {
$path = Convert-Path ~\documents
$grouped = dir $Path -file -recurse | group {
$rxpath = $path.replace("\","\\")
[regex]$rx="^$rxpath\\\w+((\s\w+)?)*(?=\\)"
$rx.match($_.fullname).Value
}
$data = $grouped.where({$_.name}) | foreach {
$stats = $_.group | measure-object length -sum
$_ | Select Name,@{Name="Size";Expression={$stats.sum}},Count
}
$data | sort Size
}
#endregion
#region Splatting with hash tables
psedit .\sample2.ps1
#endregion
#region create [pscustomobject] instead of Select-object with hashtables
Measure-Command {
Get-Ciminstance -ClassName win32_logicalDisk -filter "DriveType=3" |
Select DeviceID,VolumeName,@{Name="SizeGB";Expression={$_.size/1GB -as [int]}},
@{Name="FreeGB";expression={[math]::Round($_.freespace/1gb,4)}},
@{Name="PctFree";Expression={[math]::Round(($_.freespace/$_.size)*100,2)}},
@{Name="DiskPartition";Expression={($_ | Get-CimAssociatedInstance -ResultClassName win32_diskpartition).type }},
@{Name="Drive";Expression= {($_ | Get-CimAssociatedInstance -ResultClassName win32_diskpartition | Get-CimAssociatedInstance -ResultClassName win32_diskdrive).Model}},
@{Name="Computername";Expression={$_.SystemName}}
}
#we can also tweak performance by only getting the properties we need
Measure-command {
$p = @{
ClassName = 'win32_logicalDisk'
filter = "DriveType=3"
Property = "DeviceID","Size","Freespace","VolumeName","SystemName"
}
$disks = Get-Ciminstance @p
foreach ($disk in $disks) {
[pscustomobject]@{
DeviceID = $disk.deviceID
VolumeName = $disk.volumeName
SizeGB = $disk.size/1GB -as [int]
FreeGB = [math]::Round($disk.freespace/1gb,4)
PctFree = [math]::round(($disk.freespace/$disk.size)*100,2)
DiskPartition = ($disk | Get-CimAssociatedInstance -ResultClassName win32_diskpartition).type
Drive = ($disk | Get-CimAssociatedInstance -ResultClassName win32_diskpartition | Get-CimAssociatedInstance -ResultClassName win32_diskdrive).Model
Computername = $disk.SystemName
}
} #foreach
}
#you might also consider using a PowerShell class
#endregion
#region setting psdefaultparametervalues in a function
psedit .\sample3.ps1
#endregion
#region write-progress vs write-host
psedit .\sample4.ps1
psedit S:\Demo-WriteProgress.ps1
#endregion
#region set-content vs out-file vs [system.io.file]
Measure-Command {
1..500 | out-file .\scratch-outfile.txt
}
Measure-command {
1..500 | Set-Content -Path .\scratch-content.txt
}
Measure-Command {
$p = join-path (convert-path .) -ChildPath scratch-io.txt
$f = [System.IO.StreamWriter]::new($p)
1..500 | foreach { $f.WriteLine($_)}
$f.Close()
}
dir scratch*.txt
dir scratch*.txt | foreach { psedit $_.fullname}
del scratch*.txt
get-process | out-file .\scratch-ps.txt
get-process | Set-Content -path .\scratch-ps2.txt
psedit .\scratch-ps.txt
#check this
psedit .\scratch-ps2.tx
get-process | out-string | set-Content -path .\scratch-ps3.txt
psedit .\scratch-ps3.txt
dir scratch-ps*
#fun options
set-content -Path .\scratch.txt -Value $env:computername -Stream Source
get-eventlog -list | out-string | add-content .\scratch.txt
get-content scratch.txt
get-content scratch.txt -stream source
del scratch*.txt
#endregion
#region alias parameters
psedit .\sample2.ps1
#endregion
#region parameter validation vs inline
psedit .\sample5.ps1
#endregion
#region use of Write-Verbose messages
psedit .\sample6.ps1
#endregion
#region code commenting including closing brace
psedit .\sample6.ps1
#endregion
#region use templates and snippets
#this can very based on your editor of choice
get-command -noun IseSnippet
#here's how I use snippets in the PowerShell ISE
psedit .\scratch.ps1
#endregion
#region foreach-object vs foreach
Measure-Command {
dir c:\scripts -Directory | foreach-object {
$stats = (dir $_.fullname -file -Recurse | Measure-Object -Property length -sum)
[pscustomobject]@{
Path = $_.FullName
Files = $stats.count
Size = $stats.sum
}
} | Sort Size -Descending | Select -First 5
}
#challenges with ForEach
Measure-Command {
$dirs = dir c:\scripts -Directory
foreach ($dir in $dirs) {
$stats = (dir $dir.fullname -file -Recurse | Measure-Object -Property length -sum)
[pscustomobject]@{
Path = $dir.FullName
Files = $stats.count
Size = $stats.sum
}
} | sort Size -Descending | Select -First 5
}
Measure-Command {
$dirs = dir c:\scripts -Directory
$data = foreach ($dir in $dirs) {
$stats = (dir $dir.fullname -file -Recurse | Measure-Object -Property length -sum)
[pscustomobject]@{
Path = $dir.FullName
Files = $stats.count
Size = $stats.sum
}
}
$data| sort Size -Descending | Select -First 5
}
#endregion
#region module single psm1 vs multiple files?
cd .\DemoModule
dir | foreach { psedit $_.fullname}
#endregion
#region module export module members AND use a manifest
import-module .\DemoModule.psd1 -force
get-command -Module DemoModule
get-module DemoModule | select exported*
gsi localhost
#modify module files and try again
Remove-module DemoModule
#endregion
#region Platyps for documentation
import-module .\DemoModule.psd1 -force
help Get-SysInfo
#install-module platyps
import-module platyps
get-command -Module platyps
New-MarkdownHelp -Module DemoModule -OutputFolder .\docs -force
dir .\docs | foreach {psedit $_.FullName}
New-ExternalHelp -Path .\docs -OutputPath .\en-us -Force
help Get-SysInfo
help get-os -full
#reset demo
# dir .\docs | del
# dir .\en-us | del
#endregion