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: Export Exchange Mailboxes

Export 1 mailbox $userAlias="kungfu.panda"$storageUNC="\\FILESHERVER01\Backups\PSTs"# Import Exchange Management PowershellAdd-PSSnapin Microsoft.Exchange.Management.PowerShell.SnapIn;$mailboxRequest=(New-MailboxExportRequest -Mailbox $userAlias -FilePath "$storageUNC\$userAlias.pst").Mailboxfunction monitorCompletion($check){ $migrationStatus=(Get-MailboxExportRequest…

PowerShell: Create Mailbox Report on Office 365

# connect-to-exchange-online.ps1 # How to connect to Office 365 Cloud Services using PowerShell # Office…

PowerShell: Get IP’s From Computer Names

Resolve from Names to IPs: $names=@( 'TESTVM001', 'TESTVM002', 'TESTVM003' ) foreach($name in $names){ $ips =…