-
Notifications
You must be signed in to change notification settings - Fork 14
/
Import-DataTable.ps1
46 lines (39 loc) · 3 KB
/
Import-DataTable.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
function Import-DataTable
{
<#
.Synopsis
Imports a datatable
.Description
Imports a datatable from a file on disk. The datatable must be exported with Export-Datatable
.Link
Export-DataTable
.Link
ConvertTo-DataTable
.Example
dir | Select Name, LastWriteTime, CreationTime | Export-DataTable -OutputPath $home\FileInfo.dat
Import-DataTable $home\FileInfo.dat
#>
[OutputType([Data.Datatable])]
param(
# The path to the data table.
[Parameter(Mandatory=$true,Position=0,ValueFromPipelineByPropertyName=$true)]
[Alias('FullName')]
$FilePath
)
process {
#region Resolve absolute path
$resolvedPath = $ExecutionContext.SessionState.Path.GetResolvedPSPathFromPSPath($FilePath)
if (-not $resolvedPath) { return }
#endregion Resolve absolute path
#region Open file and compressed stream
$fileStream = New-Object IO.FileStream "$resolvedPath", Open
$cs = New-Object System.IO.Compression.GZipStream($fileStream, [IO.Compression.CompressionMode]"Decompress")
#endregion Open file and compressed stream
# Deserialize the data
,(New-Object System.Runtime.Serialization.Formatters.Binary.BinaryFormatter).Deserialize($cs)
#region Close compressed stream and file
$cs.Close()
$fileStream.Close()
#endregion Close compressed stream and file
}
}