Environment Info:
PS C:\> Get-WmiObject Win32_OperatingSystem
SystemDirectory : C:\Windows\system32
Organization :
BuildNumber : 9600
RegisteredUser : xxxxxxxxxxxxxxxxxxxxxxx
SerialNumber : xxxxx-xxxxx-xxxxx-xxxxx
Version : 6.3.9600 # Windows 8.1, Update 1
PS C:\> $PSVersionTable
Name Value
---- -----
PSVersion 5.1.14409.1018
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.14409.1018
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
Background:
why I made this:
- my USB HDD is a little bit unstable. and I need to find files and store paths progressively.
- if some error will occur, store the error information instead of the path.
- I intend to use
[TreeElement]objects for storing and searching.
I made a simple tree class:
# need Powershell v5.0 higher
class TreeElement
{
$Value = $null
[System.Collections.Generic.List[TreeElement]] $Children = [System.Collections.Generic.List[TreeElement]]::new()
[TreeElement] $Parent = $null
TreeElement($Value)
{
$this.Value = $Value
}
[TreeElement] AddChild([TreeElement] $Child)
{
$this.Children.Add($Child)
return $Child
}
[TreeElement] AddChildValue($ChildValue)
{
$Child = [TreeElement]::new($ChildValue)
$this.Children.Add($Child)
return $Child
}
}
I finished coding, and tested like this:
$root = [TreeElement]::new(1)
$root.AddChildValue(2)
$root.AddChildValue(3)
$root.AddChildValue(4)
$root.AddChildValue(5)
$root
The last line will show this in the console.
Value Children Parent
----- -------- ------
1 {TreeElement, TreeElement, TreeElement, TreeElement}
nop.
I wanted to store $root to a file.
so I decided to use *-Clixml, and coded:
Function Store-Tree
{
[OutputType([void])]
Param
(
[string]$Path,
[TreeElement]$RootElement
)
Export-Clixml -Path $Path -InputObject $RootElement
}
Function Restore-Tree
{
Param
(
[string]$Path
)
# restore from xml
$obj = Import-Clixml -Path $Path
# Reconstruct instances...
$result = Reconstruct-Tree -RootObject $obj
return $result
}
Function Reconstruct-Tree
{
[OutputType([TreeElement])]
Param
(
[PSObject] $RootObject
)
$root = [TreeElement]::new($RootObject.Value)
foreach($ChildObject in $RootObject.Children)
{
[TreeElement] $branch = Reconstruct-Tree $ChildObject
[void] $root.AddChild($branch)
}
return $root
}
Then I tested it:
PS> mkdir C:\temp
PS> $path = "C:\temp\root-tree.xml"
PS> $root # original instance
Value Children Parent
----- -------- ------
1 {TreeElement, TreeElement, TreeElement, TreeElement}
PS> Store-Tree -Path $path -RootElement $root # store in xml. instances are converted to [PsObject] internally.
PS> $root_r = Restore-Tree -Path $path # restore instances with type of [TreeElement].
Value Children Parent
----- -------- ------
TreeElement {} # what?
Problem:
I expected $root_r has an instance almost the same as $root.
but $root_r has an expected object in the Children member.
I wanted to return just the object.
debugging showed that the [TreeElement] constructor was running when the function Reconstruct-Tree returned the object.
Question:
Is there a way to return just the object? Did I do something wrong?
I googled some site, but no information was gained.
Any help is appreciated.
Thank you for your help in advance.