Skip to content

Commit 4890882

Browse files
authored
Added logging, improved error handling
1 parent e1c9eaf commit 4890882

File tree

1 file changed

+89
-22
lines changed

1 file changed

+89
-22
lines changed

GetSSL.ps1

+89-22
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,56 @@
1-
<#
1+
<#
22
Created by: David Nahodyl, Blue Feather 10/8/2016
33
Contact: contact@bluefeathergroup.com
4-
Last Updated: 2/20/2020
5-
Version: 2.0
4+
Last Updated: 4/7/2020
5+
Version: 2.1
66
77
Need help? We can set this up to run on your server for you! Send an email to
88
contact@bluefeathergroup.com or give a call at (770) 765-6258
99
#>
1010

1111
<# Change the domain variable to the domain/subdomain for which you would like
12-
an SSL Certificate#>
12+
an SSL Certificate#>
1313
$domains = 'fms.mydomain.com';
1414

1515
<# You can also get a certificate for multiple host name. Uncomment the line below
1616
and enter your domains in the array matching the example format if you'd like a
1717
mult-domain certificate. Let's Encrypt will peform separate validation for each
1818
of the domains, so be sure that your server is reachable at all of them before
1919
attempting to get a certificate. #>
20-
# $domains = 'fms.mycompany.com,second.mycompany.com';
20+
#$domains = 'fms.mydomain.com,subdomain.mydomain.com';
2121

2222

23-
<# Change the contact email address to your real email address so that Let's Encrypt
24-
can contact you if there are any problems #>
23+
<# Change the contact email address to your real email address so that Let's Encrypt
24+
can contact you if there are any problems #>
2525
$email = 'test@mydomain.com'
2626

2727
<# Enter the path to your FileMaker Server directory, ending in a backslash \ #>
2828
$fmsPath = 'C:\Program Files\FileMaker\FileMaker Server\'
2929

30-
<# enter the path to le64.exe #>
31-
$le64Path = 'C:\Program Files\FileMaker\le64.exe'
30+
<# Enter the path to le64.exe #>
31+
$le64Path = 'C:\Program Files\FileMaker\SSL Renewal\le64.exe'
3232

3333
<# Enable or disable test mode with a boolean 1 or 0. This is set true (1) by default for safety during initial testing but will need
3434
# to be set to false (0) to get a real certificate.#>
3535
$testMode = 1
3636

37+
38+
3739
<#
3840
You should not need to edit anything below this point.
3941
---------------------------------------------------------------------------------------------------#>
4042

