【问题标题】:Deserialized Object type issues - specifically with Powershell 5 classes and Import-CliXml反序列化对象类型问题 - 特别是 Powershell 5 类和 Import-CliXml
【发布时间】:2017-06-08 00:35:07
【问题描述】:

在使用 Import-CliXml 命令重新导入反序列化对象时,我遇到了 Powershell 5 类和对象类型的问题。

我有一个计算机类型的对象,我希望将其存储为 xml,然后在下次运行脚本时重新导入

class Computer 
{
    $Private:hostname
    $Private:ipAddress

    Computer([String] $hostname, [String] $ipAddress)
    {
        $this.hostname = $hostname
        $this.ipAddress = $ipAddress
    }
    static [Computer] reserialize([PSObject] $deserializedComputer)
    {
        return [Computer]::new($deserializedComputer.hostname, $deserializedComputer.ipAddress)
    }
}

我使用以下方法导出和导入对象:

$computer = [Computer]::new("test-machine", "192.168.1.2")
$computer | Export-CliXml C:\Powershell\exportTest.xml
$deserializedComputer = Import-CliXml C:\Powershell\exportTest.xml

我知道当这个对象被重新导入时,它被反序列化并且基本上只是一个数据容器(类型为 [Deserialized.Computer])。在尝试使用我的 reserialize 方法重新序列化它之前,我试图弄清楚如何键入检查这个对象。

例如,如果我尝试投射 $deserializedComputer,它会告诉我:

Cannot convert value "Computer" to type "Computer". Error: "Cannot convert the "Computer" value of type "Deserialized.Computer" to type 
"Computer"."

我明白为什么不能强制转换,我只是使用错误消息指出对象知道它的类型是 [Deserialized.Computer]

我找不到从 $deserializedComputer.getMember() 返回的任何内容,表明它是 [Deserialized.Computer] 类型,我能找到的唯一信息是它是 [PSObject] 类型,我如何键入检查这个对象确实是 [Deserialized.Computer] 类型?

我应该添加该类型 [Deserialized.Computer] 在运行时不存在,所以我不能直接在我的代码中使用它,否则我会简单地使用:

$deserializedComputer.getType() -eq [Deserialized.Computer]

【问题讨论】:

  • $deserializedComputer 是一个[psobject],其PSTypeNames 叶值设置为Deserialized.Computer。见$deserializedComputer.psobject.TypeNames

标签: powershell serialization deserialization xml-deserialization powershell-5.0


【解决方案1】:

使用(提示:gm 是 Get-Member 的别名)

$type = $deserializedComputer | gm | Select -Property TypeName -First 1

那么你应该能够访问像

这样的值
$type.TypeName

您也可以使用 check 来确保它是一台计算机

$deserializedComputer.ToString()

或者如果你想要其他方式使用

[type]$deserializedComputer.ToString()

编辑:

您可以使用以下方法检查它

# you can also use $deserializedComputer.ToString() -eq [Computer].ToString()
if ([type]$deserializedComputer.ToString() -eq [Computer])
{

}

您的完整课程看起来像:

class Computer 
{
    $Private:hostname
    $Private:ipAddress

    Computer(){}

    Computer([String] $hostname, [String] $ipAddress)
    {
        $this.hostname = $hostname
        $this.ipAddress = $ipAddress
    }
    static [Computer] reserialize([PSObject] $deserializedComputer)
    {
    # you can also use $deserializedComputer.ToString() -eq [Computer].ToString()
    if ([type]$deserializedComputer.ToString() -eq [Computer])
    {
       return [Computer]::new($deserializedComputer.hostname, $deserializedComputer.ipAddress)
    }
    return [Computer]::new()
    }
}

以及导出/导入

$filePath = "C:\Powershell\exportTest.xml"

$computer = [Computer]::new("test-machine", "192.168.1.2")
$computer | Export-CliXml $filePath
$deserializedComputer = Import-CliXml $filePath

再序列化方法

[Computer]::reserialize($deserializedComputer)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多