【问题标题】:How do you add a property to a function object on the PowerShell PSDrive "function:"?如何将属性添加到 PowerShell PSDrive“函数:”上的函数对象?
【发布时间】:2010-10-07 09:15:08
【问题描述】:

我尝试使用 $function:foo 值和 get-item function:foo。所有尝试都成功修改了临时函数对象,但在重新分配给存储函数时缺少附加属性(通过 $function:foo = ... 或 set-item function:foo ...)。

这是我尝试的结果(全部失败):

设置

$=>函数 foo { "foo" } $=>$f = $function:foo $=>$f = $f | add-member note 属性栏 BARvalue -pass $=>$f |通用汽车* 类型名称:System.Management.Automation.ScriptBlock 名称 MemberType 定义 ---- ---------- ---------- bar NoteProperty System.String bar=BARvalue

#1

$=>set-item 函数:f $f -force $=>$函数:foo |通用汽车* >

#2

$=>$函数:f = $f $=>$函数:foo |通用汽车* >

#3

$=>$f = 获取项目函数:foo $=>$f |通用汽车 类型名称:System.Management.Automation.FunctionInfo 名称 MemberType 定义 ---- ---------- ---------- 等于方法 System.Boolean Equals(Object obj) GetHashCode 方法 System.Int32 GetHashCode() GetType 方法 System.Type GetType() ToString 方法 System.String ToString() PSDrive NoteProperty System.Management.Automation.PSDriveInfo PSDrive=Function PIsContainer NoteProperty System.Boolean PIsContainer=False PSPath NoteProperty System.String PSPath=Microsoft.PowerShell.Core\Function::foo PSProvider NoteProperty System.Management.Automation.ProviderInfo PSProvider=Microsoft.... CmdletBinding 属性 System.Boolean CmdletBinding {get;} CommandType 属性 System.Management.Automation.CommandTypes CommandType {get;} DefaultParameterSet 属性 System.String DefaultParameterSet {get;} 定义属性 System.String 定义 {get;} 描述 属性 System.String 描述 {get;set;} 模块属性 System.Management.Automation.PSModuleInfo 模块 {get;} ModuleName 属性 System.String ModuleName {get;} 名称属性 System.String 名称 {get;} 选项属性 System.Management.Automation.ScopedItemOptions 选项 {get;set;} 参数 属性 System.Collections.Generic.Dictionary`2[[System.String, mscorli... ParameterSets 属性 System.Collections.ObjectModel.ReadOnlyCollection`1[[System.Man... ScriptBlock 属性 System.Management.Automation.ScriptBlock ScriptBlock {get;} 可见性属性 System.Management.Automation.SessionStateEntryVisibility Visibi... $=>$f = $f | add-member note 属性栏 barValue -pass $=>$f |通用汽车* 类型名称:System.Management.Automation.FunctionInfo 名称 MemberType 定义 ---- ---------- ---------- bar NoteProperty System.String bar=barValue $=>设置项功能:foo $f $=>$函数:foo |通用汽车* >

不知道我做错了什么。似乎在重新分配时这些属性被剥离了。那是对的吗?定义的行为?我没有看到任何文档说 FunctionInfo 对象或 ScriptBlocks 被异常处理。这是语言的某个深奥角落吗?

【问题讨论】:

    标签: powershell properties


    【解决方案1】:

    我的第一个想法是,当您将此属性附加到对象时,您会将其附加到该对象的特定实例。当您的变量丢失对该对象的引用时,该新属性的任何知识都会丢失。

    我的猜测是下一次获得该项目时,您正在创建一个具有 foo 属性的新 FunctionInfo 对象(存储在函数提供程序中)。

    当您调用 Get-Item 或 Get-ChildItem 时,它会返回对表示基础项的 .NET 类型的对象引用。这些项目不会无限期地存在于内存中(想象一下每个本地驱动器上的每个文件和内存中的每个映射驱动器都有一个 FileInfo 对象......哎哟)。由于每次调用 Get-Item 时 PowerShell 都会创建一个新实例,因此您将获得基本的 FunctionInfo 对象。

    如果您想为特定类型的所有项目添加属性,您可以使用 PowerShell 的可扩展类型系统。您可以创建自定义 .ps1xml 文件并将其加载到 PowerShell 会话中,该会话可以将属性添加到类型的每个实例。 PowerShell 团队博客上的一些很好的例子在这里 -> Hate Add-MemberLeveraging the PowerShell Type Extensions to Get Documentation

    编辑(解决评论):我了解您要执行的操作,但失败是由于新属性被“嫁接”到允许动态添加属性的 PSObject 包装器上。新属性永远不会真正属于您从 PSDrive 检索的 FunctionInfo 对象。

    此外,函数提供程序(函数:psdrive)没有存储该附加属性的机制。它知道并使用 FunctionInfo 对象。当您从 Function: PSDrive 请求项目时,Function 提供程序返回一个 FunctionInfo 对象。当您将函数保存到 Function: PSDrive 时,提供程序只能存储它知道如何处理的属性的值。可以编写一个自定义提供程序来处理来自 PSObject 包装器的新属性,但这不是此提供程序中默认功能的一部分。

    编辑#2:好的,这一直困扰着我。 I blogged about a workaround.

    【讨论】:

    • 我只想将属性附加到特定项目而不是所有类型。我同意附件的临时性质。但我正在重新分配对象并将其保存到函数中:PSDrive 它仍然存在。保留了其他属性(有任何更改),但缺少添加的属性。
    • 感谢您的澄清;这有助于我理解问题。也感谢您的解决方法。
    猜你喜欢
    • 2015-03-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-09
    • 2014-10-31
    • 2012-02-13
    • 1970-01-01
    • 2021-03-31
    相关资源
    最近更新 更多