如何访问特定PowerShell对象的字段?

时间:2019-05-24 17:14:58

标签: powershell

从传统的编程角度来看,我在PowerShell中编写脚本一直很费劲,并且弄清楚哪些对象具有哪些字段。在大多数语言中的IDE中,仅查看对象字段是非常容易的。

我最近一直在使用Get-Member,这对防止刺伤和减轻这种挫败感非常有帮助。但是,我仍然有一些困难。这是一个示例:

命令:

Get-BitLockerVolume | Get-Member

输出:

   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;}

很好。现在,如果我想查看KeyProtector字段的字段怎么办?

我在这里尝试:

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

在系统上,bitlocker卷实际上具有有效的密钥保护器字段,我可以得到结果,因为通过管道传输的结果不为空。

   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;}     

在无法解决这个(最可能解决)方案的系统上呢?当没有BitlockerVolume对象具有有效的KeyProtector字段时,没有任何内容通过管道传递到Get-Member,并且返回错误。

如果我只想查看对象的属性,而我没有有效/实例化的对象来传递给Get-Member cmdlet,该怎么办?

1 个答案:

答案 0 :(得分:1)

让我们谈谈我们所看到的。

Get-BitLockerVolume | Get-Member

我们正在寻找KeyProtector

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

我们可以看到KeyProtector是一个对象[Microsoft.BitLocker.Structures.BitLockerVolumeKeyProtector]

所以我们可以拿到那个物体并成为它的成员

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

它将返回大量的东西,但是您可能真正想要的是属性

[Microsoft.BitLocker.Structures.BitLockerVolumeKeyProtector].DeclaredProperties

让我们多清理一下

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

响应为

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