我試圖使用$ function:foo值和get-item函數:foo。所有嘗試都成功修改臨時函數對象,但重新分配給存儲函數時(通過$ function:foo = ...或set-item function:foo ...),附加屬性會丟失。如何將屬性添加到PowerShell PSDrive上的函數對象「function:」?
這裏是我的嘗試的結果(全部失敗):
設置
$=>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
#1
$=>set-item function:f $f -force $=>$function:foo | gm b* >
#2
$=>$function:f = $f $=>$function:foo | gm b* >
#3
$=>$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* >
不知道我在做什麼錯。似乎這些屬性在重新分配時被剝離出來。那是對的嗎?定義的行爲?我還沒有看到任何文檔說FunctionInfo對象或ScriptBlocks異常處理。這是這個語言的一些深奧的角落嗎?
我只想將屬性附加到特定項目而不是全部類型。我贊同這個附件的臨時性質。但我重新分配對象並將其保存到函數:PSDrive中。其他屬性保留(有任何更改),但添加的屬性丟失。 – rivy 2009-02-13 20:11:26