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