Disable-RDPRemotelyCIM.ps1


Description

Disable-RDPRemotelyCIM calls the Terminal Services CIM class on remote hosts and invokes SetAllowTSConnections to block Remote Desktop connections and clear the firewall exception. Provide one or more computer names (accepts pipeline input) and the function iterates over each system.

Usage examples

Disable-RDPRemotelyCIM -ComputerName 'srv-core01'
Get-Content .\servers.txt | Disable-RDPRemotelyCIM

Use the companion Enable-RDPRemotelyCIM function to re-enable connectivity when required.


Script

function Disable-RDPRemotelyCIM {
    # You can also disable it using the below method.
    [CmdletBinding()]
    param (
        [Parameter()]
        [string[]]
        $ComputerName
    )

    foreach ($Computer in $ComputerName) {
        $Win32TerminalServiceSettings = Get-CimInstance -Namespace root/cimv2/TerminalServices -ClassName Win32_TerminalServiceSetting -ComputerName $Computer
        $Win32TerminalServiceSettings | Invoke-CimMethod -MethodName SetAllowTSConnections -Arguments @{AllowTSConnections=0;ModifyFirewallException=0} -ComputerName $Computer
    }
}

Back to Top


Download

Please feel free to copy parts of the script or if you would like to download the entire script, simple click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.


Report Issues

You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.

Issue


Back to Top