当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 关于物业

作者:精品下载站 日期:2024-12-14 02:20:15 浏览:13 分类:玩电脑

关于物业


简短描述

描述如何在 PowerShell 中使用对象属性。

详细描述

PowerShell 使用称为对象的结构化信息集合来表示数据存储中的项目或计算机的状态。通常,您使用属于 Microsoft .NET Framework 的对象,但也可以在 PowerShell 中创建自定义对象。

物品与其对象之间的关联非常密切。当您更改对象时,通常会更改它所代表的项目。例如,当您在 PowerShell 中获取文件时,您不会获取实际的文件。相反,您会得到一个代表该文件的 FileInfo 对象。当您更改 FileInfo 对象时,文件也会更改。

大多数对象都有属性。属性是与对象关联的数据。不同类型的对象具有不同的属性。例如,表示文件的 FileInfo 对象具有 IsReadOnly 属性,如果该文件具有只读属性,则该属性包含 $True如果没有,则为 $False。表示文件系统目录的 DirectoryInfo 对象具有包含父目录路径的 Parent 属性。

对象属性

要获取对象的属性,请使用 Get-Member cmdlet。例如,要获取 FileInfo 对象的属性,请使用 Get-ChildItem cmdlet 获取表示文件的 FileInfo 对象。然后,使用管道运算符 (|) 将 FileInfo 对象发送到 Get-Member。以下命令获取 pwsh.exe 文件并将其发送到 Get-Member$PSHOME 自动变量包含 PowerShell 安装目录的路径。

Get-ChildItem $PSHOME\pwsh.exe | Get-Member

该命令的输出列出了 FileInfo 对象的成员。成员包括属性和方法。当您使用 PowerShell 时,您可以访问对象的所有成员。

要仅获取对象的属性而不获取方法,请使用 Get-Member cmdlet 的 MemberType 参数,其值为 Property,如下例所示。

Get-ChildItem $PSHOME\pwsh.exe | Get-Member -MemberType Property
TypeName: System.IO.FileInfo

Name              MemberType Definition
----              ---------- ----------
Attributes        Property   System.IO.FileAttributes Attributes {get;set;}
CreationTime      Property   System.DateTime CreationTime {get;set;}
CreationTimeUtc   Property   System.DateTime CreationTimeUtc {get;set;}
Directory         Property   System.IO.DirectoryInfo Directory {get;}
DirectoryName     Property   System.String DirectoryName {get;}
Exists            Property   System.Boolean Exists {get;}
Extension         Property   System.String Extension {get;}
FullName          Property   System.String FullName {get;}
IsReadOnly        Property   System.Boolean IsReadOnly {get;set;}
LastAccessTime    Property   System.DateTime LastAccessTime {get;set;}
LastAccessTimeUtc Property   System.DateTime LastAccessTimeUtc {get;set;}
LastWriteTime     Property   System.DateTime LastWriteTime {get;set;}
LastWriteTimeUtc  Property   System.DateTime LastWriteTimeUtc {get;set;}
Length            Property   System.Int64 Length {get;}
Name              Property   System.String Name {get;}

找到属性后,您可以在 PowerShell 命令中使用它们。

属性值

尽管特定类型的每个对象都具有相同的属性,但这些属性的值描述了特定的对象。例如,每个 FileInfo 对象都有一个 CreationTime 属性,但每个文件的该属性值都不同。

获取对象属性值的最常见方法是使用成员访问运算符 (.)。键入对该对象的引用,例如包含该对象的变量或获取该对象的命令。然后,键入运算符 (.),后跟属性名称。

例如,以下命令显示 pwsh.exe 文件的 CreationTime 属性的值。 Get-ChildItem 命令返回表示 pwsh.exe 文件FileInfo 对象。该命令括在括号中,以确保在访问任何属性之前执行该命令。

(Get-ChildItem $PSHOME\pwsh.exe).CreationTime
Tuesday, June 14, 2022 5:17:14 PM

您还可以将对象保存在变量中,然后使用成员访问 (.) 方法获取其属性,如以下示例所示:

$a = Get-ChildItem $PSHOME\pwsh.exe
$a.CreationTime
Tuesday, June 14, 2022 5:17:14 PM

您还可以使用 Select-ObjectFormat-List cmdlet 来显示对象的属性值。 Select-ObjectFormat-List 各有一个 Property 参数。您可以使用Property参数来指定一个或多个属性及其值。或者,您可以使用通配符 (*) 来表示所有属性。

