-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathNew-PASPlatformPackage.ps1
234 lines (200 loc) · 8.78 KB
/
New-PASPlatformPackage.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
function New-PASPlatformPackage {
<#
.SYNOPSIS
Creates a CyberArk platform (policies and usages) package.
.DESCRIPTION
Creates a CyberArk platform package zip archive that can be deployed through the Privileged Vault Web Access. It will optionally create the PVWA settings from a provided Policies.xml file.
.EXAMPLE
PS C:\> Get-ChildItem C:\SamplePlatformBuild\files | New-PASPlatformPackage -PlatformId 'SamplePlatform' -CPMPolicyFile C:\SamplePlatformBuild\my-platforms-cpm-settings.ini -PVWASettingsFile C:\SamplePlatformBuild\my-platforms-pvwa-settings.xml
Creates a platform package zip archive for the SamplePlatform platform using the provided CPM policy and PVWA settings files. The optional platform files in C:\SamplePlatformBuild\files are included in the zip archive.
.EXAMPLE
PS C:\> New-PASPlatformPackage -PlatformId 'SamplePlatform' -CPMPolicyFile C:\SamplePlatformBuild\my-platforms-cpm-settings.ini -ExtractPVWASettings $true -PoliciesFile 'C:\Program Files (x86)\CyberArk\PSM\Temp'
Creates a platform package zip archive for the SamplePlatform platform using the provided CPM policy. The PVWA settings file is extracted out of an existing Policies.xml file and included in the zip archive.
.LINK
https://docs.cyberark.com/Product-Doc/OnlineHelp/PAS/Latest/en/Content/Platforms/Platform-Packages-Import-Introduction.htm
.LINK
https://docs.cyberark.com/Product-Doc/OnlineHelp/PAS/Latest/en/Content/Platforms/Platform-Packages-Import-Introduction.htm#ImportaPlatformPackage
#>
[CmdletBinding()]
param (
# Specifies the Id of the platform the package is being created for.
[Parameter(
Mandatory = $true,
ParameterSetName = 'ExtractPVWASettings'
)]
[Parameter(
Mandatory = $true,
ParameterSetName = 'ProvidePVWASettings'
)]
[string]
$PlatformId,
# Specifies a path to the CPM Policy file to be included in the package.
[Parameter(
Mandatory = $true,
ParameterSetName = 'ExtractPVWASettings'
)]
[Parameter(
Mandatory = $true,
ParameterSetName = 'ProvidePVWASettings'
)]
[ValidateScript( { Test-Path -Path $_ -PathType Leaf })]
[ValidateNotNullOrEmpty()]
[string]
$CPMPolicyFile,
[Parameter(
Mandatory = $false,
ParameterSetName = 'ProvidePVWASettings'
)]
[Parameter(Mandatory = $false)]
[ValidateScript( { Test-Path -Path $_ -PathType Leaf })]
[ValidateNotNullOrEmpty()]
[string]
$PVWASettingsFile,
# Extract the platform's settings out of an existing Policies.xml file
[Parameter(
Mandatory = $false,
ParameterSetName = 'ExtractPVWASettings'
)]
[boolean]
$ExtractPVWASettings,
# The platform settings to extract out of the Policies file.
[Parameter(
Mandatory = $false,
ParameterSetName = 'ExtractPVWASettings'
)]
[string]
$ExtractPlatform = $PlatformId,
[Parameter(
Mandatory = $true,
ParameterSetName = 'ExtractPVWASettings'
)]
[ValidateScript( { Test-Path -Path $_ -PathType Leaf })]
[ValidateNotNullOrEmpty()]
[string]
$PoliciesFile,
# Specifies a path to one or more locations.
[Parameter(
ValueFromPipeline = $true,
Mandatory = $false,
ParameterSetName = 'ExtractPVWASettings'
)]
[Parameter(
ValueFromPipeline = $true,
Mandatory = $false,
ParameterSetName = 'ProvidePVWASettings'
)]
[ValidateNotNullOrEmpty()]
[ValidateScript( { Test-Path -Path $_ })]
[string[]]
[Alias('PSPath')]
$Path,
# Folder path where to create the package zip archive.
[Parameter(
Mandatory = $false,
ParameterSetName = 'ExtractPVWASettings'
)]
[Parameter(
Mandatory = $false,
ParameterSetName = 'ProvidePVWASettings'
)]
$DestinationPath = $PWD,
# Overwrite policy ID in all platform files..
[Parameter(
Mandatory = $false,
ParameterSetName = 'ExtractPVWASettings'
)]
[Parameter(
Mandatory = $false,
ParameterSetName = 'ProvidePVWASettings'
)]
[boolean]
$OverwritePolicyIds = $false
)
begin {
$FilesToArchive = @()
$TemporaryDirectory = New-TemporaryDirectory
$PlatformWorkingDirectory = Join-Path -Path $TemporaryDirectory -ChildPath $PlatformId
New-Item -Path $PlatformWorkingDirectory -ItemType Directory
$CPMPolicyFile = Copy-Item -Path $CPMPolicyFile -Destination (Join-Path -Path $PlatformWorkingDirectory -ChildPath "Policy-$PlatformId.ini") -PassThru
if ($OverwritePolicyIds) { (Get-Content -Path $CPMPolicyFile) -replace 'PolicyID=(?<id>[\w\d]*)', "PolicyID=$PlatformId" | Set-Content -Path $CPMPolicyFile }
$FilesToArchive += $CPMPolicyFile
switch ($PSCmdlet.ParameterSetName) {
'ProvidePVWASettings' {
$PVWASettingsFile = Copy-Item -Path $PVWASettingsFile -Destination (Join-Path -Path $PlatformWorkingDirectory -ChildPath "Policy-$PlatformId.xml") -PassThru
if ($OverwritePolicyIds) { Update-PlatformXml -PlatformXmlFile $PVWASettingsFile -PlatformId $PlatformId }
$FilesToArchive += $PVWASettingsFile
}
'ExtractPVWASettings' {
$PVWASettingsFilePath = Join-Path -Path $PlatformWorkingDirectory -ChildPath "Policy-$PlatformId.xml"
$Settings = Get-PlatformPVWASettings -PlatformId $ExtractPlatform -PoliciesFile $PoliciesFile
$Settings | Set-Content -Path $PVWASettingsFilePath
if ($OverwritePolicyIds) { Update-PlatformXml -PlatformXmlFile $PVWASettingsFilePath -PlatformId $PlatformId }
$FilesToArchive += $PVWASettingsFilePath
}
}
}
process {
if ($Path.Count -gt 0) {
$FilesToArchive += Get-ChildItem -Path $Path | ForEach-Object FullName
}
else {
Write-Debug "No platform files passed!"
}
}
end {
Compress-Archive -Path $FilesToArchive -DestinationPath (Join-Path -Path $DestinationPath -ChildPath "$PlatformId.zip") -CompressionLevel Fastest
Remove-Item $TemporaryDirectory -Force -Recurse
}
}
function Get-PlatformPVWASettings {
param (
# Id of the platform to extract settings for.
[Parameter(Mandatory = $true)]
[string]$PlatformId,
# Path to the Policies.xml file to extract the settings from.
[Parameter(Mandatory = $true)]
[string]$PoliciesFile
)
$FileXml = [xml](Get-Content -Path $PoliciesFile)
$SelectXPath = "//Device/Policies/Policy[@ID='$PlatformId'] | //Usages/Usage[@ID='$PlatformId']"
$PlatformXml = $FileXml.SelectSingleNode($SelectXPath)
if ($PlatformXml.Name -eq 'Usage') {
$SettingsXml = $PlatformXml
}
elseif ($PlatformXml.Name -eq 'Policy') {
# Get name of the Device
$DeviceName = $PlatformXml.ParentNode.ParentNode.Name
# Create a new XML document.
$NewXml = New-Object xml
# Create the Device element with the Name attribute and add it to our new XML document.
$DeviceElement = $NewXml.CreateElement('Device')
$DeviceElement.SetAttribute('Name', $DeviceName)
# Must have Out-Null here as AppendChild returns an XmlNode and uncaptured output in PowerShell
# is implicity emitted to the pipeline.
$NewXml.AppendChild($DeviceElement) | Out-Null
# Create and add Policies
$PoliciesElement = $NewXml.CreateElement('Policies')
$NewXml.Device.AppendChild($PoliciesElement) | Out-Null
# Add the Policy element under Policies.
$Policy = $NewXml.ImportNode($PlatformXml, $true)
# FirstChild = Policies in this case as there is only one child.
$NewXml.Device.FirstChild.AppendChild($Policy) | Out-Null
$SettingsXml = $NewXml
}
return $SettingsXml.OuterXml
}
function New-TemporaryDirectory {
$parent = [System.IO.Path]::GetTempPath()
[string] $name = [System.Guid]::NewGuid()
New-Item -ItemType Directory -Path (Join-Path $parent $name)
}
function Update-PlatformXml {
param(
$PlatformId,
$PlatformXmlFile
)
$Xml = [xml](Get-Content -Path $PlatformXmlFile)
$PolicyNode = $Xml.SelectSingleNode("//Policy")
$PolicyNode.SetAttribute('ID', $PlatformId)
$Xml.Save((Get-ChildItem $PlatformXmlFile | Select-Object FullName).FullName)
}