November 22nd, 2008

Verbs vs Nouns by Snapin

PowerShell Team
PowerShell Team

A while ago the System Center Virtual Machine Manager team showed me a chart of their cmdlets.  It was an Excel spreadsheet which had the VERBs as rows and the NOUNs as columns.  I thought that this was a wonderful way to look at the cmdlets.  I woke up this morning and decided to write a script which would do something similar.  I’ve attached that script which accomplishes that task.  In the process, I decided that there is a good general purpose function here but I haven’t figured out what it’s name is so right now I’m just calling it ConvertTo-XXX (I’m open to suggestions).  NOTE – the cmdlet requires the latest version of PowerShell (the stuff we released at the PDC) so if you don’t have those bits, you’ll have to wait a few more weeks until CTP3 is available to try it out.  Also note that this is using yesterday’s build which may have more cmdlets than the PRE-Beta we release.

I’m experimenting with a few coding practices so some of these may stay and some may go.  I’d love any feedback you have on that topic.

PS> .\ConvertTo-XXX -Test
Microsoft.PowerShell.Diagnostics

Name   Counter        WinEvent   
—-   ——-        ——–   
Export Export-Counter *          
Get    Get-Counter    Get-WinEvent
Import Import-Counter *          


Microsoft.WSMan.Management

Name       WSMan            WSManAction        WSManCredSSP         WSManInstance        WSManQuickConfig     WSManSessionOption   
—-       —–            ———–        ————         ————-        —————-     ——————   
Connect    Connect-WSMan    *                  *                    *                    *                    *                    
Disable    *                *                  Disable-WSManCredSSP *                    *                    *                    
Disconnect Disconnect-WSMan *                  *                    *                    *                    *                    
Enable     *                *                  Enable-WSManCredSSP  *                    *                    *                    
Get        *                *                  Get-WSManCredSSP     Get-WSManInstance    *                    *                    
Invoke     *                Invoke-WSManAction *                    *                    *                    *                    
New        *                *                  *                    New-WSManInstance    *                    New-WSManSessionOption
Remove     *                *                  *                    Remove-WSManInstance *                    *                    
Set        *                *                  *                    Set-WSManInstance    Set-WSManQuickConfig *                    
Test       Test-WSMan       *                  *                    *                    *                    *                    


Microsoft.PowerShell.Core

Name       Command        Console        Help     History        Job         Module        ModuleManifest      ModuleMember        Object       
—-       ——-        ——-        —-     ——-        —         ——        ————–      ————        ——       
Add        *              *              *        Add-History    *           *             *                   *                   *            
Clear      *              *              *        Clear-History  *           *             *                   *                   *            
Disable    *              *              *        *              *           *             *                   *                   *            
Enable     *              *              *        *              *           *             *                   *                   *            
Enter      *              *              *        *              *           *             *                   *                   *            
Exit       *              *              *        *              *           *             *                   *                   *            
Export     *              Export-Console *        *              *           *             *                   Export-ModuleMember *            
ForEach    *              *              *        *              *           *             *                   *                   ForEach-Object
Get        Get-Command    *              Get-Help Get-History    Get-Job     Get-Module    *                   *                   *            
Import     *              *              *        *              *           Import-Module *                   *                   *            
Invoke     Invoke-Command *              *        Invoke-History *           *             *                   *                   *            
New        *              *              *        *              *           New-Module    New-ModuleManifest  *                   *            
Receive    *              *              *        *              Receive-Job *             *                   *                   *            
Register   *              *              *        *              *           *             *                   *                   *            
Remove     *              *              *        *              Remove-Job  Remove-Module *                   *                   *            
Set        *              *              *        *              *           *             *                   *                   *            
Start      *              *              *        *              Start-Job   *             *                   *                   *            
Stop       *              *              *        *              Stop-Job    *             *                   *                   *            
Test       *              *              *        *              *           *             Test-ModuleManifest *                   *            
Unregister *              *              *        *              *           *             *                   *                   *            
Wait       *              *              *        *              Wait-Job    *             *                   *                   *            
Where      *              *              *        *              *           *             *                   *                   Where-Object 


Microsoft.PowerShell.Utility

