-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInstall-Cert.ps1
More file actions
43 lines (35 loc) · 1.71 KB
/
Copy pathInstall-Cert.ps1
File metadata and controls
43 lines (35 loc) · 1.71 KB
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
$ErrorActionPreference = 'Stop';
function Install-Cert {
[CmdletBinding()]
param
(
[Parameter(Position=0, Mandatory=$true, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
[ValidateScript({Test-Path -Path $_ -PathType Leaf})]
[string]$Path,
[Parameter(Position=1, Mandatory=$true, ValueFromPipeline=$True, ValueFromPipelineByPropertyName=$True)]
[ValidateNotNullOrEmpty()]
[string]$Password,
[Parameter(Position=2, Mandatory=$true)]
[ValidateNotNullOrEmpty()]
[string]$CertStore
)
Write-Verbose -Message ('Installing certificate from path: {0}' -f $Path);
try
{
# Create the certificate
$pfxcert = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Certificate2 -ErrorAction Stop;
$KeyStorageFlags = [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::Exportable -bxor [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::MachineKeySet -bxor [System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::PersistKeySet;
Write-Verbose ('Key storage flags is: {0}' -f $KeyStorageFlags);
$pfxcert.Import($Path, (ConvertTo-SecureString $Password -AsPlainText -Force), $KeyStorageFlags);
# Create the X509 store and import the certificate
$store = New-Object -TypeName System.Security.Cryptography.X509Certificates.X509Store -ArgumentList $CertStore, LocalMachine -ErrorAction Stop;
$store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite);
$store.Add($pfxcert);
$store.Close();
Write-Output -InputObject $pfxcert;
}
catch
{
throw $_;
}
}