-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAzureKeyVaultPowerShell-Part1.ps1
More file actions
79 lines (62 loc) · 2.61 KB
/
Copy pathAzureKeyVaultPowerShell-Part1.ps1
File metadata and controls
79 lines (62 loc) · 2.61 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
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
# Sample Shells
#PowerShell version
$PSVersionTable.PSVersion
#Azure Connect
Connect-AzAccount
# Parameters
$ResourceGroup = "azKeytestg123";
$location="eastus";
$keyVaultName = "aztestvault2021"; #Must be unique
#Create a Resource Group
New-AzResourceGroup -Name $ResourceGroup -Location $location
#Create a KeyVault
New-AzKeyVault -Name $keyVaultName -ResourceGroupName $ResourceGroup -Location $location
#Certificate Policy parameters
$SubjectName = "CN=MYTestCA.com"
$IssuerName = "Self"
$validity = 6
$KeySize = 2048
$RenewDays = 10
$DnsName = "MYTestCA.com"
$KeyType = "RSA"
$oid1= "1.3.6.1.5.5.7.3.1"
$oid1= "1.3.6.1.5.5.7.3.2"
$KeyUsage1 = "DigitalSignature"
$KeyUsage2 = "KeyCertSign"
# Setup the policy to create a certificate
$Policy = New-AzKeyVaultCertificatePolicy -SecretContentType "application/x-pkcs12" -SubjectName $SubjectName -IssuerName $IssuerName -ValidityInMonths $validity -ReuseKeyOnRenewal -KeySize $KeySize -RenewAtNumberOfDaysBeforeExpiry $RenewDays -CertificateTransparency 0 -DnsName $DnsName -KeyType $KeyType -Ekus $oid1,$oid1 -KeyUsage $KeyUsage1,$KeyUsage2
$nameofcert="MYCERT1"
# Creat a certificate
$certificateOps = Add-AzKeyVaultCertificate -VaultName $keyVaultName -Name $nameofcert -CertificatePolicy $Policy
#PrintCSR
$certificateOps.CertificateSigningRequest
#Create a file for CSR
$csrFile = "C:\cert\cert1.csr"
#Add content to csr format
Add-Content -Path $csrFile -Value "-----BEGIN CERTIFICATE REQUEST-----" -Encoding Ascii
Add-Content -Path $csrFile -Value $certRequest.CertificateSigningRequest -Encoding Ascii
Add-Content -Path $csrFile -Value "-----END CERTIFICATE REQUEST-----" -Encoding Ascii
########################
# copy the csr, Sign a CA using CSR.
# import to CA
########################
$crtFile = "C:\cert\cert1.cer"
$Name = "MYCERT1"
#Import certificate to the keyvault
Import-AzKeyVaultCertificate -VaultName $keyVaultName -Name $Name -FilePath $crtFile
#Certificate Status
Get-AzKeyVaultCertificate -VaultName $keyVaultName -Name $nameofcert
########################
# import a pfx certificate
########################
$pfxPassword = "123"
$Password = ConvertTo-SecureString -String $pfxPassword -AsPlainText -Force
$filePath = "C:\cert\MyCertificate.pfx"
$Name = "m365proguide"
Import-AzKeyVaultCertificate -VaultName $keyVaultName -Name $Name -FilePath $filePath -Password $Password
Get-AzKeyVaultCertificate -VaultName $keyVaultName -Name $Name
########################
# remove the resource group
########################
#Remove Azure Resource Group
Remove-AzResourceGroup -Name "$ResourceGroup"