Showing relationships with PSWriteHTML

Showing relationships with PSWriteHTML

Have you seen the powershell module PSWriteHTML? It’s a neat powershell module that allows you to create all kinds of HTML pages, search around and you’ll loads of really complex examples, try here for a starter.


Anyway, here’s a quick one for you, I’ve created a very basic example extracting from a SQL query and creating a relationship map. The proc called in this case is a custom proc running the same code that SSMS does when you view dependencies, but any data with a relationship would work, from this nodes are created for each object and links added between them in just a few lines.

install-module pswritehtml

import-module pswritehtml 

$Results = Invoke-Sqlcmd -ServerInstance localhost -Database MSDB -Query "EXEC GetDepends" 
#get all the objects that need a node
$objects = $results.objectname
$objects+= $results.relative_name
#now make sure there are no duplicates
$uniquename = $objects|select -Unique

#build the page
New-HTML -TitleText 'My diagram' -Online -FilePath c:\temp\Example-depends.html {
#add the diagram
    New-HTMLDiagram  {
        foreach ($u in $uniquename){
            New-DiagramNode -Label $u
        }
        Foreach($r in ($Results|WHERE {$_.object_id -ine $_.relative_id})){
            New-DiagramLink -From $r.object_name -To $r.relative_name
        }
    }
} -ShowHTML

You then end up with a wild object relationship map like this.

Leave a comment