-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy path1075.ps1
More file actions
627 lines (492 loc) · 26 KB
/
Copy path1075.ps1
File metadata and controls
627 lines (492 loc) · 26 KB
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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
##############################################################################
#.AUTHOR
# Josh Einstein
# Einstein Technologies, LLC
##############################################################################
$BuiltInAliases = @{}
$BuiltInCmdlets = @{}
$BuiltInFunctions = @{}
$BuiltInVariables = @{}
##############################################################################
#.SYNOPSIS
# Gets a list of tokenized strings from the specified PowerShell code sample
# which can be used for dependency analysis or other unique purposes.
#
#.DESCRIPTION
# This command will use the currently active PowerShell ISE editor tab if the
# current shell is PowerShell ISE and a block of code was not provided to
# the command.
#
#.PARAMETER Type
# If specified, returns only PSTokens of the specified type(s).
#
#.PARAMETER Token
# If specified, returns only PSTokens that contain the specified text.
#
#.PARAMETER Text
# The block of text to tokenize. If this parameter is not specified, and the
# current shell is PowerShell ISE, the current editor tab's text will be used
# otherwise, no output is returned.
#
#.PARAMETER Path
# Specifies the path to an item. Wildcards are permitted.
#
#.PARAMETER LiteralPath
# Specifies the path to an item. Unlike Path, the value of LiteralPath is
# used exactly as it is typed. No characters are interpreted as wildcards.
# If the path includes escape characters, enclose it in single quotation marks.
# Single quotation marks tell Windows PowerShell not to interpret any
# characters as escape sequences.
#
#.PARAMETER Lines
# If specified, returns only the tokens that are on one of the line numbers
# specified. This is useful for doing a two-pass check as in the first example
# which aims to list function declarations which consist of two related tokens,
# the keyword containing the word "function" and the command argument containing
# the function name. It is assumed they will both be on the same line.
#
#.EXAMPLE
# $FunctionLines = Get-PSToken -Type Keyword -Token function | %{ $_.StartLine }
# $FunctionNames = Get-PSToken -Type CommandArgument | ?{ $FunctionLines -contains $_.StartLine }
#
#.LINK
# Get-Dependency
#
#.RETURNVALUE
# A collection of PSTokens parsed from the source code.
##############################################################################
function Get-PSToken {
[CmdletBinding(DefaultParameterSetName='Selection')]
param(
[Parameter(Position=1)]
[String[]]$Type,
[Parameter(Position=2)]
[String[]]$Token,
[Parameter(ParameterSetName='Path', Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[String[]]$Path,
[Alias("PSPath")]
[Parameter(ParameterSetName='LiteralPath', Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[String[]]$LiteralPath,
[Parameter()]
[Int32[]]$Lines
)
process {
if ($PSCmdlet.ParameterSetName -eq 'Selection') {
if (-not $PSISE) { throw 'The Selection parameter set is not valid outside of the PowerShell ISE.' }
if (-not $PSISE.CurrentOpenedFile) { throw 'There is no file currently opened.' }
if (-not $PSISE.CurrentOpenedFile.IsSaved) { throw 'Please save the currently active document first.' }
if ($PSISE.CurrentOpenedFile.IsUntitled) { throw 'Please save the currently active document first.' }
$ResolvedPaths = @(Resolve-Path -LiteralPath $PSISE.CurrentOpenedFile.FullPath)
}
elseif ($PSCmdlet.ParameterSetName -match '^(Literal)?Path$') {
switch ($PSCmdlet.ParameterSetName) {
Path { $ResolvedPaths = @(Resolve-Path -Path $Path) }
LiteralPath { $ResolvePathArgs = @(Resolve-Path -LiteralPath $LiteralPath) }
}
}
# Delegate path expansion to Resolve-Path
foreach ($ResolvedPath in $ResolvedPaths) {
$ScriptContent = Get-Content $ResolvedPath
# Call the PSParser to tokenize!
$ParserErrors = [System.Management.Automation.PSParseError[]]@()
$ParserTokens = [System.Management.Automation.PSParser]::Tokenize($ScriptContent, [Ref]$ParserErrors)
# If there were any errors, write them out as warnings
for ($i=0; $i -lt $ParserErrors.Length; $i++) {
$ParserError = $ParserErrors[$i]
if (-not $ParserError.Token) { Write-Warning $ParserError.Message }
else { Write-Warning "($($ParserError.Token.StartLine), $($ParserError.Token.StartColumn)) $($ParserError.Message)" }
}
if (-not $ParserTokens.Count) { return }
# Filter the output
if ($Type.Length) { $ParserTokens = @($ParserTokens | ?{ $Type -contains $_.Type }) } # Filter By Type
if ($Token.Length) { $ParserTokens = @($ParserTokens | ?{ $Token -contains $_.Content }) } # Filter By Content
if ($Lines.Length) { $ParserTokens = @($ParserTokens | ?{ $Lines -contains $_.StartLine }) } # Filter By Line
if (-not $ParserTokens.Count) { return }
# Return the tokens, adding a Path property to each one
# that points back to the script or editor file
# note that text blocks will have a null path
$ParserTokens | Add-Member -PassThru NoteProperty Script (Split-Path -Leaf $ResolvedPath)
}
} # process
}
##############################################################################
#.SYNOPSIS
# Calculates the dependencies of a script file, block of PowerShell code, or
# an open PowerShell ISE document.
#
#.DESCRIPTION
# Before deploying a script or module, it is important to ensure that any
# external dependencies are resolved otherwise code that runs fine on your
# machine will bomb on someone else's. This function will scan a single
# level of dependencies from the specified script and by default returns
# any dependency that is 1) not a part of the built-in PowerShell command
# and variable configuration and 2) not defined in the script being analyzed.
# You can override this behavior and include these dependencies with the
# Force parameter.
#
#.PARAMETER Path
# Specifies the path to an item. Wildcards are permitted.
#
#.PARAMETER LiteralPath
# Specifies the path to an item. Unlike Path, the value of LiteralPath is
# used exactly as it is typed. No characters are interpreted as wildcards.
# If the path includes escape characters, enclose it in single quotation marks.
# Single quotation marks tell Windows PowerShell not to interpret any
# characters as escape sequences.
#
#.PARAMETER Text
# The block of text to tokenize. If this parameter is not specified, and the
# current shell is PowerShell ISE, the current editor tab's text will be used
# otherwise, no output is returned.
#
#.PARAMETER Unresolved
# When specified, only unresolved dependencies are returned.
#
#.PARAMETER Force
# When specified, all dependencies are included, even if they are known to
# be defined locally or in the default PowerShell configuration.
#
#.EXAMPLE
# Get-Dependency | Out-GridView
#
#.LINK
# Get-PSToken
#
#.RETURNVALUE
# An array of PSObjects containing information about the external dependencies.
##############################################################################
function Get-Dependency {
[CmdletBinding(DefaultParameterSetName='Selection')]
param(
[Parameter(ParameterSetName='Path', Position=1, Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
[String[]]$Path,
[Alias("PSPath")]
[Parameter(ParameterSetName='LiteralPath', Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[String[]]$LiteralPath,
[Parameter()]
[Switch]$Unresolved,
[Parameter()]
[Switch]$Force
)
begin {
$Dependencies = New-Object System.Collections.ArrayList
$LocalFunctions = @{}
$ImportedAliases = @{}
$ImportedCmdlets = @{}
$ImportedFunctions = @{}
##############################################################################
#.SYNOPSIS
# Strips off the scope modifier of an identifier so its name can be checked.
##############################################################################
filter Normalize-Identifier([String]$Name) {
if ($_ -is [System.Management.Automation.PSToken]) { $Name = $_.Content }
if ($_ -is [String]) { $Name = $_ }
$Name -replace '^$?(Script|Global|Local):',''
}
##############################################################################
#.SYNOPSIS
# Wraps calls to Get-PSToken, making sure to use the appropriate parameter set.
##############################################################################
function Get-PSTokenProxy([String[]]$Type,[String[]]$Token,[Int32[]]$Lines) {
$GetPSTokenArgs = @{}
if ($Type.Length) { $GetPSTokenArgs['Type'] = $Type }
if ($Token.Length) { $GetPSTokenArgs['Token'] = $Token }
if ($Lines.Length) { $GetPSTokenArgs['Lines'] = $Lines }
$ResolvedPaths | Get-PSToken @GetPSTokenArgs
}
##############################################################################
#.SYNOPSIS
# Rebuilds the cache of built-in command, alias, function, and variable names
# that are present in an unconfigured PowerShell session so that they can
# be skipped from dependency checking.
##############################################################################
function Discover-BuiltInCommands {
if ($BuiltInCmdlets.Count -eq 0) {
$BuiltInAliases.Clear()
$BuiltInCmdlets.Clear()
$BuiltInFunctions.Clear()
$BuiltInVariables.Clear()
$Posh = [PowerShell]::Create()
try {
[Void]$Posh.AddScript('Get-Command -CommandType Cmdlet,Function,Alias')
foreach($CommandInfo in $Posh.Invoke()) {
switch($CommandInfo.CommandType) {
Alias { $BuiltInAliases[$CommandInfo.Name] = $true }
Cmdlet { $BuiltInCmdlets[$CommandInfo.Name] = $true }
Function { $BuiltInFunctions[$CommandInfo.Name] = $true }
}
}
[Void]$Posh.AddScript('Get-Variable')
foreach($VariableInfo in $Posh.Invoke()) {
$BuiltInVariables[$VariableInfo.Name] = $true
}
# Note: PSISE won't appear as a built-in variable this way
# I decided I actually prefer it that way. I want to know if
# I accidentially couple a script to the ISE
$BuiltInVariables['this'] = $true
}
finally {
$Posh.Dispose()
}
} # if count 0
}
##############################################################################
#.SYNOPSIS
# Scans the source code for Import-Module statements and gathers up the
# module export information into hashtables.
##############################################################################
function Discover-ModuleImports {
$ImportedAliases.Clear()
$ImportedCmdlets.Clear()
$ImportedFunctions.Clear()
$TokenLines = @(Get-PSTokenProxy Command Import-Module | %{$_.StartLine})
if (-not $TokenLines.Length) { return } # nothing to do
$TokenNames = @(Get-PSTokenProxy CommandArgument -Lines $TokenLines | Normalize-Identifier)
if (-not $TokenNames.Length) { return } # nothing to do
# Find out which referenced modules are not imported
$MissingModules = @()
foreach ($TokenName in $TokenNames) {
if (-not (Get-Module $TokenName)) {
$MissingModules += $TokenName
}
}
if ($MissingModules.Length) {
$MenuItem1 = New-Object System.Management.Automation.Host.ChoiceDescription "&No - Do Not Import Modules"
$MenuItem2 = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes - Import These Modules"
[System.Management.Automation.Host.ChoiceDescription[]]$MenuItems = @($MenuItem1,$MenuItem2)
$MenuCaption = "Imported Modules Not Loaded"
$MenuMessage = "One or more modules imported by the script are not currently loaded.`r`n" +
"This can lead to unresolved external references and missing`r`n" +
"information in the dependency report.`r`n" +
"`r`n" +
"You can attempt to temporarily import the following modules into the`r`n" +
"session by selection the option below.`r`n" +
"`r`n"
foreach ($MissingModule in $MissingModules) {
$MenuMessage += " Import-Module $MissingModule`r`n"
}
# Show the menu
# If the user picks choice Y then import the modules
# Because you can't import modules into the global scope from a lower scope,
# the imported module will not be visible from outside of the current scope.
$Response = $Host.UI.PromptForChoice($MenuCaption, $MenuMessage, $MenuItems, 0)
if ($Response = 1) {
foreach ($MissingModule in $MissingModules) {
# Using Invoke-Expression so this Import-Module command
# doesn't cause a screw-up when checking dependencies of
# Dependency.psm1. Gay huh...
Invoke-Expression ".{Import-Module $MissingModule -ErrorAction Continue}"
}
}
}
$ImportedModules = @(Get-Module $TokenNames -ListAvailable) + @(Get-Module $TokenNames)
foreach ($ImportedModule in $ImportedModules) {
$ImportedModule.ExportedAliases.Values | %{ $ImportedAliases[$_.Name] = $_ }
$ImportedModule.ExportedCmdlets.Values | %{ $ImportedCmdlets[$_.Name] = $_ }
$ImportedModule.ExportedFunctions.Values | %{ $ImportedFunctions[$_.Name] = $_ }
}
}
##############################################################################
#.SYNOPSIS
# Scans the source code for function and filter keywords that define local
# functions that should not be treated as external dependencies.
##############################################################################
function Discover-LocalFunctions {
$LocalFunctions.Clear()
# Gather information about defined functions
$TokenLines = @(Get-PSTokenProxy Keyword function,filter | %{$_.StartLine})
if (-not $TokenLines.Length) { return } # nothing to do
$TokenNames = @(Get-PSTokenProxy CommandArgument -Lines $TokenLines | Normalize-Identifier)
if (-not $TokenNames.Length) { return } # nothing to do
# No CommandInfo for these so we'll just store a bool
foreach ($TokenName in $TokenNames) {
$LocalFunctions[$TokenName] = $true
}
}
##############################################################################
#.SYNOPSIS
# Removes references to built-in Cmdlets, functions, variables, etc from
# the input stream.
##############################################################################
filter Exclude-BuiltInReferences {
if ($Force) { return $_ }
$Token = $_
$TokenName = Normalize-Identifier $Token.Content
# Is it built in?
# If so, skip it.
if ($Token.Type -eq 'Command') {
if ($BuiltInAliases[$TokenName]) { return }
if ($BuiltInCmdlets[$TokenName]) { return }
if ($BuiltInFunctions[$TokenName]) { return }
}
elseif ($Token.Type -eq 'Variable') {
if ($BuiltInVariables[$TokenName]) { return }
}
$Token
}
##############################################################################
#.SYNOPSIS
# Removes references to locally defined functions from the input stream.
##############################################################################
filter Exclude-LocalReferences {
if ($Force) { return $_ }
$Token = $_
$TokenName = Normalize-Identifier $Token.Content
# Is it built in?
# If so, skip it.
if ($Token.Type -eq 'Command') {
if ($LocalFunctions[$TokenName]) { return }
}
$Token
}
##############################################################################
#.SYNOPSIS
# Gets a single command with the specified name, taking into account that the
# command name may need to have conflicting wildcard characters escaped and
# avoiding the error when a command does not exist.
##############################################################################
function Get-SafeCommand([String]$Name) {
$SafeName = [System.Management.Automation.WildcardPattern]::Escape($Name)
$CommandInfo = Get-Command -Name $SafeName -ErrorAction SilentlyContinue | Select -First 1
if (-not $CommandInfo) { $CommandInfo = $ImportedAliases[$Name] }
if (-not $CommandInfo) { $CommandInfo = $ImportedFunctions[$Name] }
if (-not $CommandInfo) { $CommandInfo = $ImportedCmdlets[$Name] }
$CommandInfo
}
##############################################################################
#.SYNOPSIS
# Gets a single variable with the specified name, taking into account that the
# variable name may need to have conflicting wildcard characters escaped and
# avoiding the error when a variable does not exist.
##############################################################################
function Get-SafeVariable([String]$Name) {
$SafeName = [System.Management.Automation.WildcardPattern]::Escape($Name)
$VariableInfo = Get-Variable -Name $SafeName -Scope Global -ErrorAction SilentlyContinue | Select -First 1
$VariableInfo
}
##############################################################################
#.SYNOPSIS
# Selects the first instance of each distinct combination of token type and
# content (function/variable name etc) and includes the line number of the
# first occurrence.
#
#.DESCRIPTION
# This is a basically just a ugly way of doing:
# SELECT Type,Content,Min(StartLine) FROM ... GROUP BY Type,Content
# Which PowerShell cannot easily represent as far as I know
##############################################################################
function Select-UniqueTokens {
$Input |
Group-Object Type,Content |
Select-Object @(
@{N='Type'; E={ $_.Group[0].Type } },
@{N='Content'; E={ $_.Group[0].Content } },
@{N='Script'; E={ $_.Group[0].Script } }
@{N='StartLine'; E={ $_.Group[0].StartLine } }
)
}
##############################################################################
#.SYNOPSIS
# Creates a new PSObject for holding information about the external dependency
##############################################################################
function New-Dependency($Token) {
# Create a PSObject to hold the values
# we will output from this function
New-Object PSObject |
Add-Member -PassThru NoteProperty Script ($Token.Script) |
Add-Member -PassThru NoteProperty Module ($null) |
Add-Member -PassThru NoteProperty Type ($Token.Type) |
Add-Member -PassThru NoteProperty Name (Normalize-Identifier $Token.Content) |
Add-Member -PassThru NoteProperty Target ($null) |
Add-Member -PassThru NoteProperty Resolved ($false) |
Add-Member -PassThru NoteProperty File ($null)
}
} # begin
process {
if ($PSCmdlet.ParameterSetName -eq 'Selection') {
if (-not $PSISE) { throw 'The Selection parameter set is not valid outside of the PowerShell ISE.' }
if (-not $PSISE.CurrentOpenedFile) { throw 'There is no file currently opened.' }
if (-not $PSISE.CurrentOpenedFile.IsSaved) { throw 'Please save the currently active document first.' }
if ($PSISE.CurrentOpenedFile.IsUntitled) { throw 'Please save the currently active document first.' }
$ResolvedPaths = @(Resolve-Path -LiteralPath $PSISE.CurrentOpenedFile.FullPath)
}
elseif ($PSCmdlet.ParameterSetName -match '^(Literal)?Path$') {
switch ($PSCmdlet.ParameterSetName) {
Path { $ResolvedPaths = @(Resolve-Path -Path $Path) }
LiteralPath { $ResolvedPaths = @(Resolve-Path -LiteralPath $LiteralPath) }
}
}
# Discover imported modules and function definitions
Discover-BuiltInCommands
Discover-ModuleImports
Discover-LocalFunctions
# Parse the source code
# Gives back command and variable references
$Tokens = @(
Get-PSTokenProxy Command,Variable |
Exclude-BuiltInReferences |
Exclude-LocalReferences |
Select-UniqueTokens
)
foreach ($Token in $Tokens) {
$Dependency = New-Dependency $Token
# HANDLE VARIABLE REFERENCES
if ($Token.Type -eq 'Variable') {
if ($VariableInfo = Get-SafeVariable $Dependency.Name) {
$Dependency.Module = $VariableInfo.ModuleName
$Dependency.Resolved = $true
}
else {
# Skip this dependency
# Could not find this variable in global state
# So we can only assume that the variable is
# localized somewhere and cannot really be
# resolved as an external dependency
# TODO: maybe look for a setter or arg name
# to determine if it's really external or not
continue
}
} # Token Type = Variable
# HANDLE COMMAND REFERENCES
if ($Token.Type -eq 'Command') {
if ($CommandInfo = Get-SafeCommand $Dependency.Name) {
$Dependency.Type= $CommandInfo.CommandType
$Dependency.Resolved = $true
# Is it an alias?
# If so, we want to resolve the target
if ($CommandInfo.CommandType -eq 'Alias') {
# Is the alias resolved?
if ($CommandInfo.ResolvedCommandName) {
# Is alias the same name as the target?
# If so, this is generally a disambiguating alias and can be ignored.
if ($CommandInfo.Name -eq $CommandInfo.ResolvedCommandName) {
if ($BuiltInAliases[$CommandInfo.ResolvedCommandName]) { continue }
if ($BuiltInCmdlets[$CommandInfo.ResolvedCommandName]) { continue }
if ($BuiltInFunctions[$CommandInfo.ResolvedCommandName]) { continue }
}
$Dependency.Target = $CommandInfo.ResolvedCommandName
$CommandInfo = $CommandInfo.ResolvedCommand
}
else {
# The alias points to an unresolved command
$Dependency.Resolved = $false
}
} # Command Type = Alias
# Command Source Info
# Note that in the case of resolved aliases, CommandInfo is now
# pointing to the resolved command and modules/paths/etc will reflect that
if ($CommandInfo.ModuleName) { $Dependency.Module = $CommandInfo.ModuleName }
if ($CommandInfo.ScriptBlock.File) { $Dependency.File = $CommandInfo.ScriptBlock.File }
if ($CommandInfo.Path) { $Dependency.File = $CommandInfo.Path }
if ($CommandInfo.DLL) { $Dependency.File = $CommandInfo.DLL }
} # Is Command Is Resolved
} # Token Type = Command
if ($Unresolved -and $Dependency.Resolved) { continue }
[Void]$Dependencies.Add($Dependency)
} # foreach Token
} # process
end {
# returns all output at the end
$Dependencies | Sort Script,Module,Type,Name
} # end
}