Skip to content

Instantly share code, notes, and snippets.

@maxlinc
Last active August 29, 2015 14:02
Show Gist options
  • Save maxlinc/4bf2c507b2aa8d0de044 to your computer and use it in GitHub Desktop.
Save maxlinc/4bf2c507b2aa8d0de044 to your computer and use it in GitHub Desktop.
Setup WinRM
Function GetRDPCert
{
$srcStoreScope = "LocalMachine"
$srcStoreName = "Remote Desktop"
$srcStore = New-Object System.Security.Cryptography.X509Certificates.X509Store $srcStoreName, $srcStoreScope
$srcStore.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadOnly)
Write-Host "Existing certificates:"
Write-Host $srcStore.Certificates | select -expand Subject
return $srcStore.Certificates -match "${env:computername}"
}
Function SetWinRMCert([System.Security.Cryptography.X509Certificates.X509Certificate]$cert)
{
Write-Host "Adding certificate to WinRM:"
Write-Host $cert
$dstStoreScope = "LocalMachine"
$dstStoreName = "My"
$dstStore = New-Object System.Security.Cryptography.X509Certificates.X509Store $dstStoreName, $dstStoreScope
$dstStore.Open([System.Security.Cryptography.X509Certificates.OpenFlags]::ReadWrite)
$dstStore.Add($cert)
}
Function SetupWinRM([String]$cert_thumbprint)
{
netsh advfirewall firewall set rule group="remote administration" new enable=yes
netsh advfirewall firewall add rule name="WinRM HTTP" dir=in action=allow protocol=TCP localport=5985
netsh advfirewall firewall add rule name="WinRM HTTPS" dir=in action=allow protocol=TCP localport=5986
winrm create winrm/config/Listener?Address=*+Transport=HTTPS "@{Hostname=`"${env:COMPUTERNAME}`"; CertificateThumbprint=`"${cert_thumbprint}`"}"
}
$cert = GetRDPCert
SetWinRMCert($cert)
SetupWinRM($cert.thumbprint)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment