Shadrach

Powershell testing

Aug 13th, 2025
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # =====================================================================
  2. # Self-signed Client Auth certificate in Local Machine store, exported
  3. # as separate PEM files (PKCS#1 RSA format)
  4. # =====================================================================
  5.  
  6. # Output PEM file paths
  7. $CertPemPath = "$env:TEMP\clientcert.crt"
  8. $KeyPemPath  = "$env:TEMP\clientkey.key"
  9.  
  10. # Create self-signed client certificate in Local Machine\My
  11. # Requires running PowerShell as Administrator
  12. $cert = New-SelfSignedCertificate `
  13.     -Subject "CN=MyClientCert" `
  14.     -Type Custom `
  15.     -KeyAlgorithm RSA `
  16.     -KeyLength 2048 `
  17.     -KeyExportPolicy Exportable `
  18.     -KeySpec Signature `
  19.     -CertStoreLocation "Cert:\LocalMachine\My" `
  20.     -NotAfter (Get-Date).AddYears(1) `
  21.     -KeyUsage DigitalSignature `
  22.     -EnhancedKeyUsage "Client Authentication"
  23.  
  24. Write-Host "Certificate created in Local Machine\My store:"
  25. Write-Host "  Subject:  $($cert.Subject)"
  26. Write-Host "  Thumbprint: $($cert.Thumbprint)"
  27. Write-Host ""
  28.  
  29. # Export private key in PKCS#1 RSA format
  30. $rsa = $cert.GetRSAPrivateKey()
  31. $pkcs1Bytes = $rsa.ExportRSAPrivateKey()
  32. $privateKeyPem = "-----BEGIN RSA PRIVATE KEY-----`n" +
  33.     ([Convert]::ToBase64String($pkcs1Bytes) -split "(.{64})" | ? { $_ -ne "" }) -join "`n" +
  34.     "`n-----END RSA PRIVATE KEY-----"
  35.  
  36. # Export certificate in PEM format
  37. $certPem = "-----BEGIN CERTIFICATE-----`n" +
  38.     ([Convert]::ToBase64String($cert.RawData) -split "(.{64})" | ? { $_ -ne "" }) -join "`n" +
  39.     "`n-----END CERTIFICATE-----"
  40.  
  41. # Save PEM files
  42. Set-Content -Path $KeyPemPath -Value $privateKeyPem -Encoding ascii
  43. Set-Content -Path $CertPemPath -Value $certPem -Encoding ascii
  44.  
  45. Write-Host "PEM files exported:"
  46. Write-Host "  Certificate: $CertPemPath"
  47. Write-Host "  Private key: $KeyPemPath"
  48. Write-Host ""
  49. Write-Host "You can view the certificate in Cert Manager:"
  50. Write-Host "  Run: certlm.msc → Personal → Certificates"
  51.  
Add Comment
Please, Sign In to add comment