Birdman
Birdman

Reputation: 1524

How to access fields of specific PowerShell objects?

Coming from a traditional programming standpoint, I've always had a bit of a struggle scripting in PowerShell and figuring out what objects have which fields. It's super easy in an IDE in most languages to just look at the object fields.

I've been using Get-Member quite a lot lately which is very helpful at taking a stab and alleviating this frustration. However, I'm still having a bit of a hard time. Here's an example:

Command:

Get-BitLockerVolume | Get-Member

Output:

   TypeName: Microsoft.BitLocker.Structures.BitLockerVolume

Name                 MemberType Definition
----                 ---------- ----------
Equals               Method     bool Equals(System.Object obj)
GetHashCode          Method     int GetHashCode()
GetType              Method     type GetType()
ToString             Method     string ToString()
AutoUnlockEnabled    Property   System.Nullable[bool] AutoUnlockEnabled {get;}
AutoUnlockKeyStored  Property   System.Nullable[bool] AutoUnlockKeyStored {get;}
CapacityGB           Property   float CapacityGB {get;}
ComputerName         Property   string ComputerName {get;}
EncryptionMethod     Property   Microsoft.BitLocker.Structures.BitLockerVolumeEncryptionMethodOnGet EncryptionMethod {get;}
EncryptionPercentage Property   System.Nullable[float] EncryptionPercentage {get;}
KeyProtector         Property   System.Collections.ObjectModel.ReadOnlyCollection[Microsoft.BitLocker.Structures.BitLockerVolumeKeyProtector] KeyProtector {get;}
LockStatus           Property   Microsoft.BitLocker.Structures.BitLockerVolumeLockStatus LockStatus {get;}
MetadataVersion      Property   int MetadataVersion {get;}
MountPoint           Property   string MountPoint {get;}
ProtectionStatus     Property   Microsoft.BitLocker.Structures.BitLockerVolumeProtectionStatus ProtectionStatus {get;}
VolumeStatus         Property   System.Nullable[Microsoft.BitLocker.Structures.BitLockerVolumeStatus] VolumeStatus {get;}
VolumeType           Property   Microsoft.BitLocker.Structures.BitLockerVolumeType VolumeType {get;}
WipePercentage       Property   System.Nullable[float] WipePercentage {get;}

Okay great. Now what if I want to see the fields of the KeyProtector field?

Here I try this:

Get-BitLockerVolume | % {$_.KeyProtector | Get-Member}

On a system where bitlocker volumes actually have a valid key protector field, I can get the results, since the piped results aren't null.

   TypeName: Microsoft.BitLocker.Structures.BitLockerVolumeKeyProtector

Name                MemberType Definition                                                                              
----                ---------- ----------                                                                              
Equals              Method     bool Equals(System.Object obj)                                                          
GetHashCode         Method     int GetHashCode()                                                                       
GetType             Method     type GetType()                                                                          
ToString            Method     string ToString()                                                                       
AutoUnlockProtector Property   System.Nullable[bool] AutoUnlockProtector {get;}                                        
KeyCertificateType  Property   System.Nullable[Microsoft.BitLocker.Structures.BitLockerVolumeKeyProtectorCertificate...
KeyFileName         Property   string KeyFileName {get;}                                                               
KeyProtectorId      Property   string KeyProtectorId {get;}                                                            
KeyProtectorType    Property   Microsoft.BitLocker.Structures.BitLockerVolumeKeyProtectorType KeyProtectorType {get;}  
RecoveryPassword    Property   string RecoveryPassword {get;}                                                          
Thumbprint          Property   string Thumbprint {get;}                                                                
Equals              Method     bool Equals(System.Object obj)                                                          
GetHashCode         Method     int GetHashCode()                                                                       
GetType             Method     type GetType()                                                                          
ToString            Method     string ToString()                                                                       
AutoUnlockProtector Property   System.Nullable[bool] AutoUnlockProtector {get;}                                        
KeyCertificateType  Property   System.Nullable[Microsoft.BitLocker.Structures.BitLockerVolumeKeyProtectorCertificate...
KeyFileName         Property   string KeyFileName {get;}                                                               
KeyProtectorId      Property   string KeyProtectorId {get;}                                                            
KeyProtectorType    Property   Microsoft.BitLocker.Structures.BitLockerVolumeKeyProtectorType KeyProtectorType {get;}  
RecoveryPassword    Property   string RecoveryPassword {get;}                                                          
Thumbprint          Property   string Thumbprint {get;}                                                                
Equals              Method     bool Equals(System.Object obj)                                                          
GetHashCode         Method     int GetHashCode()                                                                       
GetType             Method     type GetType()                                                                          
ToString            Method     string ToString()                                                                       
AutoUnlockProtector Property   System.Nullable[bool] AutoUnlockProtector {get;}                                        
KeyCertificateType  Property   System.Nullable[Microsoft.BitLocker.Structures.BitLockerVolumeKeyProtectorCertificate...
KeyFileName         Property   string KeyFileName {get;}                                                               
KeyProtectorId      Property   string KeyProtectorId {get;}                                                            
KeyProtectorType    Property   Microsoft.BitLocker.Structures.BitLockerVolumeKeyProtectorType KeyProtectorType {get;}  
RecoveryPassword    Property   string RecoveryPassword {get;}                                                          
Thumbprint          Property   string Thumbprint {get;}     

How about on a system where I can't get away with this (most likely work around) solution? When no BitlockerVolume objects have a valid KeyProtector field, nothing is piped to Get-Member and it returns with an error.

What if I just want to view the properties of an object, where I don't have valid/instantiated objects to pass to the Get-Member cmdlet?

Upvotes: 1

Views: 1508

Answers (1)

ArcSet
ArcSet

Reputation: 6860

Lets talk about what we see.

Get-BitLockerVolume | Get-Member

we are looking for KeyProtector

KeyProtector         Property   System.Collections.ObjectModel.ReadOnlyCollection[Microsoft.BitLocker.Structures.BitLockerVolumeKeyProtector] KeyProtector {get;}

We can see that KeyProtector is an object [Microsoft.BitLocker.Structures.BitLockerVolumeKeyProtector]

So we can take that object and get member on it

[Microsoft.BitLocker.Structures.BitLockerVolumeKeyProtector] | get-member

It will come back with tons of stuff but what you probably really are looking for is the properties

[Microsoft.BitLocker.Structures.BitLockerVolumeKeyProtector].DeclaredProperties

And lets clean it up a little bit more

[Microsoft.BitLocker.Structures.BitLockerVolumeKeyProtector].DeclaredProperties | select Name

The Response is

Name               
----               
KeyProtectorId     
AutoUnlockProtector
KeyProtectorType   
KeyFileName        
RecoveryPassword   
KeyCertificateType 
Thumbprint        

Upvotes: 2

Related Questions