Name        Alias        Clixml        Command         Csv             Culture     Custom        Date     Debug       Default   
—-        —–        ——        ——-         —             ——-     ——        —-     —–       ——-   
Add         *            *             *               *               *           *             *        *           *         
Clear       *            *             *               *               *           *             *        *           *         
Compare     *            *             *               *               *           *             *        *           *         
ConvertFrom *            *             *               ConvertFrom-Csv *           *             *        *           *         
ConvertTo   *            *             *               ConvertTo-Csv   *           *             *        *           *         
Disable     *            *             *               *               *           *             *        *           *         
Enable      *            *             *               *               *           *             *        *           *         
Export      Export-Alias Export-Clixml *               Export-Csv      *           *             *        *           *         
Format      *            *             *               *               *           Format-Custom *        *           *         
Get         Get-Alias    *             *               *               Get-Culture *             Get-Date *           *         
Group       *            *             *               *               *           *             *        *           *         
Import      Import-Alias Import-Clixml *               Import-Csv      *           *             *        *           *         
Invoke      *            *             *               *               *           *             *        *           *         
Measure     *            *             Measure-Command *               *           *             *        *           *         
New         New-Alias    *             *               *               *           *             *        *           *         
Out         *            *             *               *               *           *             *        *           Out-Default
Read        *            *             *               *               *           *             *        *           *         
Register    *            *             *               *               *           *             *        *           *         
Remove      *            *             *               *               *           *             *        *           *         
Select      *            *             *               *               *           *             *        *           *         
Send        *            *             *               *               *           *             *        *           *         
Set         Set-Alias    *             *               *               *           *             Set-Date *           *         
Sort        *            *             *               *               *           *             *        *           *         
Start       *            *             *               *               *           *             *        *           *         
Tee         *            *             *               *               *           *             *        *           *         
Trace       *            *             Trace-Command   *               *           *             *        *           *         
Unregister  *            *             *               *               *           *             *        *           *         
Update      *            *             *               *               *           *             *        *           *         
Wait        *            *             *               *               *           *             *        *           *         
Write       *            *             *               *               *           *             *        Write-Debug *         


Microsoft.PowerShell.Host

Name  Transcript     
—-  ———-     
Start Start-Transcript
Stop  Stop-Transcript


Microsoft.PowerShell.Management

WARNING: column “EventLog” does not fit into the display and was removed.

Name       ChildItem     Computer            ComputerMachinePassword       ComputerRestore         ComputerRestorePoint     ComputerSecureChannel      Connection      Content
—-       ———     ——–            ———————–       —————         ——————–     ———————      ———-      ——-
Add        *             Add-Computer        *                             *                       *                        *                          *               Add-…
Checkpoint *             Checkpoint-Computer *                             *                       *                        *                          *               *     
Clear      *             *                   *                             *                       *                        *                          *               Clea…
Complete   *             *                   *                             *                       *                        *                          *               *     
Convert    *             *                   *                             *                       *                        *                          *               *     
Copy       *             *                   *                             *                       *                        *                          *               *     
Debug      *             *                   *                             *                       *                        *                          *               *     
Disable    *             *                   *                             Disable-ComputerRestore *                        *                          *               *     
Enable     *             *                   *                             Enable-ComputerRestore  *                        *                          *               *     
Get        Get-ChildItem *                   *                             *                       Get-ComputerRestorePoint *                          *               Get-…
Invoke     *             *                   *                             *                       *                        *                          *               *     
Join       *             *                   *                             *                       *                        *                          *               *     
Limit      *             *                   *                             *                       *                        *                          *               *     
Move       *             *                   *                             *                       *                        *                          *               *     
New        *             *                   *                             *                       *                        *                          *               *     
Pop        *             *                   *                             *                       *                        *                          *               *     
Push       *             *                   *                             *                       *                        *                          *               *     
Register   *             *                   *                             *                       *                        *                          *               *     
Remove     *             Remove-Computer     *                             *                       *                        *                          *               *     
Rename     *             Rename-Computer     *                             *                       *                        *                          *               *     
Reset      *             *                   Reset-ComputerMachinePassword *                       *                        *                          *               *     
Resolve    *             *                   *                             *                       *                        *                          *               *     
Restart    *             Restart-Computer    *                             *                       *                        *                          *               *     
Restore    *             Restore-Computer    *                             *                       *                        *                          *               *     
Resume     *             *                   *                             *                       *                        *                          *               *     
Set        *             *                   *                             *                       *                        *                          *               Set-…
Show       *             *                   *                             *                       *                        *                          *               *     
Split      *             *                   *                             *                       *                        *                          *               *     
Start      *             *                   *                             *                       *                        *                          *               *     
Stop       *             Stop-Computer       *                             *                       *                        *                          *               *     
Suspend    *             *                   *                             *                       *                        *                          *               *     
Test       *             *                   *                             *                       *                        Test-ComputerSecureChannel Test-Connection *     
Undo       *             *                   *                             *                       *                        *                          *               *     
Use        *             *                   *                             *                       *                        *                          *               *     
Wait       *             *                   *                             *                       *                        *                          *               *     
Write      *             *                   *                             *                       *                        *                          *               *     


