-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Import-Binary.ps1
40 lines (34 loc) · 875 Bytes
/
Import-Binary.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<#
.Synopsis
Imports objects using binary serialization.
Author: Roman Kuzmin
.Description
This command de-serializes objects from the specified binary file. Together
with Export-Binary.ps1 it is used for data persistence between sessions.
.Parameter Path
Specifies the path to the input file.
.Link
https://github.com/nightroman/PowerShelf
.Link
Export-Binary.ps1
#>
param
(
[Parameter(Mandatory=1)]
$Path
)
$Path = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($Path)
$formatter = New-Object System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
$stream = New-Object System.IO.FileStream ($Path, [System.IO.FileMode]::Open, [System.IO.FileAccess]::Read, [System.IO.FileShare]::Read)
try {
$length = $stream.Length
while($stream.Position -lt $length) {
$formatter.Deserialize($stream)
}
}
catch {
Write-Error $_
}
finally {
$stream.Close()
}