Created
August 22, 2019 21:12
-
-
Save derektamsen/699248d323f0fa9ff06ddb6cbf1091e5 to your computer and use it in GitHub Desktop.
Update Remote Desktop Host Certificate Thumbprint. Used when applying an externally issued machine certificate for use with RDP.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param ($forceY = $false) | |
import-module pki | |
function promptContinue { | |
$input = Read-Host "Would you like to continue? (y/n)" | |
switch -regex ($input.ToLower()) { | |
'y(es)?' { return $true } | |
'n(o)?' { return $false } | |
default { | |
promptContinue | |
} | |
} | |
} | |
function getCertThumbprint { | |
$rds_certs = Get-ChildItem -Path 'Cert:\LocalMachine\My' | |
$signed_cert = $rds_certs | Where-Object ` | |
{($_.Subject -like '*issuer*') -and ($_.Subject -like "CN=${Hostname}*")} | |
return $signed_cert.Thumbprint | |
} | |
function getWMIPath { | |
$ts_wmi_path = (Get-WmiObject -class "Win32_TSGeneralSetting" ` | |
-Namespace root\cimv2\terminalservices ` | |
-Filter "TerminalName='RDP-tcp'").__path | |
return $ts_wmi_path | |
} | |
function setTSCertThumbprint { | |
param ( | |
$wmiPath, | |
$certThumbprint | |
) | |
Set-WmiInstance -Path $wmiPath -argument @{SSLCertificateSHA1Hash=$certThumbprint} | |
} | |
function restartSvc { | |
param ( | |
$svc | |
) | |
Write-Host "Restarting ${svc}..." | |
Restart-Service $svc | |
} | |
$certThumbprint = getCertThumbprint | |
$wmiPath = getWMIPath | |
Write-Host "Signed Cert Thumbprint: ${certThumbprint}" | |
Write-Host "Remote Desktop WMI Path: ${wmiPath}" | |
Write-Host "This will change the remote desktop certificate and restart RDP!" | |
if (-Not $forceY) { | |
$toContinue = promptContinue | |
if (-Not $toContinue) { | |
Write-Host 'Aborting set cert thumbprint!' | |
break | |
} | |
} | |
setTSCertThumbprint -wmiPath $wmiPath -certThumbprint $certThumbprint | |
restartSvc -svc 'SessionEnv' | |
Write-Host "Done" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment