Advanced Windows PowerShell Scripting Video Training

Advanced Windows PowerShell Scripting Video Training
Advanced Windows PowerShell Scripting Video Training

Thursday, December 1, 2016

Describing Object Properties

Today we are going to look at a member of an object called a property.  Simply put, a property describes something. Take your phone for instance.  Let’s identify several properties. 
  •         Color
  •         Make
  •         Model
  •         Operating System
  •         Memory


Every cell phone on the market would have these properties, but we all know the value of these properties will not be the same.  Here are mine:
Property
Value
Color
Black
Make
Samsung
Model
Galaxy 7
Operating System
Android
Memory
32GB

Does your phone’s property values match mine? Most likely not.  Since your phone has the same properties as mine, we can make a safe assumption that it is an object of type Cell Phone.  Let’s go with our example from yesterday and take a look at the properties of a volume.  We are going to use the cmdlet Get-Member to show us all of the properties in the object produced by Get-Volume
PS C:\> Get-Volume | Get-Member -MemberType Properties


   TypeName:
Microsoft.Management.Infrastructure.CimInstance#ROOT/Microsoft/Windows/Storage/MSFT_Volume

Name                 MemberType     Definition                                           
----                 ----------     ----------                                            
AllocationUnitSize   Property       uint32 AllocationUnitSize {get;}                     
DriveLetter          Property       char DriveLetter {get;}                              
FileSystem           Property       string FileSystem {get;}                             
FileSystemLabel      Property       string FileSystemLabel {get;set;}                    
ObjectId             Property       string ObjectId {get;}                                
PassThroughClass     Property       string PassThroughClass {get;}                       
PassThroughIds       Property       string PassThroughIds {get;}                         
PassThroughNamespace Property       string PassThroughNamespace {get;}                   
PassThroughServer    Property       string PassThroughServer {get;}                      
Path                 Property       string Path {get;}                                   
PSComputerName       Property       string PSComputerName {get;}                         
Size                 Property       uint64 Size {get;}                                   
SizeRemaining        Property       uint64 SizeRemaining {get;}                          
UniqueId             Property       string UniqueId {get;}                               
DedupMode            ScriptProperty System.Object DedupMode {get=switch ($this.psBase.C...
DriveType            ScriptProperty System.Object DriveType {get=switch ($this.psBase.C...
FileSystemType       ScriptProperty System.Object FileSystemType {get=switch ($this.psB...
HealthStatus         ScriptProperty System.Object HealthStatus {get=switch ($this.psBas...
OperationalStatus    ScriptProperty System.Object OperationalStatus {get=$_status = @()...

There are actually 4 different property types that you may come across.  The way how to property was created determines what type of property it is.

NoteProperty: A .NET object.

AliasProperty: An alias for another property.

ScriptProperty: A property with get and set methods written in PowerShell.

CodeProperty: A property with get and set methods written in C#, VB.

A script property is one where you can actually see the code that determined the value of the property.  Take a look at our Get-Volume example.
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 the value of DriveType.  The word “Fixed” is not stored as part of the data. It is actually the integer value of 3. This is because there is a defined list of acceptable values for this property and storing a number, as opposed to a string, is more memory efficient.  Take a look at this definition of this ScriptProperty.

PS C:\> Get-Volume | Get-Member -Name Drivetype | Select-Object -ExpandProperty Definition
System.Object DriveType {get=switch ($this.psBase.CimInstanceProperties["DriveType"].Value)

          {
          0 { "Unknown" }
          1 { "Invalid Root Path" }
          2 { "Removable" }
          3 { "Fixed" }
          4 { "Remote" }
          5 { "CD-ROM" }
          6 { "RAM Disk" }
          Default { "Unknown" }
          };}

Let’s ask the Object of Get-Volume to give us it’s TypeName
PS C:\> Get-Volume | Get-Member | Select-Object -First 1 -Property TypeName

TypeName                                                                                 
--------                                                                                 
Microsoft.Management.Infrastructure.CimInstance#ROOT/Microsoft/Windows/Storage/MSFT_Volume


Going online and searching for MFST_Volume takes us to. https://msdn.microsoft.com/en-us/library/windows/desktop/hh830604(v=vs.85).aspx. Here is a screen shot of the DriveType property description. 

The data type for this property is an integer.  It is read only and it has 7 valid values.  The value of 3 represents a “Fixed” drive.

You will not be able to run Get-Help on an object to get information on what its members mean.  You will have to go to MSDN for that. To determine your search query for an objects information, use the TypeName.  If the TypeName contains a “\” like the one above, only query everything to the right of the final “\”.  In this case, MSFT_Volume. Look at the example below.

PS C:\> Get-Process | Get-Member | Select-Object -First 1 -Property TypeName

TypeName                 
--------                 
System.Diagnostics.Process

Since the TypeName does not contain any “\”, your query will be System.Diagnostics.Process.  You can also use my Show-MSDNResource cmdlet.  Make sure you read the setup instructions and help file.  You can get the cmdlet here.

You may also notice that the MSDN resources do not contain all of the properties that Get-Member displays.  That is because there are other cmdlets used in the process of collecting the information. These cmdlet add members to the object in the pipeline, but are not part of the original object. Here is the list of properties for MSFT_Volume on MSDN


Notice that it contains 10 properties.  Here is the result of Get-Volume | Get-Member.

PS C:\> Get-volume | Select-Object -Property * -First 1


OperationalStatus     : OK
HealthStatus          : Healthy
DriveType             : Fixed
FileSystemType        : NTFS
DedupMode             : NotAvailable
ObjectId              : {1}\\JASONPC2\root/Microsoft/Windows/Storage/Providers_v2\WSP_Volu
                        me.ObjectId="{c918366b-4c74-11e3-824f-806e6f6e6963}:VO:\\?\Volume{
                        463405a4-618b-43f5-a8d2-6279f60cf907}\"
PassThroughClass      :
PassThroughIds        :
PassThroughNamespace  :
PassThroughServer     :
UniqueId              : \\?\Volume{463405a4-618b-43f5-a8d2-6279f60cf907}\
AllocationUnitSize    : 4096
DriveLetter           :
FileSystem            : NTFS
FileSystemLabel       :
Path                  : \\?\Volume{463405a4-618b-43f5-a8d2-6279f60cf907}\
Size                  : 471855104
SizeRemaining         : 103841792
PSComputerName        :
CimClass              : ROOT/Microsoft/Windows/Storage:MSFT_Volume
CimInstanceProperties : {ObjectId, PassThroughClass, PassThroughIds,
                        PassThroughNamespace...}
CimSystemProperties   : Microsoft.Management.Infrastructure.CimSystemProperties

It contains 22 properties.  Again, this is because PowerShell and the cmdlets used to get this information is adding their own information to the object as it passes through the pipeline.

You may also noticed that some values are NULL.  This is OK.  The reason for this is either new properties were added to this objects description (class) and your current technology does not support it or it is older properties that newer technology does not use.  In either case, it is OK.  PowerShell will not error.  It will just leave the value at NULL.

No comments: