Skip to content

Commit ef572cf

Browse files
Feature/1.0.7 (#11)
* chore: Update module version to 1.0.7 and backup Entrust Security World files * chore: Update module version to 1.0.7 and backup Entrust Security World files
1 parent 1ae3431 commit ef572cf

8 files changed

Lines changed: 365 additions & 18 deletions

File tree

README.md

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ PowerShell module "SystemAdmins.AdcsToolbox" for Active Directory Certificate Se
88
- [Installation](#installation)
99
- [Usage](#usage)
1010
- [Cmdlets](#cmdlets)
11-
- [Backup-CADatabase](#Backup-CADatabase)
11+
- [Backup-CA](#Backup-CA)
12+
- Export-CACertificate
1213
- [Get-CACertificate](#Get-CACertificate)
1314
- [Get-CACommonName](#Get-CACommonName)
1415
- [Get-CACrlConfig](#Get-CACrlConfig)
@@ -82,7 +83,7 @@ Most AD CS servers don't have access to the internet, therefore it's required to
8283

8384
## Cmdlets
8485

85-
### Backup-CADatabase
86+
### Backup-CA
8687

8788
#### Synopsis
8889

@@ -95,24 +96,50 @@ Backup certificate authority with or without the private key.
9596
| String | Path | Backup folder path | False | C:\Path\To\My\Folder |
9697
| Switch | PrivateKey | Include private key in the backup | True | |
9798

98-
### Output
99-
100-
Hashtable
101-
10299
#### Example(s)
103100

104101
Create a backup without a private key to the folder "C:\Backup".
105102

106103
```powershell
107-
Backup-CADatabase -Path 'C:\Backup'
104+
Backup-CA -Path 'C:\Backup'
108105
```
109106

110107
Create a backup with the private key to the folder "C:\Backup".
111108

112109
```powershell
113-
Backup-CADatabase -Path 'C:\Backup' -PrivateKey
110+
Backup-CA -Path 'C:\Backup' -PrivateKey
114111
```
115112

113+
### Output
114+
115+
Hashtable
116+
117+
118+
119+
### Export-CACertificate
120+
121+
#### Synopsis
122+
123+
Export certificate authority certificate (public key).
124+
125+
#### Parameter(s)
126+
127+
| Type | Parameter | Description | Optional | Accepted Values |
128+
| ------ | ---------- | ------------------ | -------- | -------------------- |
129+
| String | FolderPath | Backup folder path | False | C:\Path\To\My\Folder |
130+
131+
#### Example(s)
132+
133+
Export the CA certificate (public key) the folder "C:\Backup".
134+
135+
```powershell
136+
Export-CACertificate -FolderPath 'C:\Backup'
137+
```
138+
139+
### Output
140+
141+
String
142+
116143

117144

118145
### Get-CACertificate

src/SystemAdmins.AdcsToolbox/SystemAdmins.AdcsToolbox.psd1

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
RootModule = 'SystemAdmins.AdcsToolbox.psm1';
44

55
# Version number of this module.
6-
ModuleVersion = '1.0.6';
6+
ModuleVersion = '1.0.7';
77

88
# Supported PSEditions
99
CompatiblePSEditions = @('Desktop');
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
function Backup-EntrustSecurityWorld
2+
{
3+
<#
4+
.SYNOPSIS
5+
Backup Entrust Security World files.
6+
.DESCRIPTION
7+
Creates a folder and backup the Entrust Security World files to the folder.
8+
.PARAMETER Path
9+
Backup path.
10+
.EXAMPLE
11+
Backup-EntrustSecurityWorld -Path 'C:\Backup';
12+
#>
13+
[cmdletbinding()]
14+
[OutputType([pscustomobject])]
15+
param
16+
(
17+
# Backup path.
18+
[Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
19+
[ValidateNotNullOrEmpty()]
20+
[ValidateScript({ $_ -match '^[a-zA-Z]:\\' })]
21+
[ValidateScript({ Test-Path $_ -PathType 'Container' -IsValid })]
22+
[string]$Path = $script:ModuleBackupFolder
23+
)
24+
25+
BEGIN
26+
{
27+
# Write to log.
28+
$customProgress = Write-CustomProgress -Activity $MyInvocation.MyCommand.Name -CurrentOperation 'Backup Entrust Security World';
29+
30+
# Get if the software is installed.
31+
$isInstalled = Test-EntrustSecurityWorldInstalled;
32+
33+
# Construct the backup folder.
34+
$backupFolderPath = Join-Path -Path $Path -ChildPath 'EntrustSecurityWorld';
35+
36+
# Security World data folder.
37+
$securityWorldDataFolderPath = Join-Path -Path $env:NFAST_KMDATA -ChildPath 'local';
38+
39+
# Result object.
40+
$result = [pscustomobject]@{
41+
BackupFolderPath = $backupFolderPath;
42+
SecurityWorldDataFolderPath = $securityWorldDataFolderPath;
43+
};
44+
}
45+
PROCESS
46+
{
47+
# If the software is not installed.
48+
if ($true -eq $isInstalled)
49+
{
50+
# If the backup path does not exist.
51+
if (-not (Test-Path -Path $backupFolderPath))
52+
{
53+
# Write to log.
54+
Write-CustomLog -Message ("Creating backup folder '{0}'" -f $backupFolderPath) -Level Verbose;
55+
56+
# Create the path.
57+
$null = New-Item -Path $backupFolderPath -ItemType 'Directory' -Force;
58+
}
59+
60+
# If the Security World folder does not exist.
61+
if (-not (Test-Path -Path $securityWorldDataFolderPath))
62+
{
63+
# Write to log.
64+
Write-CustomLog -Message ("Security World data folder '{0}' does not exist, skipping backup" -f $securityWorldDataFolderPath) -Level Verbose;
65+
}
66+
# Else the Security World folder exist.
67+
else
68+
{
69+
# Write to log.
70+
Write-CustomLog -Message ("Copying all files from '{0}' to '{1}'" -f $securityWorldDataFolderPath, $backupFolder) -Level Verbose;
71+
72+
# Copy the files from the folder.
73+
$null = Copy-Item -Path ($securityWorldDataFolderPath + '\*') -Destination $backupFolder -Recurse -Force;
74+
75+
# Write to event log.
76+
Write-CustomEventLog -EventId 152 -AdditionalMessage ('Backup folder is {0}' -f $backupFolder);
77+
}
78+
}
79+
}
80+
END
81+
{
82+
# Write to log.
83+
Write-CustomProgress @customProgress;
84+
85+
# If the software is installed.
86+
if ($true -eq $isInstalled)
87+
{
88+
# Return result.
89+
return $result;
90+
}
91+
}
92+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
function Test-EntrustSecurityWorldInstalled
2+
{
3+
<#
4+
.SYNOPSIS
5+
Test if the Entrust Security World Software is installed.
6+
.DESCRIPTION
7+
Return true or false.
8+
.EXAMPLE
9+
Test-EntrustSecurityWorldInstalled;
10+
#>
11+
[cmdletbinding()]
12+
[OutputType([bool])]
13+
param
14+
(
15+
)
16+
17+
BEGIN
18+
{
19+
# Write to log.
20+
$customProgress = Write-CustomProgress -Activity $MyInvocation.MyCommand.Name -CurrentOperation 'Check if Entrust Security World Software is installed';
21+
22+
# Boolean to return.
23+
[bool]$isInstalled = $false;
24+
}
25+
PROCESS
26+
{
27+
# If the software is installed.
28+
if (-not [string]::IsNullOrEmpty($env:NFAST_KMDATA))
29+
{
30+
# Write to log.
31+
Write-CustomLog -Message 'Entrust Security World Software is installed' -Level Verbose;
32+
33+
# Test if the folder exist.
34+
if (Test-Path -Path $env:NFAST_KMDATA)
35+
{
36+
# Write to log.
37+
Write-CustomLog -Message ("Security World data folder '{0}' exist" -f $env:NFAST_KMDATA) -Level Verbose;
38+
39+
# Set boolean to true.
40+
$isInstalled = $true;
41+
}
42+
}
43+
# Else the role is not installed.
44+
else
45+
{
46+
# Write to log.
47+
Write-CustomLog -Message 'Entrust Security World Software is not installed' -Level Verbose;
48+
49+
# Write to event log.
50+
Write-CustomEventLog -EventId 151;
51+
}
52+
}
53+
END
54+
{
55+
# Write to log.
56+
Write-CustomProgress @customProgress;
57+
58+
# Return boolean.
59+
return $isInstalled;
60+
}
61+
}

src/SystemAdmins.AdcsToolbox/private/module/eventlog.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,5 +184,17 @@
184184
"entryType": "Warning",
185185
"logName": "Application",
186186
"message": "The request was could not be removed"
187+
},
188+
{
189+
"eventId": 151,
190+
"entryType": "Information",
191+
"logName": "Application",
192+
"message": "Entrust Security World Software is not installed"
193+
},
194+
{
195+
"eventId": 152,
196+
"entryType": "Information",
197+
"logName": "Application",
198+
"message": "Created a copy of the Entrust Security World files"
187199
}
188200
]

src/SystemAdmins.AdcsToolbox/public/Backup-CADatabase.ps1 renamed to src/SystemAdmins.AdcsToolbox/public/Backup-CA.ps1

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function Backup-CADatabase
1+
function Backup-CA
22
{
33
<#
44
.SYNOPSIS
@@ -10,9 +10,9 @@ function Backup-CADatabase
1010
.PARAMETER PrivateKey
1111
Backup private key.
1212
.EXAMPLE
13-
Backup-CADatabase -Path 'C:\Backup';
13+
Backup-CA -Path 'C:\Backup';
1414
.EXAMPLE
15-
Backup-CADatabase -Path 'C:\Backup' -PrivateKey;
15+
Backup-CA -Path 'C:\Backup' -PrivateKey;
1616
#>
1717
[cmdletbinding()]
1818
[OutputType([pscustomobject])]
@@ -27,7 +27,11 @@ function Backup-CADatabase
2727

2828
# Private key backup.
2929
[Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
30-
[switch]$PrivateKey
30+
[switch]$PrivateKey,
31+
32+
# Password for the backup.
33+
[Parameter(Mandatory = $false, ValueFromPipelineByPropertyName = $true)]
34+
[string]$Password
3135
)
3236

3337
BEGIN
@@ -64,8 +68,11 @@ function Backup-CADatabase
6468
}
6569

6670
# If the path does not exist.
67-
if (-not (Test-Path $Path))
71+
if (-not (Test-Path -Path $Path))
6872
{
73+
# Write to log.
74+
Write-CustomLog -Message ("Creating backup folder '{0}'" -f $Path) -Level Verbose;
75+
6976
# Create the path.
7077
$null = New-Item -Path $Path -ItemType 'Directory' -Force;
7178
}
@@ -86,6 +93,25 @@ function Backup-CADatabase
8693
# Get the common name of the certificate authority.
8794
$commonName = Get-CACommonName;
8895

96+
# Splatting for the backup.
97+
$backupSplat = @{
98+
Path = $Path;
99+
ErrorAction = 'Stop';
100+
};
101+
102+
# If the password is set.
103+
if (-not [string]::IsNullOrEmpty($Password))
104+
{
105+
# Write to log.
106+
Write-CustomLog -Message 'Backup will be password protected' -Level Verbose;
107+
108+
# Convert the password to a secure string.
109+
$securePassword = ConvertTo-SecureString -String $Password -AsPlainText -Force;
110+
111+
# Add password to the splat.
112+
$null = $backupSplat.Add('Password', $securePassword);
113+
}
114+
89115
# Object to return.
90116
[pscustomobject]$result = [pscustomobject]@{
91117
DatabasePath = ('{0}\DataBase' -f $Path);
@@ -94,20 +120,33 @@ function Backup-CADatabase
94120
}
95121
PROCESS
96122
{
123+
# Export CA certificate.
124+
$null = Export-CACertificate -FolderPath $Path;
125+
97126
# If private key backup is requested.
98127
if ($true -eq $PrivateKey)
99128
{
100129
# Write to event log.
101130
Write-CustomEventLog -EventId 12;
102131

132+
# If Entrust Security World is installed.
133+
if ($true -eq (Test-EntrustSecurityWorldInstalled))
134+
{
135+
# Backup Entrust Security World.
136+
$entrustSecurityWorld = Backup-EntrustSecurityWorld -Path $Path;
137+
138+
# Add member to result.
139+
$null = Add-Member -InputObject $result -MemberType NoteProperty -Name 'EntrustSecurityWorldPath' -Value $entrustSecurityWorld.BackupFolderPath -Force;
140+
}
141+
103142
# Try to backup the private key.
104143
try
105144
{
106145
# Write to log.
107146
Write-CustomLog -Message ("Trying to backup the database with private key to the directory '{0}'" -f $Path) -Level Verbose;
108147

109148
# Backup the database.
110-
Backup-CARoleService -Path $Path -KeepLog -Force -ErrorAction Stop;
149+
Backup-CARoleService @backupSplat;
111150

112151
# Write to log.
113152
Write-CustomLog -Message ("Successfully made a backup of the database including the private key to the directory '{0}'" -f $Path) -Level Verbose;
@@ -128,7 +167,7 @@ function Backup-CADatabase
128167
Write-CustomEventLog -EventId 3;
129168

130169
# Backup without private key.
131-
$null = Backup-CADatabase -Path $Path;
170+
$null = Backup-CA -Path $Path;
132171
}
133172
}
134173
# Else backup without private key.
@@ -144,7 +183,7 @@ function Backup-CADatabase
144183
Write-CustomLog -Message ("Trying to backup the database without the private key to the directory '{0}'" -f $Path) -Level Verbose;
145184

146185
# Backup the database.
147-
Backup-CARoleService -Path $Path -DatabaseOnly -KeepLog -Force -ErrorAction Stop;
186+
Backup-CARoleService -DatabaseOnly @backupSplat;
148187

149188
# Write to log.
150189
Write-CustomLog -Message ("Successfully made a backup of the database without the private key to the directory '{0}'" -f $Path) -Level Verbose;

0 commit comments

Comments
 (0)