Sunday, February 14, 2016

PowerShell v3 Using Visio to Display Cmdlets by Noun

,
To get a better grip on what all exists in the new v3 command space, I wanted to know what I could do with the Get-Command cmdlet in order to get things grouped by Noun. If this is unfamiliar to you, all cmdlets have two parts in their name as a part of the cmdlet design philosophy:

  • Verb
  • Noun
In the case of Get-Item, the verb is Get and the noun is Item. And, Send-MailMessage has a verb of Send and noun of MailMessage. So, whatever preceeds the dash is the verb and whatever follows is the noun. Fine. Obvious. Duh. I want to do something with this though. And, Visio is coming to mind. So, I need to get my data organized, then, I want a new chart. Thats a little more ambitious. 

Ok. Lets see about step 1: organizing the data. To get around the organization I came up with a four step command that:
  1. Gets the cmdlets
  2. Selects their full name and noun
  3. Groups the list based on nouns
  4. Selects (and expands) the groups once sorted
The command looks like this:
Get-Command-CommandType cmdlet|
selectName,@{e={($_.name -split -)[1]};N=Noun} |
groupNoun |
select-ExpandProperty Group
Easy enough. When I run this, just to give a sample, the first 10 lines looks like this:
Add-BitsFile           BitsFile 
Add-Computer           Computer 
Checkpoint-Computer    Computer 
Remove-Computer        Computer 
Rename-Computer        Computer 
Restart-Computer       Computer 
Restore-Computer       Computer 
Stop-Computer          Computer 
Add-Content            Content  
Clear-Content          Content  
Get-Content            Content  
Set-Content            Content 
Cool. Organized, simple, straightforward. Now, for the fun part...i.e., the new puzzle. How can I get these guys into a pretty format in Visio? Or, maybe Word. Ill start with Visio. Using Eds post
Hey, Scripting Guy! Is It Possible to Automate Microsoft Visio?
I was able to come up with something basic, but, it still did not have any kind of decent formatting. It simply put grouped output into rectangles in a Visio document. As I get further into the Visio object model I will try to find a clean, prettier way to put all this together. At the moment, all it does is dump them into a document. Ill actually need to move things around. For now, thats a good bit of time saved. Getting a pretty, well-formed script that really organizes this is probably a round 2 of this effort.
# Instantiate new Visio document
$application = New-Object -ComObject Visio.Application

# Add document to application
$documents = $application.Documents

# Open basic diagram template
$document = $documents.Add(Basic Diagram.vst)

# Select active document page
$pages = $application.ActiveDocument.Pages

# Select first page
$page = $pages.Item(1)

# Select basic shapes stencil
$stencil = $application.Documents.Add("Basic Shapes.vss")

# Get list of cmdlets and group by noun
Get-Command -CommandTypecmdlet |
Sort Noun|
Group noun|
ForEach-Object {
    # Build label
    $output+= "$($_.Name)`n"
    ForEach-Object{
        # Check to see if group has more than 1 cmdlet
        if($_.Group.Name.Count -gt 1)
        {
            ($_.Group.Name -split `n) |
            ForEach-Object{
                # Add cmdlet names to label with new line per cmdlet
                $output+= + $_ + "`n"
            }
        }
        else
        {
            # Add cmdlet names to label
            $output+= + $_.Group.Name
        }
    }

    # Create new square
    $item = $stencil.Masters.Item("Rectangle")

    # Drop square on page
    $shape= $page.Drop($item, 1.0, 10.6)

    # Set text to current group name
    $shape.Text = $output

0 comments to “PowerShell v3 Using Visio to Display Cmdlets by Noun”

Post a Comment

 

Computer Info Copyright © 2016 -- Powered by Blogger