例如,以下命令显示 pwsh.exe 文件的所有属性的值。

Get-ChildItem $PSHOME\pwsh.exe | Format-List -Property *
PSPath              : Microsoft.PowerShell.Core\FileSystem::C:\Program Files\PowerShell-preview\pwsh.exe
PSParentPath        : Microsoft.PowerShell.Core\FileSystem::C:\Program Files\PowerShell-preview
PSChildName         : pwsh.exe
PSDrive             : C
PSProvider          : Microsoft.PowerShell.Core\FileSystem
PSIsContainer       : False
Mode                : -a---
ModeWithoutHardLink : -a---
VersionInfo         : File:             C:\Program Files\PowerShell-preview\pwsh.exe
                      InternalName:     pwsh.dll
                      OriginalFilename: pwsh.dll
                      FileVersion:      7.3.0.5
                      FileDescription:  pwsh
                      Product:          PowerShell
                      ProductVersion:   7.3.0-preview.5 SHA: cfc237ac85cf24fa760923ace568201c8f3256aa
                      Debug:            False
                      Patched:          False
                      PreRelease:       False
                      PrivateBuild:     False
                      SpecialBuild:     False
                      Language:         Language Neutral

BaseName            : pwsh
ResolvedTarget      : C:\Program Files\PowerShell-preview\pwsh.exe
Target              :
LinkType            :
Length              : 285088
DirectoryName       : C:\Program Files\PowerShell-preview
Directory           : C:\Program Files\PowerShell-preview
IsReadOnly          : False
FullName            : C:\Program Files\PowerShell-preview\pwsh.exe
Extension           : .exe
Name                : pwsh.exe
Exists              : True
CreationTime        : 6/14/2022 5:17:14 PM
CreationTimeUtc     : 6/14/2022 10:17:14 PM
LastAccessTime      : 7/18/2022 11:32:06 AM
LastAccessTimeUtc   : 7/18/2022 4:32:06 PM
LastWriteTime       : 6/14/2022 5:17:14 PM
LastWriteTimeUtc    : 6/14/2022 10:17:14 PM
LinkTarget          :
Attributes          : Archive

静态属性

您可以在 PowerShell 中使用 .NET 类的静态属性。静态属性是类的属性,与标准属性不同,标准属性是对象的属性。

要获取类的静态属性,请使用 Get-Member cmdlet 的 Static 参数。例如,以下命令获取 System.DateTime 类的静态属性。

Get-Date | Get-Member -MemberType Property -Static
TypeName: System.DateTime

Name     MemberType Definition
----     ---------- ----------
MaxValue Property   static datetime MaxValue {get;}
MinValue Property   static datetime MinValue {get;}
Now      Property   datetime Now {get;}
Today    Property   datetime Today {get;}
UtcNow   Property   datetime UtcNow {get;}

要获取静态属性的值,请使用以下语法。

[<ClassName>]::<Property>

例如,以下命令获取 System.DateTime 类的 UtcNow 静态属性的值。

[System.DateTime]::UtcNow

成员访问枚举

从 PowerShell 3.0 开始,当您使用成员访问运算符 (.) 访问列表集合中不存在的属性时,PowerShell 会自动枚举集合中的项目并返回以下值每个项目的属性。有关详细信息,请参阅 about_Member-Access_Enumeration。

示例

此命令返回 Get-Service 返回的每个服务的 DisplayName 属性值。

(Get-Service).DisplayName
Application Experience
Application Layer Gateway Service
Windows All-User Install Agent
Application Identity
Application Information
...

所有集合都有一个 Count 属性,用于返回集合中对象的数量。

(Get-Service).Count
176

从 PowerShell 3.0 开始,您可以获取非集合的单例对象的 CountLength 属性。

(Get-Service Audiosrv).Count
1

但是,某些对象具有长度属性。例如,字符串的长度是字符串中的字符数。 Count 属性是对象实例的数量。

PS> $str = 'string'
PS> $str.Length
6
PS> $str.Count
1

如果单个对象和集合上都存在属性,则仅返回集合的属性。

$collection = @(
    [pscustomobject]@{length = "foo"}
    [pscustomobject]@{length = "bar"}
)
# PowerShell returns the collection's Length.
$collection.length
2

参见

  • about_Objects
  • about_Member-Access_Enumeration
  • about_方法
  • Format-List
  • Get-Member
  • Select-Object

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