Powershell ConvertTo Json无法转换添加到内置类型的自定义属性

Powershell ConvertTo Json无法转换添加到内置类型的自定义属性,powershell,powershell-3.0,Powershell,Powershell 3.0,我正在尝试向内置PowerShell类型添加自定义属性,然后将该对象转换为Json。我遇到的问题是converttojson不会转换我添加的自定义属性。比如说, $Proc = Get-Process explorer $Proc.Modules | %{ $_ | Add-Member NoteProperty MyCustomProperty "123456" -PassThru } $Proc.Modules[0].MyCustomProperty -eq "123456"

我正在尝试向内置PowerShell类型添加自定义属性,然后将该对象转换为Json。我遇到的问题是converttojson不会转换我添加的自定义属性。比如说,

$Proc = Get-Process explorer
$Proc.Modules | %{
    $_ | Add-Member NoteProperty MyCustomProperty "123456" -PassThru
}

$Proc.Modules[0].MyCustomProperty -eq "123456"    
# Returns true

$Json = ConvertTo-Json ($Proc.Modules) -Depth 4
$Json -match "123456"   
# Returns false.  Expect it to be true
编辑:如果我在converttojson中使用“select*”,那么它就可以工作。例如

$Json = ConvertTo-Json ($Proc.Modules | select *) -Depth 4
$Json -match "123456"   
# Returns true

有人能解释为什么会发生这种情况吗?

似乎
converttojson
只在看到
PSObject
实例时才查看扩展属性。如果传递unwrapped对象,则只有基本对象属性将进入结果JSON:

Add-Type -TypeDefinition @'
    using System;
    using System.Management.Automation;
    public class SomeType {
        public static SomeType NewInstanceWithExtendedProperty() {
            SomeType instance = new SomeType();
            PSObject.AsPSObject(instance).Properties.Add(new PSNoteProperty("ExtendedProperty", "ExtendedValue"));
            return instance;
        }
        public string SomeProperty {
            get {
                return "SomeValue";
            }
        }

    }
'@
$a=[SomeType]::NewInstanceWithExtendedProperty()
ConvertTo-Json ($a, [PSObject]$a)
此代码将返回:

[
{
“SomeProperty”:“SomeValue”
},
{
“SomeProperty”:“SomeValue”,
“ExtendedProperty”:“ExtendedValue”
}
]

如您所见,
ExtendedProperty
仅当您显式地将
$a
强制转换为
PSObject

时才能进入JSON。似乎
转换为JSON
仅当它看到
PSObject
实例时才查看扩展属性。如果传递unwrapped对象,则只有基本对象属性将进入结果JSON:

Add-Type -TypeDefinition @'
    using System;
    using System.Management.Automation;
    public class SomeType {
        public static SomeType NewInstanceWithExtendedProperty() {
            SomeType instance = new SomeType();
            PSObject.AsPSObject(instance).Properties.Add(new PSNoteProperty("ExtendedProperty", "ExtendedValue"));
            return instance;
        }
        public string SomeProperty {
            get {
                return "SomeValue";
            }
        }

    }
'@
$a=[SomeType]::NewInstanceWithExtendedProperty()
ConvertTo-Json ($a, [PSObject]$a)
此代码将返回:

[
{
“SomeProperty”:“SomeValue”
},
{
“SomeProperty”:“SomeValue”,
“ExtendedProperty”:“ExtendedValue”
}
]

正如您所看到的,
ExtendedProperty
仅当您显式地将
$a
强制转换为
PSObject

PowerShell v2不包括
转换为JSON
cmdlet时才能进入JSON。如果使用某个自定义实现,则应指定哪一个。已修复。所有版本的PS>=3.0都会出现问题。
ConvertTo-Json
只有在看到
PSObject
时才会查看自定义属性。那么为什么“$Procs[40].Modules[0]|ConvertTo-Json”的输出包含自定义属性?@CharlieJoynt是的,我使用ILSpy来查看
ConvertTo-Json
行为。关于包装,不确定您想看到什么,这只是一个实验事实:
函数f{param([Parameter(valuefrompippeline)][Object]$InputObject)[Type]::GetTypeArray((,$InputObject))};[对象]::new()| f;f([object]::new())
-对
f
的第一次调用将打印您拥有的
PSObject
,而第二次打印
object
。PowerShell v2不包括
转换为Json
cmdlet。如果使用某个自定义实现,则应指定哪一个。已修复。所有版本的PS>=3.0都会出现问题。
ConvertTo-Json
只有在看到
PSObject
时才会查看自定义属性。那么为什么“$Procs[40].Modules[0]|ConvertTo-Json”的输出包含自定义属性?@CharlieJoynt是的,我使用ILSpy来查看
ConvertTo-Json
行为。关于包装,不确定您想看到什么,这只是一个实验事实:
函数f{param([Parameter(valuefrompippeline)][Object]$InputObject)[Type]::GetTypeArray((,$InputObject))};[对象]::new()| f;f([object]::new())
-第一次调用
f
将打印您拥有的
PSObject
,而第二次打印
对象