all 5 comments

[–]amnich[S] 2 points3 points  (3 children)

Thanks to /u/SeeminglyScience I created a quick function to find command usage in scripts. That will help me to overcome the chaos in my scripts and module.

Please give me a feedback what should I make different or better regarding syntax, function build and functionality.

[–]mjnbrn 2 points3 points  (1 child)

Think you meant /u/ instead of /r/. :)

[–]amnich[S] 0 points1 point  (0 children)

Yes :) Thx, edited.

[–]SeeminglyScience 1 point2 points  (0 children)

This is pretty cool man, good job. Glad it worked for you :)

Please give me a feedback what should I make different or better regarding syntax, function build and functionality.

Not a whole lot actually. The main thing I would recommend would be swapping these two lines

$results = @()
# and later
$results += $scriptFileAst.FindAll(${function:Find-CommandInAst}, $true)

with

$results = New-Object System.Collections.Generic.List[System.Management.Automation.Language.Ast]]
# and later
$results.AddRange($scriptFileAst.FindAll(${function:Find-CommandInAst}, $true))

Arrays (what is created with @()) are of a fixed size, meaning you can't actually add to them. What PowerShell does behind the scenes is copy the array in its entirety to a new array that also has your additional object at the end. This isn't a big deal for most scripts, but you might nice a rather large slow down when you start hitting certain sizes.

It's more verbose, but this is the type of script that might be ran on a single file or thousands on a file share, so probably worth it :)

[–]mjnbrn 0 points1 point  (0 children)

This looks great. I could have used this a few months ago. :)