43+
$outPath = $PSScriptRoot + '\'
44+
$logFile = $outPath + '\SSL-Renewal.log'
45+
46+
<# Disable any already-running transcript #>
47+
$ErrorActionPreference="SilentlyContinue"
48+
Stop-Transcript | out-null
49+
$ErrorActionPreference = "Continue" # or "Stop"
50+
51+
<# Start the transcript #>
52+
Start-Transcript -path $logFile
53+
4154
if ($domain -eq('fms.mydomain.com')){
4255
Write-Output 'You must enter your real domain! The script will now exit.'
4356
exit
@@ -107,10 +120,10 @@ Catch
107120
</system.webServer>
108121
</configuration>' | Out-File -FilePath $webConfigPath;
109122

110-
$keyPath = $fmsPath + 'CStore\serverKey.pem'
111-
$certPath = $fmsPath + 'CStore\crt.pem'
112-
$csrPath = $fmsPath + 'CStore\domain.csr'
113-
$accountPath = $fmsPath + 'CStore\account.key'
123+
$keyPath = $outPath+ 'key.pem'
124+
$certPath = $outPath+ 'certificate.pem'
125+
$csrPath = $outPath + 'domain.csr'
126+
$accountPath = $outPath + 'account.key'
114127

115128

116129

@@ -129,20 +142,38 @@ if (-not $testMode)
129142
<# check if the certificate succeeded and exit if there was a failure #>
130143
if ($LASTEXITCODE -ne 0)
131144
{
132-
exit;
145+
<# Stop the transcript #>
146+
Stop-Transcript
147+
exit
133148
}
134149

135150

136151
<# cd to FMS directory to run fmsadmin commands #>
137-
cd $fmsPath'\Database Server\';
152+
cd $fmsPath'\Database Server\'
138153

139-
<# Install the certificate #>
140-
<#fmsadmin certificate import requires confirmation in 17, so put a '-y' in here to skip input. This won't do anything in earlier versions. #>
141-
.\fmsadmin certificate import $certPath -y;
142154

143-
<# Append the intermediary certificate to support older FMS before 15 #>
144-
Add-Content $fmsPath'CStore\serverCustom.pem' '
145-
-----BEGIN CERTIFICATE-----
155+
$cstorePath = $fmsPath + 'CStore\'
156+
$liveKeyPath = $cstorePath + 'serverKey.pem'
157+
$oldKeyPath = $cstorePath + 'oldKey.pem'
158+
159+
Write-Output 'Comparing private key files'
160+
161+
162+
$haveMovedKey = 0
163+
164+
if(Compare-Object -ReferenceObject $(Get-Content $keyPath) -DifferenceObject $(Get-Content $liveKeyPath)){
165+
Write-Output 'Key is different. Moving old key and replacing'
166+
Move-Item -Path $liveKeyPath -Destination $oldKeyPath
167+
$haveMovedKey = 1
168+
} else {
169+
Write-Output 'Keys are the same'
170+
}
171+
172+
173+
174+
Write-Output writing out intermediary
175+
$intermediaryPath = $outPath + 'intermediary.pem';
176+
$intermediaryContents = '-----BEGIN CERTIFICATE-----
146177
MIIEkjCCA3qgAwIBAgIQCgFBQgAAAVOFc2oLheynCDANBgkqhkiG9w0BAQsFADA/
147178
MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT
148179
DkRTVCBSb290IENBIFgzMB4XDTE2MDMxNzE2NDA0NloXDTIxMDMxNzE2NDA0Nlow
@@ -170,12 +201,48 @@ PfZ+G6Z6h7mjem0Y+iWlkYcV4PIWL1iwBi8saCbGS5jN2p8M+X+Q7UNKEkROb3N6
170201
KOqkqm57TH2H3eDJAkSnh6/DNFu0Qg==
171202
-----END CERTIFICATE-----'
172203

204+
Set-Content -Path $intermediaryPath -Value $intermediaryContents
205+
206+
207+
208+
Compare-Object $keyPath $liveKeyPath
209+
210+
Write-Output 'Attempting to install certificate to FileMaker Server'
211+
212+
<# Install the certificate #>
213+
<#fmsadmin certificate import requires confirmation in 17, so put a '-y' in here to skip input. This won't do anything in earlier versions. #>
214+
.\fmsadmin certificate import $certPath --keyfile $keyPath --intermediateCA $intermediaryPath -y;
215+
216+
217+
<# Check and make sure the install succeeded #>
218+
if ($LASTEXITCODE -ne 0)
219+
{
220+
<# The certificate install failed #>
221+
Write-Output 'fmsadmin certificate install command failed.'
222+
223+
<# Move the old private key back if there was a problem #>
224+
if ($haveMovedKey){
225+
Write-Output 'Moving old key back to original location'
226+
Move-Item -Path $oldKeyPath -Destination $liveKeyPath
227+
}
228+
229+
Write-Output 'Exiting Script'
230+
<# Stop the transcript #>
231+
Stop-Transcript
232+
exit;
233+
}
234+
235+
Write-Output 'FMS certificate import command completed'
236+
173237
<# Restart the FMS service #>
174238
Write-Output 'Automatically Stopping FileMaker Server'
175239
net stop 'FileMaker Server';
176240
Write-Output 'Automatically Starting FileMaker Server'
177241
net start 'FileMaker Server';
178242

179243

244+
<# Stop the transcript #>
245+
Stop-Transcript
246+
180247
<# All done! Exit. #>
181-
exit;
248+
exit;

0 commit comments

Comments
 (0)