Why mess with Visio from PowerShell?
I’ve got a couple of posts with some really basic code to access things in PowerShell and it occurred to me…I probably haven’t made it clear why you might want to do this (other than that you can).
So, instead of moving on to connections (which will be next, followed by containers), I thought I’d take a break and show you a really quick and practical script.
The Scenario
Imagine that you just got handed a network diagram of a bunch (dozens?) of servers that you manage. The only problem is, the diagram only includes the server name. Your boss needs it to include the IP Addresses for each item. You have a choice. You can either manually look up each name with Resolve-DNSName and copy-and-paste the IP Address into the diagram, or you can use PowerShell to solve the problem.
Here’s the script. The funny part (to me, at least) is that the complexity is trying to get the IPV4 address out of Resolve-DNSName, the Visio part is simple.
$Visio = New-Object -ComObject Visio.Application $Doc=$Visio.Documents.Open('c:\temp\ServerDiagram.vsdx') $Page=$Visio.ActivePage foreach($shape in $page.Shapes){ if(!$shape.Text.Contains("`n")){ $address=Resolve-DnsName $shape.text $address=$address| where-object {$_.IP4Address } | select-object -First 1 $newLabel="{0}`n{1}" -f $shape.text,$address.IP4Address $Shape.Text=$newLabel } }
All it does is look for shapes on the page that don’t contain newlines, and update those shapes with the IP Address it wrestles out of Resolve-DNSName.
I think you can see that something like this is a handy tool to have in your back pocket. I know that when I draw server diagrams I don’t label the IP Addresses manually anymore. This script (or one just like it) has saved me hours.
And that, my friend, is what PowerShell is all about.
–Mike