21 November 2016

How do I validate verb validity in Powershell?

 So you're about to write a new Powershell cmdlet or script.  What verbs can you use in the name?  Everyone knows you can just do a quick

Get-Verb

Which would dump out something like this:

Verb                                                        Group
----                                                        -----
Add                                                         Common
Clear                                                       Common
Close                                                       Common
Copy                                                        Common
Enter                                                       Common
Exit                                                        Common
Find                                                        Common
Format                                                      Common
Get                                                         Common
Hide                                                        Common
Join                                                        Common
Lock                                                        Common
Move                                                        Common
New                                                         Common
Open                                                        Common
Optimize                                                    Common
Pop                                                         Common
Push                                                        Common
Redo                                                        Common
Remove                                                      Common
Rename                                                      Common
Reset                                                       Common
Resize                                                      Common
Search                                                      Common
Select                                                      Common
Set                                                         Common
Show                                                        Common
Skip                                                        Common
Split                                                       Common
Step                                                        Common
Switch                                                      Common
Undo                                                        Common
Unlock                                                      Common
Watch                                                       Common
Backup                                                      Data
Checkpoint                                                  Data
Compare                                                     Data
Compress                                                    Data
Convert                                                     Data
ConvertFrom                                                 Data
ConvertTo                                                   Data
Dismount                                                    Data
Edit                                                        Data
Expand                                                      Data
Export                                                      Data
Group                                                       Data
Import                                                      Data
Initialize                                                  Data
Limit                                                       Data
Merge                                                       Data
Mount                                                       Data
Out                                                         Data
Publish                                                     Data
Restore                                                     Data
Save                                                        Data
Sync                                                        Data
Unpublish                                                   Data
Update                                                      Data
Approve                                                     Lifecycle
Assert                                                      Lifecycle
Complete                                                    Lifecycle
Confirm                                                     Lifecycle
Deny                                                        Lifecycle
Disable                                                     Lifecycle
Enable                                                      Lifecycle
Install                                                     Lifecycle
Invoke                                                      Lifecycle
Register                                                    Lifecycle
Request                                                     Lifecycle
Restart                                                     Lifecycle
Resume                                                      Lifecycle
Start                                                       Lifecycle
Stop                                                        Lifecycle
Submit                                                      Lifecycle
Suspend                                                     Lifecycle
Uninstall                                                   Lifecycle
Unregister                                                  Lifecycle
Wait                                                        Lifecycle
Debug                                                       Diagnostic
Measure                                                     Diagnostic
Ping                                                        Diagnostic
Repair                                                      Diagnostic
Resolve                                                     Diagnostic
Test                                                        Diagnostic
Trace                                                       Diagnostic
Connect                                                     Communications
Disconnect                                                  Communications
Read                                                        Communications
Receive                                                     Communications
Send                                                        Communications
Write                                                       Communications
Block                                                       Security
Grant                                                       Security
Protect                                                     Security
Revoke                                                      Security
Unblock                                                     Security
Unprotect                                                   Security
Use                                                         Other


Now you COULD painstakingly go through the mass of verbs... oh right, they're NOT in alphabetical order!!!  Well, that's not entirely true.  It is in alphabetical order by GROUP.  Who gives a damn about Group?!?  Ugh.
OK, so we could also just pipe the output to the sort command thus:

Get-Verb | sort Verb

That will result in properly sorted output which would make it easier to locate (or not) the intended verb in the list thereby determining if the verb is valid for use according to best practice guidance. :-)
But all that is a terribly manual process and as anyone who knows me will tell you, once I have to repeat a tedious process for a second or third time, I look for a way to automate it.
That's exactly what I did here too.  Presenting...

Get-ValidVerb

This is my little function to do the above quickly and easily.  It can easily be compiled into a module that's loaded in your profile so you'll always have access to it.  Here's the code:

function Get-ValidVerb([string] $verbToCheck)
{
 $verbs = Get-Verb | sort Verb;
 $list =  New-Object -TypeName System.Collections.Generic.List[string];
 foreach ($verb in $verbs) 
 { 
  $list.Add([string]$verb.Verb.ToLower());
 }
 return $list.Contains($verbToCheck.ToLower());
}

The script logic is easy.

  1. The script simply takes a single string argument containing the verb to check for validity.
  2. In line 3 we capture the output from our (Get-Verb | sort Verb) command to a variable.
  3. In line 4 we create a .NET List object that contains strings.
  4. In line 5 we iterate through all items in the dumped list.
  5. In line 7 we use the .Add() method of the List class to add only the Verb of the item (remember, it has a Group column too) to the list.  I added another piece of logic here in that I convert the verb to lowercase with .ToLower() after casting it as a [string] type.  This is done in order to allow the next bit of logic...
  6. Finally in line 9, we leverage the .Contains() method of the List class to check if the verb we're checking, is contained in the list.  You'll notice that we cast this verb to check to lower case as well.  By casting both the list and the verb to check to lower case, we get a case insensitive comparison.  This prevents the problem where Get-ValidVerb "get" will return False because of case different in the G.

Enjoy
C







Microsoft Authentication Library (MSAL) Overview

The Microsoft Authentication Library (MSAL) is a powerful library designed to simplify the authentication process for applications that conn...