Posted On July 1, 2020

PowerShell: Enable Remote Desktop

kimconnect 0 comments
blog.KimConnect.com >> Codes >> PowerShell: Enable Remote Desktop
$computernames=@'
SERVER1
SERVER2
'@

$computers=@($computernames -split "`n" -replace "\..*$")

function enableRemoteDesktop{
    $regHiveTs='HKLM:\System\CurrentControlSet\Control\Terminal Server'
    $regKeyTs='fDenyTSConnections'
    $enable=0
    $currentValue=(get-itemproperty $regHiveTs).$regKeyTs
    if($currentValue -ne $enable){
        Set-ItemProperty -Path $regHiveTs -name 'fDenyTSConnections' $regKeyTs -value $enable
        Disable-NetFirewallRule -DisplayGroup 'Remote Desktop'
        $newValue=(get-itemproperty $regHiveTs).$regKeyTs
        write-host "$env:computername`: $regHiveTs key $regKeyTs has been set to $enable"
    }else{
        write-host "$env:computername`: $regHiveTs key $regKeyTs is already set to $enable"
    }
}
 
function disableRemoteDesktop{
    $regHiveTs='HKLM:\System\CurrentControlSet\Control\Terminal Server'
    $regKeyTs='fDenyTSConnections'
    $disable=1
    $currentValue=(get-itemproperty $regHiveTs).$regKeyTs
    if($currentValue -ne $disable){
        Set-ItemProperty -Path $regHiveTs -name 'fDenyTSConnections' $regKeyTs -value $disable
        Disable-NetFirewallRule -DisplayGroup 'Remote Desktop'
        $newValue=(get-itemproperty $regHiveTs).$regKeyTs
        write-host "$env:computername`: $regHiveTs key $regKeyTs has been set to $disable"
    }else{
        write-host "$env:computername`: $regHiveTs key $regKeyTs is already set to $disable"
    }    
}
 
invoke-command -computername $computers -ScriptBlock{
    param($enableRemoteDesktop)
    [ScriptBlock]::Create($enableRemoteDesktop).invoke()
} -Args ${function:enableRemoteDesktop}

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell: How to Quickly Ping a Target

Method 1: Legacy ping command # Legacy method that works without any fusses $pingResult=ping google.com…

PowerShell: Get Windows OS Name

function getOsBuild($computername=$env:computername){ $build="" $pingable=test-connection $computername -Count 1 -Quiet if($pingable){ $psinfoIsAvailable=get-command psinfo -ea SilentlyContinue # Requires…

PowerShell: WinSCP Module

$username='bongo' $port=20202 $remoteHost='dev-sftp.kimconnect.net' $privateKey='C:\scripts\keys\testkey.ppk' $remoteRoot='/' $downloadFolder='D:\downloads' $remoteDirectory='/var/www/sftp.kimconnect.net' $currentDirectory=$remoteDirectory $localFolder=(New-Object -ComObject Shell.Application).NameSpace('shell:Downloads').Self.Path $appName='winscp' function includePowerShellWrapper($appName){ #…
Other project: DragonCoin.com