-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Export-Binary.ps1
66 lines (57 loc) · 1.65 KB
/
Export-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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<#
.Synopsis
Exports objects using binary serialization.
Author: Roman Kuzmin
.Description
This command serializes objects to the specified binary file. Together with
Import-Binary.ps1 it is used for data persistence between sessions.
Objects should be serializable. The command stops on any serialization
issues and in this case the output file is not complete, more likely.
Nevertheless, objects written before an error can be recovered by
Import-Binary.ps1 with ErrorAction set to Continue or Ignore.
Note that in PowerShell V2 PSObject is not serializable.
.Inputs
Objects to be serialized.
.Outputs
None.
.Parameter Path
Specifies the path to the output file.
.Parameter InputObject
Specifies the objects to export. Use it either as the parameter for a
single object or pipe several objects to the command.
.Parameter Append
Tells to add the output to the end of the specified file.
.Link
https://github.com/nightroman/PowerShelf
.Link
Import-Binary.ps1
#>
param
(
[Parameter(Mandatory=1)]
[string]$Path,
[Parameter(ValueFromPipeline=1)]
$InputObject,
[switch]$Append
)
begin {
$ErrorActionPreference = 'Stop'
$Path = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($Path)
$formatter = New-Object System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
$mode = if ($Append) { [System.IO.FileMode]::Append } else { [System.IO.FileMode]::Create }
$stream = New-Object System.IO.FileStream ($Path, $mode, [System.IO.FileAccess]::Write, [System.IO.FileShare]::None)
}
process {
try {
if ($null -ne $InputObject) {
$formatter.Serialize($stream, $InputObject)
}
}
catch {
$stream.Close()
Write-Error $_
}
}
end {
$stream.Close()
}