PowerShell脚本为特定值选择

如何在PowerShell显示中获取特定值?PowerShell脚本为特定值选择

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

命令:

Get-WmiObject win32_logicaldisk -Filter "Drivetype=3 

输出:

DeviceID : C:

DriveType : 3

ProviderName :

FreeSpace : 183760687104

Size : 255791026176

VolumeName :

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

回答:

你只需按名称访问字段:

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

以上是 PowerShell脚本为特定值选择 的全部内容, 来源链接: utcz.com/qa/261865.html

回到顶部