-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImport-SCOMCertificate.ps1
305 lines (249 loc) · 11.5 KB
/
Import-SCOMCertificate.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<#
.SYNOPSIS
This script imports a selected certificate into the SCOM (System Center Operations Manager) configuration.
.DESCRIPTION
The script allows the user to select a certificate from the local machine's certificate store, validates the selected certificate against specific criteria, and then imports the certificate into the SCOM configuration by updating the registry and restarting the HealthService.
.PARAMETER None
This script does not take any parameters.
.EXAMPLE
.\Import-SCOMCertificate.ps1
This command runs the script and opens a GUI for selecting and importing a certificate.
.NOTES
Author: Lorne Sepaugh
Date: 2024-10-05
Version: 1.0
#>
$ErrorActionPreference = 'SilentlyContinue'
# Get the script start time
$scriptStartTime = Get-Date
# Certificate Store to pull from
$certificates = Get-ChildItem -Path Cert:\LocalMachine\My
# Load the Windows Forms assembly
Add-Type -AssemblyName System.Windows.Forms
# Create a new form
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Select Certificate'
$form.Size = New-Object System.Drawing.Size(600, 300)
# Create a list view to display certificates
$listView = New-Object System.Windows.Forms.ListView
$listView.Location = New-Object System.Drawing.Point(10, 10)
$listView.Size = New-Object System.Drawing.Size(570, 200)
$listView.View = [System.Windows.Forms.View]::Details
$listView.FullRowSelect = $true
# Add columns to the list view
$listView.Columns.Add("Friendly Name", 150)
$listView.Columns.Add("Subject", 150)
$listView.Columns.Add("Issuer", 150)
$listView.Columns.Add("Valid From", 100)
$listView.Columns.Add("Valid To", 100)
# Map for storing certificate objects
$certMap = @{}
# Add certificate details to the list view
$certificates | ForEach-Object {
$item = New-Object System.Windows.Forms.ListViewItem($_.FriendlyName)
$item.SubItems.Add($_.Subject)
$item.SubItems.Add($_.Issuer)
$item.SubItems.Add($_.NotBefore.ToString())
$item.SubItems.Add($_.NotAfter.ToString())
# Add the ListView item to the list view
$listView.Items.Add($item)
# Map the item to the certificate
$certMap[$item] = $_
}
# Add the list view to the form
$form.Controls.Add($listView)
# Initialize selectedCert variable
$selectedCert = $null
# Create a button to close the form
$button = New-Object System.Windows.Forms.Button
$button.Location = New-Object System.Drawing.Point(250, 215)
$button.Size = New-Object System.Drawing.Size(75, 23)
$button.Text = 'OK'
$button.Add_Click({
if ($listView.SelectedItems.Count -gt 0) {
$selectedItem = $listView.SelectedItems[0]
$global:SelectedCert = $certMap[$selectedItem]
#Write-Host "Debug: Selected Item is $selectedItem"
#Write-Host "Debug: Certificate is $global:SelectedCert"
}
$form.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.Close()
})
$form.Controls.Add($button)
# Show the form as a modal dialog
$form.ShowDialog()
# Ensure the form is disposed properly
$form.Dispose()
# Capture the selected certificate and display its properties
if ($global:SelectedCert) {
# Validate the selected certificate
$errors = @()
# Check Subject
$computerName = [System.Net.Dns]::GetHostName()
if ($global:SelectedCert.Subject -notmatch "CN=$computerName") {
$errors += "Subject must be 'CN=$computerName'."
}
# Check Key Usage
if (-not $global:SelectedCert.HasPrivateKey) {
$errors += "Private key is required."
}
if ($global:SelectedCert.SignatureAlgorithm.FriendlyName -ne "sha256RSA") {
$errors += "Hash algorithm must be SHA256."
}
if ($global:SelectedCert.PublicKey.Key.KeySize -lt 2048) {
$errors += "Key length must be at least 2048."
}
if (-not ($global:SelectedCert.Extensions | Where-Object { $_.Oid.FriendlyName -eq "Key Usage" } | ForEach-Object { $_.Format(0) } | Where-Object { $_ -match "Digital Signature, Key Encipherment" })) {
$errors += "Key usage must include Digital Signature and Key Encipherment."
}
# Check Enhanced Key Usage
$ekuOids = ($global:SelectedCert.Extensions | Where-Object { $_.Oid.FriendlyName -eq "Enhanced Key Usage"}).EnhancedKeyUsages.Value
if ($ekuOids -notcontains "1.3.6.1.5.5.7.3.1" -or $ekuOids -notcontains "1.3.6.1.5.5.7.3.2") {
$errors += "Enhanced Key Usage must include both`n Server Authentication (1.3.6.1.5.5.7.3.1)`n Client Authentication (1.3.6.1.5.5.7.3.2)."
}
# Check Compatibility Settings
if ($global:SelectedCert.Version -lt 3) {
$errors += "Certificate must be compatible with Windows Server 2003 or newer."
}
# Check Cryptography Settings
if ($global:SelectedCert.PublicKey.Oid.FriendlyName -ne "RSA") {
$errors += "Algorithm name must be RSA."
}
# Display errors if any
if ($errors.Count -gt 0) {
[System.Windows.Forms.MessageBox]::Show(
"The selected certificate does not meet the following requirements, the script will exit:`n`n" + ($errors -join "`n"),
"Certificate Validation Failed",
[System.Windows.Forms.MessageBoxButtons]::OK,
[System.Windows.Forms.MessageBoxIcon]::Error
)
exit
} else {
$confirmation = [System.Windows.Forms.MessageBox]::Show(
"The selected certificate appears to meet all requirements. Continue with selected cert?`n
Friendly Name: $($global:SelectedCert.FriendlyName)
Subject: $($global:SelectedCert.Subject)
Issuer: $($global:SelectedCert.Issuer)
Valid From: $($global:SelectedCert.NotBefore)
Valid To: $($global:SelectedCert.NotAfter)",
"Certificate Validation Passed",
[System.Windows.Forms.MessageBoxButtons]::YesNo,
[System.Windows.Forms.MessageBoxIcon]::Question
)
}
# If the user confirms, proceed with the certificate import
if ($confirmation -eq [System.Windows.Forms.DialogResult]::Yes) {
# Split the string into 2-character chunks
$chunks = $($global:SelectedCert.SerialNumber) -split '(.{2})' -ne ''
# Reverse the chunks
$reversedChunks = [System.Collections.ArrayList]::new()
foreach ($chunk in $chunks) {
[void]$reversedChunks.Add($chunk)
}
$reversedChunks.Reverse()
# Join the reversed chunks with a space
$reversedString = ($reversedChunks -join ' ')
# Convert to binary bytes
$byteArray = @()
foreach ($chunk in $reversedChunks) {
$byteArray += [Convert]::ToByte($chunk, 16)
}
# Display the selected certificate properties to the user
Write-Host @"
Selected Certificate:
Friendly Name: $($global:SelectedCert.FriendlyName)
Subject: $($global:SelectedCert.Subject)
Issuer: $($global:SelectedCert.Issuer)
Valid From: $($global:SelectedCert.NotBefore)
Valid To: $($global:SelectedCert.NotAfter)
Thumbprint (ChannelCertificateHash): $($global:SelectedCert.Thumbprint)
Serial Number: $($global:SelectedCert.SerialNumber)
Serial Number (Mirrored): $reversedSerialNumber
"@
# Set the registry path
$registryPath = "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft Operations Manager\3.0\Machine Settings"
# Set the registry values
New-ItemProperty -Path $registryPath -Name "ChannelCertificateSerialNumber" -Value ([Byte[]]$byteArray) -Type Binary -Force -Verbose
New-ItemProperty -Path $registryPath -Name "ChannelCertificateHash" -Value $global:SelectedCert.Thumbprint -Force -Verbose
# Restart the HealthService
$serviceName = "HealthService"
# Start the restart service command as a background job
$job = Start-Job -ScriptBlock {
Restart-Service -Name $using:serviceName -Force
}
# Function to show progress bar
function Show-ProgressBar {
param (
[int]$duration = 30,
[string]$text = 'Waiting for the "' + $($serviceName) + '" service to restart...'
)
$startTime = Get-Date
$endTime = $startTime.AddSeconds($duration)
# Monitor job and update progress bar
while ($true) {
$elapsedTime = (Get-Date) - $startTime
$percentComplete = [int]($elapsedTime.TotalSeconds / $duration * 100)
Write-Progress -Activity $text -Status "Restarting Service..." -PercentComplete $percentComplete
if ($job.State -in 'Completed', 'Failed', 'Stopped') {
Start-Sleep -Seconds 5
Write-Progress -Activity $text -Status "Restarting Service..." -Completed
break
}
if ($elapsedTime.TotalSeconds -ge $duration) {
Write-Progress -Activity $text -Status "Restarting Service..." -Completed
break
}
Start-Sleep -Seconds 1
}
}
# Show the progress bar while waiting for the job
Show-ProgressBar -duration 30 -text 'Waiting for HealthService to restart...'
# Clean up the job if it is still running
if ($job.State -ne 'Completed') {
Stop-Job -Job $job
Remove-Job -Job $job
}
If ((Get-Service HealthService).Status -ne 'Running') {
Write-Error "Failed to restart the HealthService. Please check the Operations Manager event log for more information."
exit
} else {
Write-Host "HealthService has been restarted. Checking the Operations Manager event log for certificate import status..."
}
# Check the Operations Manager event log for specific event IDs related to certificate import
$logName = "Operations Manager"
$EventIds = @(20049, 20050, 20052, 20053, 20066, 20069, 20077)
# Function to get the latest event log entry
function Get-LatestEventLogEntry {
param (
[string]$logName,
[int[]]$eventIds
)
$filter = @{
LogName = $logName
Id = $eventIds
StartTime = $scriptStartTime
}
Get-WinEvent -FilterHashtable $filter -MaxEvents 1 | Sort-Object TimeCreated -Descending
}
# Check for the latest success event
$latestEvent = Get-LatestEventLogEntry -logName $logName -eventIds $EventIds
# Switch on the event ID to display the appropriate message to the user
Switch ($latestEvent.ID) {
'' {
Write-Host "No events related to certificates found recently in the Operations Manager log, there may've been an issue during import. Please try again."
}
20053 {
Write-Host "Certificate import was successful.`n Event ID: $($latestEvent.ID)`n Message: $($latestEvent.Message)" -ForegroundColor Green
}
default {
Write-Error "There were issues with the certificate import:`n Event ID: $($latestEvent.ID)`n Message: $($latestEvent.Message)"
}
}
} ## End of if ($confirmation -eq [System.Windows.Forms.DialogResult]::Yes)
else {
Write-Warning "Certificate selection cancelled. Exiting..."
}
} ## End of if ($global:SelectedCert) from the cert picker popup
else {
Write-Warning "No certificate selected. Exiting..."
}