2

Import-CliXmlコマンドを使用して逆シリアル化されたオブジェクトを再インポートするとき、Powershell 5クラスとオブジェクト型に問題があります。逆シリアル化されたオブジェクト型の問題 - 具体的にはPowershell 5クラスとImport-CliXml

私はタイプのコンピュータのオブジェクトを持っていると私は私がエクスポートして、以下を使用してオブジェクトをインポートする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.Cコンピュータ]?

このタイプを[Deserialized]に追加する必要があります。コンピュータ]私はそう、私は単純に使用することになり、私のコード内で直接これを使用することはできません実行時に存在しません:

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

のインポート/エクスポート '$ deserializedComputer'は' [psobject] '' Deserialized.Computer'に設定された 'PSTypeNames'葉の値です。 '$ deserializedComputer.psobject.TypeNames'を参照してください。 –

答えて

2

使用(ヒント:gmはをGet-Memberのエイリアスです)

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

その後、あなたはまた、それが

を使用してコンピュータであることを確認するためにチェックを入力することができます

$type.TypeName 

のような値にアクセスすることができるはずです10

$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) 
関連する問題