Sunday, February 04, 2007

PowerShell: Designing a CmdLet (Part IIb - the code)

Download Test Code

If you need assistance in compiling and testing the CmdLet code see Don Jones' Make-A-CmdLet blog.

I have included the Get-Computer.vb source file and the generated DLL "PSSExtensionsVB.DLL" to the zip file.  You can load the DLL by following these instructions:

$dllpath=<path to saved dll> C:\Windows\Microsoft.NET\Framework\v2.0.50727\installutil $dllpath add-pssnapin PSSExtensionsVB

Now try the following commands:


Get-Computer localhost

Get-computer comp1,comp2,comp3

Get-Computer -file c:\computers.txt

dir c:\computers.txt | Get_Computer.txt

$hosts = "host1",host2", "host3"

$hosts | Get-Computer

Any input object or collection where the object "name" property is a computer can feed this CmdLet.

Try the following:

PS>$mycomputer = Get-Computer localhost                                                                                                     
VERBOSE: DefaultSet                                                                                                                         
Services                                  Name         IsPingable 
--------                                  ----         ---------- 
{Alerter, ALG, AppMgmt, aspnet_state...}  localhost    True 
Key                   Value                                                                 
---                   -----                                                                 
Alerter               \\OMEGA\root\cimv2:Win32_Service.Name="Alerter" 
ALG                   \\OMEGA\root\cimv2:Win32_Service.Name="ALG"
AppMgmt               \\OMEGA\root\cimv2:Win32_Service.Name="AppMgmt" 
aspnet_state          \\OMEGA\root\cimv2:Win32_Service.Name="aspnet_state"
AudioSrv              \\OMEGA\root\cimv2:Win32_Service.Name="AudioSrv" 
BITS                  \\OMEGA\root\cimv2:Win32_Servic="BITS"
Browser               \\OMEGA\root\cimv2:Win32_Service.Name="Browser"


Use GM to explore the properties of $mycomputer.

Look at the code and see how little code is needed to create this much functionality.  NET coupled with PowerShell is very powerful and flexible.

In the next installment I will explore more of the options available for creating powerful CmdLet's that help to simplify administration.  Along the way I will add some more interesting abilities to the "Computer" object for fun and to help test the growing CmdLet design.  Remember, we are only at the design stage.  After we have tested all of the elements and chosen the final configuration we still have to complete the design.  After the design is complete we will begin the final stage of code generation and testing.

No comments:

Post a Comment