Wednesday, February 07, 2007

Designing A CmdLet (Part IId - Test Code)

I am posting a new zip of the test code here with some additions to the "Computer" object. ( Link to full project source at end of blog article)   [NOTE: this code is not "production" quality yet.  I have not guarded against every possible exception and have not tested to guaratee the code won't "hang".  The code DOESN NOT change anything in the system or in PowerShell and has not caused any issues after many executions.  I amproviding it for you to experiment with and, hopefully, provide some feedback.

I have added OSInfo as a collection of OS information about all things informational to the operating system. ( How's that for a sentence?  Never give a programmer a pen and paper!)

With this version of the DLL you can:

PS>$computer=get-computer localhost                       
Name        IsPingable OSInfo            Services                           
----        ---------- ------            --------                           
localhost   True       \\OMEGA\root\c... {Alerter, ALG, AppMgmt, aspnet_... 

SystemDirectory : C:\WINDOWS\system32
Organization    : Designed Systems & Services
BuildNumber     : 2600              
RegisteredUser  : James Vierra                  
SerialNumber    : 55274-006-6247011-22174                              
Version         : 5.1.2600                                    


We can expand on OSInfo like this:  ( please excuse the line wraps.)


   TypeName: System.Management.ManagementObject#root\cimv2\Win32_OperatingSystem

Name                                      MemberType   Definition
----                                      ----------   ----------
Reboot                                    Method       System.Management.ManagementBaseObject Reboot()
SetDateTime                               Method       System.Management.ManagementBaseObject SetDateTime(System.String LocalDateTime)
Shutdown                                  Method       System.Management.ManagementBaseObject Shutdown()
Win32Shutdown                             Method       System.Management.ManagementBaseObject Win32Shutdown(System.Int32 Flags, System.I...
BootDevice                                Property     System.String BootDevice {get;set;}
BuildNumber                               Property     System.String BuildNumber {get;set;}
BuildType                                 Property     System.String BuildType {get;set;}
Caption                                   Property     System.String Caption {get;set;}
CodeSet                                   Property     System.String CodeSet {get;set;}
CountryCode                               Property     System.String CountryCode {get;set;}
CreationClassName                         Property     System.String CreationClassName {get;set;}
CSCreationClassName                       Property     System.String CSCreationClassName {get;set;}
CSDVersion                                Property     System.String CSDVersion {get;set;}
CSName                                    Property     System.String CSName {get;set;}
CurrentTimeZone                           Property     System.Int16 CurrentTimeZone {get;set;}
DataExecutionPrevention_32BitApplications Property     System.Boolean DataExecutionPrevention_32BitApplications {get;set;}
DataExecutionPrevention_Available         Property     System.Boolean DataExecutionPrevention_Available {get;set;}
DataExecutionPrevention_Drivers           Property     System.Boolean DataExecutionPrevention_Drivers {get;set;}
DataExecutionPrevention_SupportPolicy     Property     System.Byte DataExecutionPrevention_SupportPolicy {get;set;}
Debug                                     Property     System.Boolean Debug {get;set;}
Description                               Property     System.String Description {get;set;}
Distributed                               Property     System.Boolean Distributed {get;set;}
EncryptionLevel                           Property     System.UInt32 EncryptionLevel {get;set;}
ForegroundApplicationBoost                Property     System.Byte ForegroundApplicationBoost {get;set;}
FreePhysicalMemory                        Property     System.UInt64 FreePhysicalMemory {get;set;}
FreeSpaceInPagingFiles                    Property     System.UInt64 FreeSpaceInPagingFiles {get;set;}
FreeVirtualMemory                         Property     System.UInt64 FreeVirtualMemory {get;set;}
InstallDate                               Property     System.String InstallDate {get;set;}
LargeSystemCache                          Property     System.UInt32 LargeSystemCache {get;set;}
LastBootUpTime                            Property     System.String LastBootUpTime {get;set;}
LocalDateTime                             Property     System.String LocalDateTime {get;set;}
Locale                                    Property     System.String Locale {get;set;}
Manufacturer                              Property     System.String Manufacturer {get;set;}
MaxNumberOfProcesses                      Property     System.UInt32 MaxNumberOfProcesses {get;set;}
MaxProcessMemorySize                      Property     System.UInt64 MaxProcessMemorySize {get;set;}
Name                                      Property     System.String Name {get;set;}
NumberOfLicensedUsers                     Property     System.UInt32 NumberOfLicensedUsers {get;set;}
NumberOfProcesses                         Property     System.UInt32 NumberOfProcesses {get;set;}
NumberOfUsers                             Property     System.UInt32 NumberOfUsers {get;set;}
Organization                              Property     System.String Organization {get;set;}
OSLanguage                                Property     System.UInt32 OSLanguage {get;set;}
OSProductSuite                            Property     System.UInt32 OSProductSuite {get;set;}

As you can see there is plenty of good info here.  Very usable and also very usable for testing.

Load the DLL and explores the following:

  • $computer=Get-Computer localhost
  • $computer.OsInfo | gm
  • $computer.Services
  • $computer.Services["Browser"].State      #or start/stop and other methods

Download project: HERE.

There are dozens of useful methods and properties in a pipeline-able object that can be used to format reports with the Select-Object CmdLet and the Export-CliXML or ConverTo-Html and Export-CSV formatter CmdLets.


No comments:

Post a Comment