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.

Where did a User’s Account Get Locked Out?

Updated: May 15, 2015 When this article was originally published, two extra carriage returns were add causing the code to malfunction.  The code below is correct.   My client for this week’s PowerShell class had a really interesting question. They needed to know where an account is being locked out at. OK, interesting. Apparently users hop around clients and forget to log off, leading to eventual lock out of their accounts. The accounts can be unlocked, but are then relocked after Active Directory replication. This problem is solved in two parts. The first one is to modify the event auditing on the network. The second part is resolved with PowerShell. The first part involves creating a group policy that will encompass your Domain Controllers. In this GPO, make these changes. Expand Computer Configuration \ Policies \ Windows Settings \ Security Settings \ Advanced Audit Policy Configuration \ Audit Policies \ Account Management Double click User Account Management C...