forked from PoshWeb/OpenXML
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCopy-OpenXML.ps1
More file actions
108 lines (94 loc) · 3.9 KB
/
Copy-OpenXML.ps1
File metadata and controls
108 lines (94 loc) · 3.9 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
function Copy-OpenXML
{
<#
.SYNOPSIS
Copies OpenXML
.DESCRIPTION
Copies content from one OpenXML file to another
#>
param(
# The destination path
[Parameter(ValueFromPipelineByPropertyName)]
[Alias('Destination')]
[string]
$DestinationPath,
# The input object
[Parameter(ValueFromPipeline)]
[PSObject]
$InputObject,
# If set, will update existing packages.
[switch]
$Force
)
process {
# If the input was not a package
if ($inputObject -isnot [IO.Packaging.Package]) {
$loadedPackage = # see if it is a file we can load
if ($InputObject -is [IO.FileInfo]) {
Get-OpenXML $InputObject.FullName
} elseif ($inputFile = Get-Item -ErrorAction Ignore -Path "$InputObject") {
Get-OpenXML $inputFile
}
# If it was not, return.
if ($loadedPackage -isnot [IO.Packaging.Package]) { return }
$InputObject = $loadedPackage
}
# Get the absolute path of the destination, without creating the file,
$unresolvedDestination = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($DestinationPath)
# then see if the file exists.
$fileExists = Test-Path $unresolvedDestination
# If it does and we are not using the -Force
if ($fileExists -and -not $force) {
# write an error
Write-Error "$unresolvedDestionation already exists, use -Force to update" -Category ResourceExists
return
}
# If it did not exist, create it with New-Item -Force
elseif (-not $fileExists)
{
# this will create intermediate paths.
$newFile = New-Item -ItemType File -Path $unresolvedDestination -Force
if (-not $newFile) { return }
}
# Try to open or create our package for read and write.
$destinationPackage = [IO.Packaging.Package]::Open($unresolvedDestination, 'OpenOrCreate', 'ReadWrite')
# If we could not, we are done.
if (-not $destinationPackage) { return }
# Get the input parts and relationships
$inputPackageParts = $InputObject.GetParts()
$inputPackageRelationships = $InputObject.GetRelationships()
# For each part in the input
foreach ($inputPart in $inputPackageParts) {
# Create or open a part in the destination
$destinationPart =
if (-not $destinationPackage.PartExists($inputPart.Uri)) {
$destinationPackage.CreatePart($inputPart.Uri, $inputPart.ContentType)
} else {
$destinationPackage.GetPart($inputPart.Uri)
}
# and copy the streams.
$inputStream = $inputPart.GetStream()
$destinationStream = $destinationPart.GetStream()
$inputStream.CopyTo($destinationStream)
$inputStream.Close()
$destinationStream.Close()
}
# Then, create any relationships that do not exist.
foreach ($inputRelationship in $inputPackageRelationships) {
if ($inputRelationship) {
if (-not $destinationPackage.RelationshipExists($inputRelationship.id)) {
$null = $destinationPackage.CreateRelationship(
$inputRelationship.targetUri,
$inputRelationship.targetMode,
$inputRelationship.relationshipType,
$inputRelationship.id
)
}
}
}
# We can now close our package, writing the file.
$destinationPackage.Close()
# We want to open it right back up again as we output the updated file.
Get-OpenXML -FilePath $unresolvedDestination
}
}