-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPSNamecheap.ps1
228 lines (161 loc) · 7.97 KB
/
PSNamecheap.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
function Create-Cert {
param(
[Parameter(Mandatory=$true)]
[string]$User,
[Parameter(Mandatory=$true)]
[string]$APIkey,
[Parameter(Mandatory=$true)]
[string]$ClientIP,
[Parameter(Mandatory=$true)]
[string]$Years,
[ValidateSet( 'PositiveSSL', 'EssentialSSL', 'InstantSSL', 'InstantSSL Pro', 'PremiumSSL', 'EV SSL', 'PositiveSSL Wildcard', 'EssentialSSL Wildcard', 'PremiumSSL Wildcard', 'PositiveSSL Multi Domain', 'Multi Domain SSL', 'Unified Communications', 'EV Multi Domain SSL')]
$SSLType,
[Parameter(Mandatory=$true)]
[ValidateSet('api.sandbox','api')]
$APIEnvironment
)
try {
$URI = "https://$APIEnvironment.namecheap.com/xml.response?ApiUser=$User&APIKey=$APIkey&UserName=$User&ClientIp=$ClientIP&Command=namecheap.ssl.create&Years=$Years&Type=$SSLType"
[xml]$call = (Invoke-WebRequest -Method GET -uri $URI ).content
if($call.ApiResponse.Status -eq 'OK'){
$New_SSL = New-Object -TypeName psobject
$New_SSL | Add-Member -MemberType NoteProperty -Name Status -Value $call.ApiResponse.Status
$New_SSL | Add-Member -MemberType NoteProperty -Name SSLtype -Value $SSLType
$New_SSL | Add-Member -MemberType NoteProperty -Name CertificateID -Value $call.ApiResponse.CommandResponse.SSLCreateResult.SSLCertificate.CertificateID
return $New_SSL
}
else {
write-host -ForegroundColor Red "ERROR:" $call.ApiResponse.Errors.Error.'#text'
}
}
catch {
$ErrorMessage = $_.Exception.Message
write-host -ForegroundColor Red "ERROR:" $ErrorMessage
}
}
function Activate-Cert {
param(
[Parameter(Mandatory=$true)]
[string]$User,
[Parameter(Mandatory=$true)]
[string]$APIkey,
[Parameter(Mandatory=$true)]
[string]$ClientIP,
[Parameter(Mandatory=$true)]
[string]$CertificateID,
[Parameter(Mandatory=$true)]
$CSR,
[Parameter(Mandatory=$true)]
[string]$AdminEmail,
[Parameter(Mandatory=$true)]
[ValidateSet('HTTPDCValidation','DNSDCValidation')]
[string]$Validation,
[Parameter(Mandatory=$true)]
[ValidateSet('api.sandbox','api')]
$APIEnvironment,
[Parameter(Mandatory=$false)]
$Additional = ''
)
try {
Add-Type -AssemblyName System.Web
$CSR_ENCODED = [System.Web.HttpUtility]::UrlEncode($CSR)
$AdminEmail_ENCODED = [System.Web.HttpUtility]::UrlEncode($AdminEmail)
$URI = "https://$APIEnvironment.namecheap.com/xml.response?ApiUser=$User&APIKey=$APIkey&UserName=$User&ClientIp=$ClientIP&Command=namecheap.ssl.activate&CertificateID=$CertificateID&csr=$CSR_ENCODED&AdminEmailAddress=$AdminEmail_ENCODED&$Validation=True$Additional"
[xml]$call = (Invoke-WebRequest -Method GET -uri $URI ).content
if($call.ApiResponse.Status -eq 'OK'){
$Active_SSL = New-Object -TypeName psobject
$Active_SSL | Add-Member -MemberType NoteProperty -Name Status -Value $call.ApiResponse.Status
if ($Validation = 'DNSDCValidation'){
$Active_SSL | Add-Member -MemberType NoteProperty -Name HostName -Value $call.ApiResponse.CommandResponse.SSLActivateResult.DNSDCValidation.dns.HostName.'#cdata-section'
$Active_SSL | Add-Member -MemberType NoteProperty -Name Target -Value $call.ApiResponse.CommandResponse.SSLActivateResult.DNSDCValidation.dns.Target.'#cdata-section'
}
else {
$Active_SSL | Add-Member -MemberType NoteProperty -Name FileName -Value $call.ApiResponse.CommandResponse.SSLActivateResult.HttpDCValidation.dns.FileName.'#cdata-section'
$Active_SSL | Add-Member -MemberType NoteProperty -Name FileContent -Value $call.ApiResponse.CommandResponse.SSLActivateResult.HttpDCValidation.dns.FileContent.'#cdata-section'
}
return $Active_SSL
}
else {
write-host -ForegroundColor Red "ERROR:" $call.ApiResponse.Errors.Error.'#text'
}
}
catch {
$ErrorMessage = $_.Exception.Message
write-host -ForegroundColor Red "ERROR:" $ErrorMessage
}
}
function Get-Cert {
param(
[Parameter(Mandatory=$true)]
[string]$User,
[Parameter(Mandatory=$true)]
[string]$APIkey,
[Parameter(Mandatory=$true)]
[string]$ClientIP,
[Parameter(Mandatory=$true)]
[string]$CertificateID,
[Parameter(Mandatory=$true)]
[ValidateSet('api.sandbox','api')]
$APIEnvironment,
[Parameter(Mandatory=$false)]
[ValidateSet('Individual','PKCS7')]
$CertType = 'Individual'
)
try {
$URI = "https://$APIEnvironment.namecheap.com/xml.response?ApiUser=$User&APIKey=$APIkey&UserName=$User&ClientIp=$ClientIP&Command=namecheap.ssl.getinfo&certificateID=$CertificateID&returncertificate=true&returntype=$CertType"
[xml]$call = (Invoke-WebRequest -Method GET -uri $URI ).content
if($call.ApiResponse.Status -eq 'OK'){
$Get_SSL = New-Object -TypeName psobject
$Get_SSL | Add-Member -MemberType NoteProperty -Name Status -Value $call.ApiResponse.CommandResponse.SSLGetInfoResult.Status
$Get_SSL | Add-Member -MemberType NoteProperty -Name Expires -Value $call.ApiResponse.CommandResponse.SSLGetInfoResult.Expires
$Get_SSL | Add-Member -MemberType NoteProperty -Name CRT -Value $call.ApiResponse.CommandResponse.SSLGetInfoResult.CertificateDetails.Certificates.Certificate.'#cdata-section'
$Get_SSL | Add-Member -MemberType NoteProperty -Name CSR -Value $call.ApiResponse.CommandResponse.SSLGetInfoResult.CertificateDetails.CSR.'#cdata-section'
$Get_SSL | Add-Member -MemberType NoteProperty -Name Type -Value $call.ApiResponse.CommandResponse.SSLGetInfoResult.Type
return $Get_SSL
}
else {
write-host -ForegroundColor Red "ERROR:" $call.ApiResponse.Errors.Error.'#text'
}
}
catch {
$ErrorMessage = $_.Exception.Message
write-host -ForegroundColor Red "ERROR:" $ErrorMessage
}
}
function Get-CertList {
param(
[Parameter(Mandatory=$true)]
[string]$User,
[Parameter(Mandatory=$true)]
[string]$APIkey,
[Parameter(Mandatory=$true)]
[string]$ClientIP,
[Parameter(Mandatory=$true)]
[ValidateSet('api.sandbox','api')]
$APIEnvironment
)
try {
$URI = "https://$APIEnvironment.namecheap.com/xml.response?ApiUser=$User&APIKey=$APIkey&UserName=$User&ClientIp=$ClientIP&Command=namecheap.ssl.getList&pagesize=100&ListType=active"
[xml]$call = (Invoke-WebRequest -Method GET -uri $URI ).content
if($call.ApiResponse.Status -eq 'OK'){
$SSL_LIST = @()
foreach( $ssl in $call.ApiResponse.CommandResponse.SSLListResult.SSL){
$Get_SSL_LIST = New-Object -TypeName psobject
$Get_SSL_LIST | Add-Member -MemberType NoteProperty -Name HostName -Value $ssl.Hostname
$Get_SSL_LIST | Add-Member -MemberType NoteProperty -Name CertificateID -Value $ssl.CertificateID
$Get_SSL_LIST | Add-Member -MemberType NoteProperty -Name PurchaseDate -Value $ssl.PurchaseDate
$Get_SSL_LIST | Add-Member -MemberType NoteProperty -Name ExpireDate -Value $ssl.ExpireDate
$Get_SSL_LIST | Add-Member -MemberType NoteProperty -Name Status -Value $ssl.Status
$SSL_LIST += $Get_SSL_LIST
}
return $call
}
else {
write-host -ForegroundColor Red "ERROR:" $call.ApiResponse.Errors.Error.'#text'
}
}
catch {
$ErrorMessage = $_.Exception.Message
write-host -ForegroundColor Red "ERROR:" $ErrorMessage
}
}