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

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.

How to run GPResult on a remote client with PowerShell

In the past, to run the GPResult command, you would need to either physically visit this client, have the user do it, or use and RDP connection.  In all cases, this will disrupt the user.  First, you need PowerShell remoting enabled on the target machine.  You can do this via Group Policy . Open PowerShell and type this command. Invoke-Command –ScriptBlock {GPResult /r} –ComputerName <ComputerName> Replace <ComputerName> with the name of the target.  Remember, the target needs to be online and accessible to you.

Error icon when creating a GPO Preference drive map

You may not have an error at all.  Take a look at the drive mapping below. The red triangle is what threw us off.  It is not an error.  It is simply a color representation of the Replace option of the Action field in the properties of the drive mappings. Create action This give you a green triangle. The Create action creates a new mapped drive for users. Replace Action The Replace action gives you a red triangle.  This action will delete and recreate mapped drives for users. The net result of the Replace action is to overwrite all existing settings associated with the mapped drive. If the drive mapping does not exist, then the Replace action creates a new drive mapping. Update Action The Update action will have a yellow triangle. Update will modify settings of an existing mapped drive for users. This action differs from Replace in that it only updates settings defined within the preference item. All other settings remain as configured on the ma...