-
Notifications
You must be signed in to change notification settings - Fork 1
/
Get-DBDirectorySize.ps1
61 lines (54 loc) · 1.97 KB
/
Get-DBDirectorySize.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
<#
.Synopsis
Retrieve newest requestID received on the target Certificate Authority
.DESCRIPTION
Requires PSPKI PowerShell module
Uses certutil -getreg config\DBDirectory to find database directory from the CA
Uses Get-ChildItem to calculate size of database .edb file
Returns integer object of database size in GB (rounded to 1 decimal places)
.EXAMPLE
Get-CA | Get-DBDirectorySize
.EXAMPLE
Get-CertificateAuthority "CA3" | Get-DBDirectorySize
#>
function Get-DBDirectorySize
{
[CmdletBinding()]
[Alias()]
[OutputType([int])]
param (
# requires PSPKI PowerShell module, creates a [PKI.CertificateServices.CertificateAuthority] object
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true)]
[PKI.CertificateServices.CertificateAuthority[]]$CertificateAuthority
)
begin {
}
process {
foreach ($CA in $CertificateAuthority)
{
try
{
# parse certutil output
$configString = $CA.ConfigString
$DBDirectory = certutil -config $configString -getreg config\DBDirectory
$DBDirectory = $DBDirectory | Where-Object { $_ -match "DBDirectory\ REG_SZ" }
$DBDirectory = ($DBDirectory -split "\= ")[1]
# convert local path to remote path
$localPath = $DBDirectory
$remotePath = "\\" + $CA.ComputerName + "\" + $($DBDirectory -replace "\:","$")
# get newest file
$DBFile = Get-ChildItem -Path "$remotePath" -Filter "*.edb" | Sort-Object $_.LastWriteTime -Descending | Select -First 1
$DBFileSize = $DBFile.Length
# output friendly size
[System.Math]::Round((($DBFileSize)/1GB),1)
}
catch
{
Write-Error $_
continue
}
}
}
end {
}
}