Microsoft.PowerShell.Security

Name        Acl     AuthenticodeSignature     Credential     ExecutionPolicy     PfxCertificate     SecureString           
—-        —     ———————     ———-     —————     ————–     ————           
ConvertFrom *       *                         *              *                   *                  ConvertFrom-SecureString
ConvertTo   *       *                         *              *                   *                  ConvertTo-SecureString 
Get         Get-Acl Get-AuthenticodeSignature Get-Credential Get-ExecutionPolicy Get-PfxCertificate *                      
Set         Set-Acl Set-AuthenticodeSignature *              Set-ExecutionPolicy *                  *                      

Here is the code for CONVERTTO-XXX.PS1

#requires -version 2.0
param(
[Parameter(ParameterSetName=”xxx”, Mandatory=$true, Position=0)]
$InputObject,

[Parameter(ParameterSetName=”xxx”, Mandatory=$true)]
$yProperty,

[Parameter(ParameterSetName=”xxx”, Mandatory=$true)]
$xProperty,

[Parameter(ParameterSetName=”xxx”)]
[ScriptBlock]
$CellValue = {“TRUE”},

[Parameter(ParameterSetName=”xxx”)]
$NullValue = “-“,

[Parameter(ParameterSetName=”TEST”, Mandatory=$true)]
[Switch]
$Test
)

    function Assert
    {
    param(
        [Parameter(Mandatory=$true, Position=0)]
        [ScriptBlock]$script,
        [Parameter(Position=1)]
        $Message=$null
    )
        if (!(&$script))
        {
            $ThrowAway,$stack = Get-PscallStack
            $msg = @”
    ASSERT( $script ) FAILED at
    $($stack | out-string)

    $Message
    Continue script execution?
“@
            if ( !($pscmdlet.ShouldContinue($msg, “ASSERT FAILED”)))
            {
                exit
            }
        }
    }

    function Add-Property
    {
    param(
        [Parameter(Mandatory=$true, Position=0)]
        [ValidateNotNullOrEmpty()]
        $obj,
        [Parameter(Mandatory=$true, Position=1)]
        [ValidateNotNullOrEmpty()]
        [String]
        $prop,
        [Parameter(Mandatory=$true, Position=2)]
        $value)
        Add-Member -InputObject $obj -MemberType NoteProperty -Name $prop -Value $value -Force
    }

if ($PSCmdlet.ParameterSetName -eq “TEST”)
{
    foreach ($snapin in Get-PSSnapin) {
        Write-output $snapin.Name
        $InputObject = get-command -PSSnapin $snapin -type Cmdlet |sort noun
        
&$MyInvocation.MyCommand.Path  $InputObject -nullValue “*” -YProperty Verb  -xProperty Noun -CellValue {$args[0].name}|ft -auto |out-string –stream 
    }
    exit   
}

foreach ($group in $InputObject | Group-Object -Property $yProperty |Sort Name)
{
   $outObj = New-Object psobject
   Add-Property $outObj Name $group.Name
   foreach ($inObj in $InputObject)
   {
        $Xname = $inObj.$xProperty
        Assert {$Xname } “Property [$XProperty] not found on input object”
        Assert {$Xname -ne “NAME” } “Cannot have ‘Name’ as a property”
        if ($InObj.$yProperty -eq $group.Name)
        {           
            Add-Property $outObj $Xname (&$CellValue $inObj)
        }elseif (!$outObj.$XName)
        {
            Add-Property $outObj $Xname  $nullValue
        }
   }
   Write-Output $outObj
}

[11/23 update – Thanks to Richard Siddaway for pointing out that the original version of this script required the script to be in a PATH directory.  I modified the script to fix that.  If you are a script, this is how you call yourself so you work whether or not your in the same directory or in a PATH directory

&$MyInvocation.MyCommand.Path 

]

Enjoy!

Jeffrey Snover [MSFT]
Windows Management Partner Architect
Visit the Windows PowerShell Team blog at:    http://blogs.msdn.com/PowerShell
Visit the Windows PowerShell ScriptCenter at:  http://www.microsoft.com/technet/scriptcenter/hubs/msh.mspx

convertto-xxx.ps1

Category
PowerShell

Author

PowerShell Team
PowerShell Team

PowerShell is a task-based command-line shell and scripting language built on .NET. PowerShell helps system administrators and power-users rapidly automate tasks that manage operating systems (Linux, macOS, and Windows) and processes.

0 comments

Discussion are closed.