Good day, colleagues. A hundred years ago, somewhere I described a quick and dirty way to see the call graph inside your PowerShell script. Now, it’s time, so to speak, to pull the owl on the globe and look at the dependency graph inside the module.
First of all, you need to install the PSQuickGraph module from the Powershell Gallery. And after that run the script, the base of which can be taken here . Honestly, I did not quite understand how to insert the code from the gist into the post on the habr. If someone tells you in comments, I will be very grateful.
So let's continue. First of all, in two words about the module itself. It is built on the QuickGraph library. In essence, this is a PowerShell wrapper over a library that is designed for quick and rough tests with graphs. One of these tests can be a piece that parses your PowerShell code, generates a link graph from it, and presents it visually.
In addition, the script itself uses the built-in PowerShell access mechanism to its AST, which, in fact, greatly simplifies our analysis of the code. In the end, by combining one and the other, we get a graph.
As a result, the script itself consists of two parts. A piece of "parser" working with AST, and the part that connects AST and the graph. This part is described in the following example.
# ( PSQuickGraph) $g = New-Graph -Type BidirectionalGraph # PS1 , AST $f = dir "C:\Repo\Work\Tools\*.ps1" -Recurse | Find-Function $f.name | % { Add-Vertex -Vertex $_ -Graph $g} # , , , $f | % { $fName = $_.name $tmpFile = New-TemporaryFile $_.line >> ($tmpFile.fullname) $expr = Find-Expression -Fullname $tmpFile.fullname $expr.name | ? {$_} | ? {$_ -notin "%","where","?","select","out-null","sort","ft","fl","Write-Verbose" } | % { Add-Edge -From $fName -to $_ -Graph $g} } # Show-GraphLayout -Graph $g # DOT Export-Graph -Format Graphviz -Graph $g -Path C:\Temp\g.gv
Well, something like this:
In addition, the last command exports the graph to the DOT format, which allows you to torment it even in the graphviz application
Source: https://habr.com/ru/post/425225/
All Articles