Objects


As we discussed in Chapter 4, an object is essentially a black box that has been designed by a developer to fill a certain need. The Microsoft Windows operating system contains hundreds of objects. From an administrative scripting or automation perspective, many of these objects can be exploited through scripting.

We've stressed many times that PowerShell is an object-oriented shell based on .NET. This means you will manipulate objects in the shell and in PowerShell scripts.

Properties

All objects have properties. Some objects have more properties than other objects. The properties depend on the object. Consider an automobile as an object. For many people, an automobile is essentially a "black box" that they don't fully understand how it works, but they do know how to use it. An automobile "object" may have properties such as model, color, horsepower, and maximum speed. These properties will have values such as Ferrari, red, 450, and 180.

Object properties that can be changed are called read-write properties. In the automobile example, the color property might be read-write because you can change the color of the car by painting it. Other properties that can't be changed by the user are considered read-only properties. In the automobile object example, horsepower or maximum speed are read-only properties since they can't be changed by the user.

Methods

An object's methods are actions that the object can take. Depending on the object, some methods are executed from the object, while other methods are executed on the object, usually to make a change to the object.

Continuing with the automobile object, it may have an accelerate method that makes it do something. Or it may have a ChangeTire method that makes a change to the object itself. Some methods can take parameters to fine tune what the method will do. For example, the automobile's accelerate method may require two parameters: 1) a start speed; and 2) end speed.

Generally, an object's methods and properties are specified using a dotted notation. Let's say we created a car object called objMyCar. We could set some properties using the dotted notation:

  • objMyCar.color="blue"

  • objMyCar.horsepower=300

  • objMyCar.MaxSpeed=120

  • Calling a method can be as simple as:

    • objMyCar.Accelerate(0,60)

This pseudo-command calls the Accelerate method of the car object with a starting speed parameter of 0 and an end speed parameter of 60.

That's all fine with a metaphorical object, but you are probably wondering how this applies to things you care about like WMI or file system objects. So, let's begin by discussing how you find out about the properties and methods of objects. One way is to review the online documentation from Web sites such as MSDN or by using solid reference books.

With the addition of .NET, PowerShell reflection can make this even easier. Now you can use the Get-Member cmdlet to ask the object what it can do and how it can be configured. Once you have a reference to an object, pass it to Get-Member through a pipeline:

 PS C:\> $host |get-member    TypeName: System.Management.Automation.Internal.Host.InternalHost Name                   MemberType Definition ----                   ---------- ---------- EnterNestedPrompt      Method     System.Void EnterNestedPrompt() Equals                 Method     System.Boolean Equals(Object obj) ExitNestedPrompt       Method     System.Void ExitNestedPrompt() GetHashCode            Method     System.Int32 GetHashCode() GetType                Method     System.Type GetType() get_CurrentCulture     Method     System.Globalization.CultureInfo get get_CurrentUICulture   Method     System.Globalization.CultureInfo get get_InstanceId         Method     System.Guid get_InstanceId() get_Name               Method     System.String get_Name() get_PrivateData        Method     System.Management.Automation.PSObjec get_UI                 Method     System.Management.Automation.Host.PS get_Version            Method     System.Version get_Version() NotifyBeginApplication Method     System.Void NotifyBeginApplication() NotifyEndApplication   Method     System.Void NotifyEndApplication() SetShouldExit          Method     System.Void SetShouldExit(Int32 exit ToString               Method     System.String ToString() CurrentCulture         Property   System.Globalization.CultureInfo Cur CurrentUICulture       Property   System.Globalization.CultureInfo Cur InstanceId             Property   System.Guid InstanceId {get;} Name                   Property   System.String Name {get;} PrivateData            Property   System.Management.Automation.PSObjec UI                     Property   System.Management.Automation.Host.PS Version                Property   System.Version Version {get;} PS C:\> 

If you only want to see an object's properties, you can use Get-Member -membertype properties. You still may need to refer to documentation to fully understand what all the output means. However, at least you have an idea of what you're looking for when you read the documentation.

With a little clever coding you can even see what the values are for a given property. This can often help when learning about a particular object:

 PS C:\> $props=$host|get-member -membertype properties PS C:\> foreach ($p in $props) { >> Write-host "Property:" $p.name >> $host.($p.name) >> } >> Property: CurrentCulture LCID             Name             DisplayName ----             ----             ----------- 1033             en-US            English (United States) Property: CurrentUICulture 1033             en-US            English (United States) Property: InstanceId Property: Name ConsoleHost Property: PrivateData ErrorForegroundColor    : Red ErrorBackgroundColor    : Black WarningForegroundColor  : Yellow WarningBackgroundColor  : Black DebugForegroundColor    : Yellow DebugBackgroundColor    : Black VerboseForegroundColor  : Yellow VerboseBackgroundColor  : Black ProgressForegroundColor : Yellow ProgressBackgroundColor : DarkCyan Property: UI RawUI : System.Management.Automation.Internal.Host.InternalHostRawUse Property: Version Major         : 1 Minor         : 0 Build         : 0 Revision      : 0 MajorRevision : 0 MinorRevision : 0 PS C:\> 

In this example we create a variable called $props that contains all the properties of the $host object. Next we use ForEach to enumerate $props, displaying the name of each property $p.name, and then displaying the value of each property with $host.($p.name). Putting $p.name in parentheses let us create the equivalent of $host.version or $host.name.



Windows PowerShell. TFM
Internet Forensics
ISBN: 982131445
EAN: 2147483647
Year: 2004
Pages: 289

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net