-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvcsa_functions.ps1
155 lines (140 loc) · 4.76 KB
/
vcsa_functions.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
Class vSphere {
[vCenter]$vCenter
[System.Collections.ArrayList]$Hosts = @()
AddHost([string]$Name) {
$newHost = [Host]::new($Name)
$this.Hosts.Add($newHost)
}
AddvCenter([string]$SystemName,[string]$FQDN) {
$new = [vCenter]::new($SystemName,$FQDN)
$this.vCenter = $new
}
}
class vCenter {
[string]$FQDN
[string]$SystemName
[PSCredential]$SSOCredential
[PSCredential]$OSCredential
[SSL]$SSL
vCenter([string]$SystemName,[string]$FQDN){
$this.FQDN = $FQDN
$this.SystemName = $SystemName
$this.SSOCredential = Get-Credential -UserName "[email protected]" -Message "SSO"
$this.OSCredential = Get-Credential -UserName "root" -Message "OS"
}
AddSSLCertificate([string]$Certificate,[string]$Key,[string]$CA) {
$this.SSL = [SSL]::new($Certificate,$Key,$CA)
}
}
class Host {
[string]$Hostname
[PSCredential]$OSCredential
Host ([string]$Hostname){
$this.Hostname = $Hostname
$this.OSCredential = Get-Credential -UserName "root" -Message "OS"
}
}
class SSL {
[string]$Certificate
[string]$Key
[string]$CA
SSL([string]$Certificate,[string]$Key,[string]$CA){
$this.Certificate = $Certificate
$this.Key = $Key
$this.CA = $CA
}
}
function Set-SSLCertificate {
[CmdletBinding()]
param (
[Parameter(ValueFromPipeline)]
[vSphere]$vSphere,
[string]$CertPath,
[string]$KeyPath,
[string]$CAPath
)
if (test-path $CAPath ){
$CARegex = [regex] "CERTIFICATE-----"
if ( !(Get-Content $CAPath | Select-String -Pattern $CARegex)) {
Write-Warning "CA certificate does not contain any certificate key start/end tags"
exit
}
}
else {
Write-Warning "Unable to locate CA certificate"
exit
}
if (test-path $CertPath ){
$CertRegex = [regex] "CERTIFICATE-----"
if ( !(Get-Content $CertPath | Select-String -Pattern $CertRegex)) {
Write-Warning "certificate does not contain any certificate key start/end tags"
exit
}
}
else {
Write-Warning "Unable to locate certificate"
exit
}
if (test-path $CertPath ){
$KEYRegex = [regex] "PRIVATE\sKEY-----"
if ( !(Get-Content $KeyPath | Select-String -Pattern $KEYRegex)) {
Write-Warning "private key does not contain any private key start/end tags"
exit
}
}
else {
Write-Warning "Unable to locate private key"
exit
}
$Cert = (Get-Content $CertPath)
$Key = (Get-Content $KeyPath)
$CA = (Get-Content $CAPath)
foreach ($Line in $Cert) {
if ($line -notlike "-----END*")
{
$CertString += $line+"\n"
}
if ($line -like "-----END*")
{
$CertString += $line
}
}
foreach ($Line in $Key) {
if ($line -notlike "-----END*")
{
$KeyString += $line+"\n"
}
if ($line -like "-----END*")
{
$KeyString += $line
}
}
foreach ($Line in $CA) {
if ($line -notlike "-----END*")
{
$CAString += $line+"\n"
}
if ($line -like "-----END*")
{
$CAString += $line
}
}
$vSphere.vCenter
$vSphere.vCenter.AddSSLCertificate($CertString,$KeyString,$CAString)
}
function Copy-SSLCerts {
param (
[Parameter(ValueFromPipeline)]
[vSphere]$vSphere
)
$VM = Get-VM $vSphere.vCenter.SystemName
Invoke-VMScript -ScriptText "echo -e `'$($vSphere.vCenter.SSL.Certificate)`' > /tmp/cert.crt;echo 'Certificate saved to /tmp/cert.crt'" -VM $VM -GuestCredential $vSphere.vCenter.OSCredential -ScriptType Bash
Invoke-VMScript -ScriptText "echo -e `'$($vSphere.vCenter.SSL.Key)`' > /tmp/cert.key;echo 'Key saved to /tmp/cert.key'" -VM $VM -GuestCredential $vSphere.vCenter.OSCredential -ScriptType Bash
Invoke-VMScript -ScriptText "echo -e `'$($vSphere.vCenter.SSL.CA)`' > /tmp/ca.key;echo 'Key saved to /tmp/ca.key'" -VM $VM -GuestCredential $vSphere.vCenter.OSCredential -ScriptType Bash
}
$vSphere = [vSphere]::new()
$vSphere.AddvCenter("VMware vCenter Server Appliance","192.168.88.201")
$vSphere.AddHost("192.168.88.198")
connect-ViServer $vSphere.Hosts[0].Hostname -Credential $vSphere.Hosts[0].OSCredential
$vSphere | Set-SSLCertificate -CertPath .\Certificate\vcsa01.burger.local.crt -KeyPath .\Certificate\vcsa01.burger.local.pem -CAPath .\Certificate\CA_IS.crt
$vSphere | Copy-SSLCerts