Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何向PowerShell PSDrive上的函数对象添加属性;功能:“;?_Powershell_Properties - Fatal编程技术网

如何向PowerShell PSDrive上的函数对象添加属性;功能:“;?

如何向PowerShell PSDrive上的函数对象添加属性;功能:“;?,powershell,properties,Powershell,Properties,我尝试使用$function:foo值和get-item函数:foo。所有尝试都成功地修改了临时函数对象,但在重新分配给存储函数时(通过$function:foo=…或set item function:foo…)缺少附加属性 以下是我尝试的结果(全部失败): 设置 $=>function foo { "foo" } $=>$f = $function:foo $=>$f = $f | add-member noteproperty bar BARvalue -pass $=>$f | gm b

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

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

设置

$=>function foo { "foo" } $=>$f = $function:foo $=>$f = $f | add-member noteproperty bar BARvalue -pass $=>$f | gm b* TypeName: System.Management.Automation.ScriptBlock Name MemberType Definition ---- ---------- ---------- bar NoteProperty System.String bar=BARvalue $=>函数foo{“foo”} $=>$f=$function:foo $=>$f=$f |添加成员注释属性栏BARvalue-传递 $=>$f | gm b* TypeName:System.Management.Automation.ScriptBlock 名称成员类型定义 ---- ---------- ---------- bar NoteProperty System.String bar=BARvalue #一,

$=>设置项功能:f$f-force $=>$函数:foo | gm b* > #二,

$=>$函数:f=$f $=>$函数:foo | gm b* > #三,

$=>$f=获取项目函数:foo $=>$f| gm 类型名称:System.Management.Automation.FunctionInfo 名称成员类型定义 ---- ---------- ---------- 等于方法系统。布尔等于(对象obj) GetHashCode方法System.Int32 GetHashCode() GetType方法系统。类型GetType() ToString方法System.String ToString() PSDrive NoteProperty System.Management.Automation.PSDriveInfo PSDrive=函数 PSIsContainer NoteProperty System.Boolean PSIsContainer=False PSPath NoteProperty System.String PSPath=Microsoft.PowerShell.Core\Function::foo PSProvider NoteProperty System.Management.Automation.ProviderInfo PSProvider=Microsoft。。。。 CmdletBinding属性系统。布尔CmdletBinding{get;} CommandType属性System.Management.Automation.CommandTypes CommandType{get;} DefaultParameterSet属性System.String DefaultParameterSet{get;} 定义属性系统。字符串定义{get;} 描述属性系统。字符串描述{get;set;} 模块属性System.Management.Automation.PSModuleInfo模块{get;} ModuleName属性系统。字符串ModuleName{get;} 名称属性系统。字符串名称{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.SessionStatentryVisibili。。。 $=>$f=$f |添加成员注释属性栏barValue-传递 $=>$f | gm b* 类型名称:System.Management.Automation.FunctionInfo 名称成员类型定义 ---- ---------- ---------- bar NoteProperty System.String bar=barValue $=>设置项函数:foo$f $=>$函数:foo | gm b* >
不确定我做错了什么。重新分配时,属性似乎被剥离。这是正确的吗?定义的行为?我没有看到任何文档说明FunctionInfo对象或脚本块被异常处理。这是语言的某个深奥的角落吗?

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

我的猜测是,下次获取该项时,您将使用foo的属性(存储在函数提供程序中)创建一个新的FunctionInfo对象

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

如果要向特定类型的所有项添加属性,可以使用PowerShell的可扩展类型系统。可以创建自定义.ps1xml文件并将其加载到PowerShell会话中,该会话可以向类型的每个实例添加属性。PowerShell团队博客上的一些很好的示例如下->和

编辑(寻址注释):我理解您试图做什么,但失败是因为新属性被“嫁接”到PSObject包装器上,该包装器允许动态添加属性。新属性从来不是您从PSDrive检索的FunctionInfo对象的一部分

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


编辑#2:好的,这一直困扰着我。

我只想将属性附加到特定项目,而不是所有类型的项目。我确实同意附件的临时性质。但我正在重新分配对象并将其保存到函数:PSDrive,保留它。其他属性(有任何更改),但缺少添加的。感谢您的澄清;这有助于我理解问题。也感谢您的解决方法。 $=>set-item function:f $f -force $=>$function:foo | gm b* > $=>$function:f = $f $=>$function:foo | gm b* > $=>$f = get-item function:foo $=>$f | gm TypeName: System.Management.Automation.FunctionInfo Name MemberType Definition ---- ---------- ---------- Equals Method System.Boolean Equals(Object obj) GetHashCode Method System.Int32 GetHashCode() GetType Method System.Type GetType() ToString Method System.String ToString() PSDrive NoteProperty System.Management.Automation.PSDriveInfo PSDrive=Function PSIsContainer NoteProperty System.Boolean PSIsContainer=False PSPath NoteProperty System.String PSPath=Microsoft.PowerShell.Core\Function::foo PSProvider NoteProperty System.Management.Automation.ProviderInfo PSProvider=Microsoft.... CmdletBinding Property System.Boolean CmdletBinding {get;} CommandType Property System.Management.Automation.CommandTypes CommandType {get;} DefaultParameterSet Property System.String DefaultParameterSet {get;} Definition Property System.String Definition {get;} Description Property System.String Description {get;set;} Module Property System.Management.Automation.PSModuleInfo Module {get;} ModuleName Property System.String ModuleName {get;} Name Property System.String Name {get;} Options Property System.Management.Automation.ScopedItemOptions Options {get;set;} Parameters Property System.Collections.Generic.Dictionary`2[[System.String, mscorli... ParameterSets Property System.Collections.ObjectModel.ReadOnlyCollection`1[[System.Man... ScriptBlock Property System.Management.Automation.ScriptBlock ScriptBlock {get;} Visibility Property System.Management.Automation.SessionStateEntryVisibility Visibi... $=>$f = $f | add-member noteproperty bar barValue -pass $=>$f | gm b* TypeName: System.Management.Automation.FunctionInfo Name MemberType Definition ---- ---------- ---------- bar NoteProperty System.String bar=barValue $=>set-item function:foo $f $=>$function:foo | gm b* >