Thursday, November 27, 2008

I have been helping Alan Renouf, co-host of the Get-Scripting podcast, with a PowerShell script and he has been nice enough to mention me and my blog on during their latest episode.

If you haven't already checked out the Get-Scripting podcast you can listen and subscribe to it at http://get-scripting.blogspot.com.

Thursday, November 27, 2008 1:43:35 PM (E. Australia Standard Time, UTC+10:00)
 Thursday, May 15, 2008

I came across this problem the other day, where I wanted to determine if a Virtual Machine was running on Hyper-V or Virtual Server 2005 host system.

WMI exposes the Win32_ComputerSystem class which contains information about the Manufacturer and Model of a particular system (this is very hand if you want to check type of system before installing an application, e.g. Virtual Machine Additions). The unfortunate news is that Win32_ComputerSystem returns the following on both Hyper-V and Virtual Server hosts:

So how to determine what it is I am looking at? Well there is another WMI class, Win32_BIOS, which can help. Here you can see a difference in the Version between products.

Hyper-V

Virtual Server 2005 SP1

So far this seems to work for me; I am interested if anyone has alternate suggestions on how to solve my little problem, especially if there are cases where this will break (for example limiting the CPU functionality to run NT 4.0?)

Thursday, May 15, 2008 3:40:55 PM (E. Australia Standard Time, UTC+10:00)
 Friday, February 01, 2008

Jeffrey Snover has posted a fantastic PowerShell Scripting presentation he did late last year on the PowerShell Team Blog.

The session is aimed at people who have had experience with PowerShell and is pitched as a deep dive - includes lots of examples and chewy detail. Topics covered include:

Forms and styles of scripting
Scriptblocks
Switch
V2 Script Cmdlets
V2 PSJobs

This is presentation definatly makes installing the Silverlight plugin worth while.

Thanks Jeffrey :)

Friday, February 01, 2008 5:49:25 PM (E. Australia Standard Time, UTC+10:00)
 Sunday, October 14, 2007

I hate drawing Visio diagrams. I’m no Picasso and, for me, drawing a network diagram is one of these necessary evils that I avoid doing as long as humanly possible.

Not anymore, the good people at Microsoft have released the Microsoft Active Directory Topology Diagrammer which dynamically creates Visio diagrams for your Windows infrastructure. The topology tool reads your AD configuration, including domains, sites, servers, administrative groups, routing groups and connectors (for those with Exchange) and outputs a detailed library of diagram for you to manipulate as required.

Impressed with this release I spent some time google’ing other Visio automation tools, chasing that illusive magic bullet to cure all my Visio pains, I found an abundance of information and samples on MSDN. One such sample, How to use OLE automation in Visio, is a VBA macro that did most of the grunt work, loading and created a new Visio diagram, drawing a shape with a label, saving the changes and closing Visio. Apart from being a macro it performed the key functionality that would be required for any tool. To obtain the flexibility I was looking for I have transposed the macro code to a Powershell script which we can expand on later.

# ' Create an instance of Visio and create a document based on the
# ' Basic Diagram template. It doesn't matter if an instance of
# ' Visio is already running, CreateObject will run a new one.
# Set AppVisio = CreateObject("visio.application")
$AppVisio = New-Object -ComObject Visio.Application

# Set docsObj = AppVisio.Documents
$docsObj = $AppVisio.Documents

# ' Create a document based on the Basic Diagram template that
# ' automatically opens the Basic Shapes stencil.
# Set DocObj = docsObj.Add("Basic Diagram.vst")
$DocObj = $docsObj.Add("Basic Diagram.vst")

# Set pagsObj = AppVisio.ActiveDocument.Pages
$pagsObj = $AppVisio.ActiveDocument.Pages

# ' A new document always has at least one page, whose index in the
# ' Pages collection is 1.
# Set pagObj = pagsObj.Item(1)
$pagObj = $pagsObj.Item(1)

# Set stnObj = AppVisio.Documents("Basic Shapes.vss")
$stnObj = $AppVisio.Documents.Add("Basic Shapes.vss")

# Set mastObj = stnObj.Masters("Rectangle")
$mastObj = $stnObj.Masters.Item("Rectangle")

