2017-12-18 119 views
-1

如何在PowerShell顯示中獲取特定值?PowerShell腳本爲特定值選擇

示例 - 當我在腳本下執行時,我得到6個值,我只需要獲取第4行值。

命令:

Get-WmiObject win32_logicaldisk -Filter "Drivetype=3 

輸出:

 
DeviceID  : C: 
DriveType : 3 
ProviderName : 
FreeSpace : 183760687104 
Size   : 255791026176 
VolumeName : 

我需要獲取唯一 「183760687104」 的價值。如何實現它。我也不想擁有FreeSpace標籤。只是簡單的值「183760687104」。

+0

那些是對象的屬性。你應該顯示你需要的。你的問題在[這裏]回答(https://stackoverflow.com/q/47863699/3110834)。 –

回答

2

你只需按名稱訪問字段:

(Get-WmiObject Win32_LogicalDisk -Filter "Drivetype=3").FreeSpace 

注意Get-WmiObject已被棄用,你應該使用Get-CimInstance代替。

(Get-CimInstance Win32_LogicalDisk -Filter "Drivetype=3").FreeSpace 

如果你想保存在一個變量的值,只是爲它分配:

$freespace = (Get-CimInstance Win32_LogicalDisk -Filter "Drivetype=3").FreeSpace 

,然後根據你願意,你可以使用$freespace。但要小心,因爲在具有多個本地磁盤的系統上,您的表達式將返回一個值數組而不僅僅是一個值,因此您可能希望爲結果添加下標以僅選擇第一個磁盤。這兩種表達式都將是肯定給你一個數字:

PS C:\Users\User> (Get-CimInstance Win32_LogicalDisk -Filter "Drivetype=3").FreeSpace[0] 
94229651456 
PS C:\Users\User> (Get-CimInstance Win32_LogicalDisk -Filter "Drivetype=3")[0].FreeSpace 
94239125504 

如果你曾經在一個對象,只是管的表達上可用的字段疑問通過gm(簡稱Get-Member):

Get-CimInstance Win32_LogicalDisk -Filter "Drivetype=3" | gm 

將列出所有可用的字段。

PS C:\Users\User> Get-CimInstance Win32_LogicalDisk -Filter "Drivetype=3" | gm 


    TypeName: Microsoft.Management.Infrastructure.CimInstance#root/cimv2/Win32_LogicalDisk 

Name       MemberType Definition 
----       ---------- ---------- 
Clone      Method  System.Object ICloneable.Clone() 
Dispose      Method  void Dispose(), void IDisposable.Dispose() 
Equals      Method  bool Equals(System.Object obj) 
GetCimSessionComputerName Method  string GetCimSessionComputerName() 
GetCimSessionInstanceId  Method  guid GetCimSessionInstanceId() 
GetHashCode     Method  int GetHashCode() 
GetObjectData    Method  void GetObjectData(System.Runtime.Serialization.SerializationInfo info, Sys... 
GetType      Method  type GetType() 
ToString      Method  string ToString() 
Access      Property uint16 Access {get;} 
Availability     Property uint16 Availability {get;} 
BlockSize     Property uint64 BlockSize {get;} 
Caption      Property string Caption {get;} 
Compressed     Property bool Compressed {get;} 
ConfigManagerErrorCode  Property uint32 ConfigManagerErrorCode {get;} 
ConfigManagerUserConfig  Property bool ConfigManagerUserConfig {get;} 
CreationClassName   Property string CreationClassName {get;} 
Description     Property string Description {get;} 
DeviceID      Property string DeviceID {get;} 
DriveType     Property uint32 DriveType {get;} 
ErrorCleared     Property bool ErrorCleared {get;} 
ErrorDescription    Property string ErrorDescription {get;} 
ErrorMethodology    Property string ErrorMethodology {get;} 
FileSystem     Property string FileSystem {get;} 
FreeSpace     Property uint64 FreeSpace {get;} 
InstallDate     Property CimInstance#DateTime InstallDate {get;} 
LastErrorCode    Property uint32 LastErrorCode {get;} 
MaximumComponentLength  Property uint32 MaximumComponentLength {get;} 
MediaType     Property uint32 MediaType {get;} 
Name       Property string Name {get;} 
NumberOfBlocks    Property uint64 NumberOfBlocks {get;set;} 
PNPDeviceID     Property string PNPDeviceID {get;} 
PowerManagementCapabilities Property uint16[] PowerManagementCapabilities {get;} 
PowerManagementSupported  Property bool PowerManagementSupported {get;} 
ProviderName     Property string ProviderName {get;} 
PSComputerName    Property string PSComputerName {get;} 
Purpose      Property string Purpose {get;} 
QuotasDisabled    Property bool QuotasDisabled {get;} 
QuotasIncomplete    Property bool QuotasIncomplete {get;} 
QuotasRebuilding    Property bool QuotasRebuilding {get;} 
Size       Property uint64 Size {get;} 
Status      Property string Status {get;} 
StatusInfo     Property uint16 StatusInfo {get;} 
SupportsDiskQuotas   Property bool SupportsDiskQuotas {get;} 
SupportsFileBasedCompression Property bool SupportsFileBasedCompression {get;} 
SystemCreationClassName  Property string SystemCreationClassName {get;} 
SystemName     Property string SystemName {get;} 
VolumeDirty     Property bool VolumeDirty {get;} 
VolumeName     Property string VolumeName {get;set;} 
VolumeSerialNumber   Property string VolumeSerialNumber {get;} 
PSStatus      PropertySet PSStatus {Status, Availability, DeviceID, StatusInfo}