Skip to content

Commit 3723c1b

Browse files
committed
Updates SetupWinRMAccessSelfSigned.ps1
The script is now able to perfoprm the entire process, starting with downloading OpenSSL and generating the certificate.
1 parent 7006a86 commit 3723c1b

File tree

1 file changed

+96
-17
lines changed

1 file changed

+96
-17
lines changed

SetupWinRMAccessSelfSigned.ps1

+96-17
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,101 @@
1-
$cert_pfx = "cert_self_signed.pfx"
2-
$cert_pfx_password = "Passw0rd"
1+
$ErrorActionPreference = "Stop"
32

4-
# Get the machine personal certificate store
5-
$store = New-Object System.Security.Cryptography.X509Certificates.X509Store(
6-
[System.Security.Cryptography.X509Certificates.StoreName]::My,
7-
[System.Security.Cryptography.X509Certificates.StoreLocation]::LocalMachine)
8-
$store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
3+
$opensslPath = "$ENV:HOMEDRIVE\OpenSSL-Win32"
94

10-
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2(
11-
$cert_pfx, $cert_pfx_password,
12-
([System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::MachineKeySet -bor
13-
[System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::PersistKeySet))
14-
$store.Add($cert)
5+
function VerifyHash($filename, $expectedHash) {
6+
$hash = (Get-FileHash -Algorithm SHA1 $filename).Hash
7+
if ($hash -ne $expectedHash) {
8+
throw "SHA1 hash not valid for file: $filename"
9+
}
10+
}
1511

16-
new-item -path wsman:\localhost\listener -transport https -address * -CertificateThumbPrint $cert.Thumbprint -Force
12+
function InstallOpenSSL() {
13+
if (!(Test-Path $opensslPath)) {
14+
$filename = "Win32OpenSSL_Light-1_0_1f.exe"
15+
Invoke-WebRequest -Uri "http://slproweb.com/download/$filename" -OutFile $filename
1716

18-
& winrm set winrm/config/service/auth `@`{Basic=`"true`"`}
19-
if ($LastExitCode) { throw "Failed to setup WinRM basic auth" }
17+
VerifyHash $filename "B6AD4E63B91A469CAF430CE9CB7FC89FDDAF8D05"
2018

21-
& netsh advfirewall firewall add rule name="WinRM HTTPS" dir=in action=allow protocol=TCP localport=5986
22-
if ($LastExitCode) { throw "Failed to setup WinRM HTTPS firewall rules" }
19+
Start-Process -Wait -FilePath $filename -ArgumentList "/silent /verysilent /sp- /suppressmsgboxes"
20+
del $filename
21+
}
22+
}
23+
24+
function GenerateSelfSignedCertificate($certFilePfx, $pfxPassword) {
25+
$opensslConf = "openssl_server_auth.cnf"
26+
27+
Set-Content $opensslConf @"
28+
distinguished_name = req_distinguished_name
29+
[req_distinguished_name]
30+
[v3_req]
31+
[v3_req_server]
32+
extendedKeyUsage = serverAuth
33+
[v3_ca]
34+
"@
35+
36+
$certFilePem = "server_cert.pem"
37+
$keyFilePem = "server_cert.key"
38+
39+
$openssl = "$opensslPath\bin\openssl.exe"
40+
$subject = "/C=RO/ST=Timis/L=Timisoara/[email protected]/organizationName=Cloudbase/CN=$ENV:COMPUTERNAME"
41+
42+
$ENV:OPENSSL_CONF = $opensslConf
43+
& $openssl req -x509 -nodes -days 3650 -newkey rsa:2048 -out $certFilePem -outform PEM -keyout $keyFilePem -subj $subject -extensions v3_req_server
44+
if ($LastExitCode) { throw "OpenSSL failed to create the self signed server certificate" }
45+
46+
& $openssl pkcs12 -export -in $certFilePem -inkey $keyFilePem -out $certFilePfx -password pass:$pfxPassword
47+
if ($LastExitCode) { throw "OpenSSL failed to export P12 certificate" }
48+
49+
del $opensslConf
50+
$ENV:OPENSSL_CONF = ""
51+
52+
del $certFilePem
53+
del $keyFilePem
54+
}
55+
56+
function ImportCertificate($certFilePfx, $pfxPassword) {
57+
# Get the machine personal certificate store
58+
$store = New-Object System.Security.Cryptography.X509Certificates.X509Store(
59+
[System.Security.Cryptography.X509Certificates.StoreName]::My,
60+
[System.Security.Cryptography.X509Certificates.StoreLocation]::LocalMachine)
61+
$store.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
62+
63+
$cert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2(
64+
"$(pwd)\$certFilePfx", $pfxPassword,
65+
([System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::MachineKeySet -bor
66+
[System.Security.Cryptography.X509Certificates.X509KeyStorageFlags]::PersistKeySet))
67+
$store.Add($cert)
68+
69+
return $cert.Thumbprint
70+
}
71+
72+
function RemoveExistingWinRMHttpsListener() {
73+
$httpsListener = Get-Item -Path wsman:\localhost\listener\* | where {$_.Keys.Contains("Transport=HTTPS")}
74+
if ($httpsListener) {
75+
Remove-Item -Recurse -Force -Path ("wsman:\localhost\listener\" + $httpsListener.Name)
76+
}
77+
}
78+
79+
function CreateWinRMHttpsFirewallRule() {
80+
& netsh advfirewall firewall add rule name="WinRM HTTPS" dir=in action=allow protocol=TCP localport=5986
81+
if ($LastExitCode) { throw "Failed to setup WinRM HTTPS firewall rules" }
82+
}
83+
84+
$certFilePfx = "server_cert.p12"
85+
$pfxPassword = "Passw0rd"
86+
87+
InstallOpenSSL
88+
89+
GenerateSelfSignedCertificate $certFilePfx $pfxPassword
90+
91+
$certThumbprint = ImportCertificate $certFilePfx $pfxPassword
92+
93+
del $certFilePfx
94+
95+
RemoveExistingWinRMHttpsListener
96+
97+
New-Item -Path wsman:\localhost\listener -transport https -address * -CertificateThumbPrint $certThumbprint -Force
98+
99+
Set-Item wsman:\localhost\service\Auth\Basic -Value $true
100+
101+
CreateWinRMHttpsFirewallRule

0 commit comments

Comments
 (0)