PowerShell脚本选择特定值[重复]

问题描述 投票:-1回答:1

这个问题在这里已有答案:

如何在PowerShell显示中获取特定值?

示例 - 当我执行下面的脚本时,我得到6个值,我只需要获得第4行值。

命令:

Get-WmiObject win32_logicaldisk -Filter "Drivetype=3

输出:

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

我只需要获取“183760687104”值。如何实现它。另外我也不想拥有FreeSpace标签。只是简单的价值“183760687104”。

powershell
1个回答
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

如果您对对象上的可访问字段有疑问,只需通过gmGet-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}
© www.soinside.com 2019 - 2024. All rights reserved.