# ' Drop the rectangle in the approximate middle of the page.
# ' Coordinates passed with the Drop method are always inches.
# Set shpObj = pagObj.Drop(mastObj, 4.25, 5.5)
$shpObj = $pagObj.Drop($mastObj, 4.25, 5.5)

# ' Set the text of the rectangle.
# shpObj.Text = "This is some text."
$shpObj.Text = "This is some text."

# ' Save the drawing and quit Visio. The message pauses the program
# ' so you can see the Visio drawing before the instance closes.
# DocObj.SaveAs "MyDrawing.vsd"
$DocObj.SaveAs("C:\MyDrawing.vsd")
# MsgBox "Drawing finished!", , "AutoVisio (OLE) Example"

# ' Quit Visio.
# AppVisio.Quit

$AppVisio.Quit()

# ' Clear the variable from memory.
# Set AppVisio = Nothing

In an effort to make it that little bit easier to understand I have commented all the sample code and incorporated the Powershell equivalents. Remember that Powershell, unlike VB 6, doesn’t support default properties. So when we are calling a method (e.g. $stnObj.Masters) we have to define the property we are setting (e.g. Item).

So great I have a rectangle with some text in the middle of a blank Visio, not very useful, but after a few changes we have something that will dynamically generate stencils for all computers in a domain.

# Zero initial drop coordinates
$x = 0
$y = 1.20

# Create an instance of Visio and create a document based on the Basic Diagram template.
$AppVisio = New-Object -ComObject Visio.Application
$docsObj = $AppVisio.Documents
$DocObj = $docsObj.Add("Basic Diagram.vst")

# Set the active page of the document to page 1
$pagsObj = $AppVisio.ActiveDocument.Pages
$pagObj = $pagsObj.Item(1)

# Load a set of stencils and select one to drop
$stnObj = $AppVisio.Documents.Add("SERVER_M.vss")
$mastObj = $stnObj.Masters.Item("Server")

# Retrieve a list of computer accounts from Active Directory
#
http://www.microsoft.com/technet/scriptcenter/resources/qanda/nov06/hey1109.mspx

$strCategory = "computer"

$objDomain = New-Object System.DirectoryServices.DirectoryEntry

$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SearchRoot = $objDomain
$objSearcher.Filter = ("(objectCategory=$strCategory)")

$colProplist = "name"
foreach ($i in $colPropList){$objSearcher.PropertiesToLoad.Add($i)}

$colResults = $objSearcher.FindAll()

foreach ($objResult in $colResults)
{
  $objComputer = $objResult.Properties; $objComputer.name
  
  # Set coordinates for drop
  $x = $x + 1.30
  
  # Loop to the next line
  if($x -ge 8)
  {
   $x = 1.30
   $y = $y + 1.20
  }
  
  # Drop the selected stencil on the active page, with the coordinates x, y
  $shpObj = $pagObj.Drop($mastObj, $x, $y)
  
  # Enter text for the object
  $shpObj.Text = $objComputer.name
 }

# Save the diagram
$DocObj.SaveAs("C:\CMDBREAK_MyDrawing.vsd")

# Quit Visio
$AppVisio.Quit()

Lost?

Highlighted above in each of the colours is the following:
Yellow – What we started with, only modified to select and drop the Server stencil.
Red – Controls where we drop each stencil, so as they are not on top of each other.
Blue - Enumerates all computer accounts in the local domain. Detailed explanation is available at Hey, Scripting Guy! How Can I Use Windows PowerShell to Get a List of All My Computers?

Now we have a script that will retrieve all the computer accounts in an AD domain and draw shapes for each, one of the monotonous tasks required for any network diagram.

To recap we have converted a VBA macro to a Powershell script and after a few additions we can generate a set of Server stencils labelled with the hostnames of all computers connected to an AD domain. This is just a start, with some more modifications it would be easy to select a different stencil based on the services running or have the script modify existing shapes in a drawing to create a dynamic network diagram.

Happy Visio Automagic.

Sunday, October 14, 2007 9:59:08 PM (E. Australia Standard Time, UTC+10:00)
Navigation
Search
On this page....
Archives
<January 2009>
SunMonTueWedThuFriSat
28293031123
45678910
11121314151617
18192021222324
25262728293031
1234567
Categories
Blogroll
Contact me
Send mail to the author(s) E-mail
Powered by

newtelligence dasBlog 2.0.7226.0.