Skip to main content

Describing Objects

With over 70 PowerShell classes under my belt, I have a good idea of where most IT Pros struggle when making the mind shift to the PowerShell world.  Objects is one of the concepts that take a bit of time to accept.  Even though everything in PowerShell is an object, we need to remember that objects have been in the realm of the developer for decades.  For IT Pros, this is a foreign concept that we must embrace.  Besides, objects make your scripting life so much easier.

Over the course of the next 7 blog postings, we will examine the different components of an object and supply you with some simple code examples to help you understand them better.

Object orientated programming was just a buzz word on the horizon when I graduated college with a degree in Computer Science in the late 90’s.  I was out in the real world as a Network Administrator as opposed to a programmer for many years.  Sure, I wrote some automation programs for myself, but they were not object based.  When PowerShell came about, I did not realize that objects were used at first.  It was not until a client asked me to stop teaching VBScript and teach PowerShell that I now needed to understand them.

As I started to play around with PowerShell, the light bulbs all of the sudden turned on in my head with regards to objects and this opened a whole new world of powerful, yet simplified coding for me. These next few blob post will hopefully open your eyes as to what objects are and help you make the transition to a much better place.

In the most basic sense, an object represents something. This could be a user account, a mailbox, a web page, or even a representation of the fan running on your device.  If you ask PowerShell to list all of the volumes on your device, each one will have the same members.  These members are properties, methods, and events.  We will describe each in more detail later on. Take a look at this output.
PS C:\> Get-Volume

DriveLetter FileSystemLabel FileSystem DriveType HealthStatus OperationalStatus SizeRemaining      Size
----------- --------------- ---------- --------- ------------ ----------------- -------------      ----
                            NTFS       Fixed     Healthy      OK                     99.03 MB    450 MB
                            NTFS       Fixed     Healthy      OK                    321.21 MB    350 MB
E           DATA            NTFS       Fixed     Healthy      OK                     21.73 GB 238.34 GB
D           DATA            NTFS       Fixed     Healthy      OK                     19.99 GB 238.34 GB
            WINRETOOLS      NTFS       Fixed     Healthy      OK                    212.47 MB    492 MB
C           OS              NTFS       Fixed     Healthy      OK                      5.06 GB 108.29 GB
            PBR Image       NTFS       Fixed     Healthy      OK                     243.9 MB   9.03 GB

Notice how each volume all contain the same members.  That is they all have a DriveLetter, HealthStatus, Size, etc… If I asked for the user accounts, would they have a SizeRemaining?  Doubtful.  In order to by a specific type of object, you must have the same members as other objects of the same type.  That way if I grab a list of all user objects, I know that all of the objects have a member called GivenName.


The next few articles in this series will describe each member in much further detail.

Comments

Popular posts from this blog

Adding a Comment to a GPO with PowerShell

As I'm writing this article, I'm also writing a customization for a PowerShell course I'm teaching next week in Phoenix.  This customization deals with Group Policy and PowerShell.  For those of you who attend my classes may already know this, but I sit their and try to ask the questions to myself that others may ask as I present the material.  I finished up my customization a few hours ago and then I realized that I did not add in how to put a comment on a GPO.  This is a feature that many Group Policy Administrators may not be aware of. This past summer I attended a presentation at TechEd on Group Policy.  One organization in the crowd had over 5,000 Group Policies.  In an environment like that, the comment section can be priceless.  I always like to write in the comment section why I created the policy so I know its purpose next week after I've completed 50 other tasks and can't remember what I did 5 minutes ago. In the Group Policy module for PowerShell V3, th

Return duplicate values from a collection with PowerShell

If you have a collection of objects and you want to remove any duplicate items, it is fairly simple. # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   # Remove the duplicate values. $Set1 | Select-Object -Unique 1 2 3 4 5 6 7 What if you want only the duplicate values and nothing else? # Create a collection with duplicate values $Set1 = 1 , 1 , 2 , 2 , 3 , 4 , 5 , 6 , 7 , 1 , 2   #Create a second collection with duplicate values removed. $Set2 = $Set1 | Select-Object -Unique   # Return only the duplicate values. ( Compare-Object -ReferenceObject $Set2 -DifferenceObject $Set1 ) . InputObject | Select-Object – Unique 1 2 This works with objects as well as numbers.  The first command creates a collection with 2 duplicates of both 1 and 2.   The second command creates another collection with the duplicates filtered out.  The Compare-Object cmdlet will first find items that are diffe

How to list all the AD LDS instances on a server

AD LDS allows you to provide directory services to applications that are free of the confines of Active Directory.  To list all the AD LDS instances on a server, follow this procedure: Log into the server in question Open a command prompt. Type dsdbutil and press Enter Type List Instances and press Enter . You will receive a list of the instance name, both the LDAP and SSL port numbers, the location of the database, and its status.