Skip to content

Commit 6e9d630

Browse files
committed
Adds self signed WinRm certificate auth script
1 parent 004aef9 commit 6e9d630

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
$ErrorActionPreference = "Stop"
2+
3+
$username = "Administrator"
4+
$password = "Passw0rd"
5+
6+
$client_cert_path = "$(pwd)\cert.pem"
7+
8+
# Enable certificate authentication
9+
& winrm set winrm/config/service/auth `@`{Certificate=`"true`"`}
10+
11+
# Import the client cert as a CA cert
12+
$clientcert = New-Object System.Security.Cryptography.X509Certificates.X509Certificate2($client_cert_path)
13+
$castore = New-Object System.Security.Cryptography.X509Certificates.X509Store(
14+
[System.Security.Cryptography.X509Certificates.StoreName]::Root,
15+
[System.Security.Cryptography.X509Certificates.StoreLocation]::LocalMachine)
16+
$castore.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
17+
$castore.Add($clientcert)
18+
19+
$secure_password = ConvertTo-SecureString $password -AsPlainText -Force
20+
# For domain auth just replace $ENV:COMPUTERNAME with the domain name
21+
$cred = New-Object System.Management.Automation.PSCredential "$ENV:COMPUTERNAME\$username", $secure_password
22+
23+
# Get the UPN from the cert extension
24+
$clientcert.Extensions[1].Format($false) -match ".*=(.*)"
25+
$upn = $Matches[1]
26+
27+
New-Item -Path WSMan:\localhost\ClientCertificate -Issuer $clientcert.Thumbprint -Subject $upn -Uri * -Credential $cred -Force

0 commit comments

Comments
 (0)