[英]Get-ItemProperty with Registry, returned object type
I can use Get-Item
with folders, files and registry keys, and the type of the object I get back will make sense;我可以将
Get-Item
与文件夹、文件和注册表项一起使用,并且我返回的对象类型是有意义的; [System.IO.DirectoryInfo]
, [System.IO.FileInfo]
or [Microsoft.Win32.RegistryKey]
. [System.IO.DirectoryInfo]
、 [System.IO.FileInfo]
或[Microsoft.Win32.RegistryKey]
。
But with registry properties, what using Get-ItemProperty
returns is a [System.Management.Automation.PSCustomObject]
.但是对于注册表属性,使用
Get-ItemProperty
返回的是[System.Management.Automation.PSCustomObject]
。 Is this because there is no dedicated type for registry property?这是因为没有用于注册表属性的专用类型吗? That seems odd.
这似乎很奇怪。 But my Google-Fu is not turning anything up.
但是我的 Google-Fu 没有出现任何问题。
My use case is this, I am doing a series of Copy and Move tasks, with all four item types potentially getting copied or moved, and I want to implement an option to rename an existing destination rather than overwriting or failing.我的用例是这样的,我正在执行一系列复制和移动任务,所有四种项目类型都可能被复制或移动,我想实现一个选项来重命名现有目标,而不是覆盖或失败。 And exactly what the rename options are depends on the object type.
重命名选项的确切含义取决于对象类型。 And from a readability standpoint,
PSCustom Object
or a simple else
for RegistryProperty is a bit ugly.从可读性的角度来看,
PSCustom Object
或 RegistryProperty 的简单else
有点难看。 So, looking for a way to get the property back as a type with a more obvious name, so when I look at the code again in 12 months it makes some sense.因此,寻找一种方法将属性恢复为具有更明显名称的类型,因此当我在 12 个月后再次查看代码时,它是有道理的。
Get-ItemProperty
returns what is conceptually a registry value object : a property of a registry key that has a name and a - uh... - value (the named value object's data ). Get-ItemProperty
返回概念上的注册表值对象:具有名称和 - 呃... - 值(命名值对象的数据)的注册表项的属性。
The .NET registry API has no type to represent such a value object - instead, it allows access via the registry key type's .GetValue($valueName)
(to get a specific value object's data) and .GetValueNames()
methods (to get the list of all value names). .NET 注册表 API没有表示此类值对象的类型 - 相反,它允许通过注册表项类型的
.GetValue($valueName)
(以获取特定值对象的数据)和.GetValueNames()
方法(以获取所有值名称的列表)。
The PowerShell implementers apparently chose not to implement their own .NET type, and chose to use PowerShell's general-purpose, dynamic "property-bag" type, [pscustomobject]
[1] to model these value objects. PowerShell 实现者显然选择不实现他们自己的 .NET 类型,而是选择使用 PowerShell 的通用动态“属性包”类型
[pscustomobject]
[1]来对这些值对象进行建模。
If you want to avoid the [pscustomobject]
instances that Get-ItemProperty
returns, you can use Get-Item
instead, which returns a Microsoft.Win32.RegistryKey
instance, ie an instance of the .NET type representing a key , on which you can invoke the methods mentioned above.如果要避免
Get-ItemProperty
返回的[pscustomobject]
实例,可以改用Get-Item
,它返回Microsoft.Win32.RegistryKey
实例,即表示键的 .NET 类型实例,您可以在该实例上调用上面提到的方法。
As an aside: If you're just interested in a given value object's data , you can use the PSv5+顺便说一句:如果您只是对给定值对象的数据感兴趣,则可以使用 PSv5+
Get-ItemPropertyValue
cmdlet (eg Get-ItemPropertyValue
cmdlet(例如Get-ItemPropertyValue HKCU:\\Console -Name LineWrap
directly returns the [int]
data of the targeted value). Get-ItemPropertyValue HKCU:\\Console -Name LineWrap
直接返回目标值的[int]
数据)。
[1] It is possible - but wasn't done in this case - to assign (one or more) self-chosen type names to [pscustomobject]
instances, which PowerShell reflects as the type name in Get-Member
output (only the first, if there are multiple) and which it respects for ETS type definitions and format-data definitions. [1] 有可能 - 但在这种情况下没有完成 - 将(一个或多个)自选类型名称分配给
[pscustomobject]
实例,PowerShell 将其反映为Get-Member
输出中的类型名称(仅第一个,如果有多个)并且它尊重 ETS 类型定义和格式数据定义。 However, such pseudo types are not accessible as type literals ;但是,此类伪类型不能作为类型文字访问; eg:
$obj = [pscustomobject] @{ PSTypeName = 'MyType'; prop = 'foo' }
例如:
$obj = [pscustomobject] @{ PSTypeName = 'MyType'; prop = 'foo' }
$obj = [pscustomobject] @{ PSTypeName = 'MyType'; prop = 'foo' }
allows you test for this type name with $obj.pstypenames -contains 'MyType'
, but not with $obj -is [MyType]
. $obj = [pscustomobject] @{ PSTypeName = 'MyType'; prop = 'foo' }
允许您使用$obj.pstypenames -contains 'MyType'
测试此类型名称,但不能使用$obj -is [MyType]
。 That said, you can base parameter declarations on them, via the [PSTypeName()]
attribute.也就是说,您可以通过
[PSTypeName()]
属性将参数声明基于它们。
There is a way to get the type of the properties:有一种方法可以获取属性的类型:
$key = get-item hkcu:\key1
$key.GetValueKind('value1')
DWord
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.