Skip to content

Instantly share code, notes, and snippets.

@palefailmel
Last active August 29, 2015 14:08
Show Gist options
  • Save palefailmel/646a079f09c641679c5b to your computer and use it in GitHub Desktop.
Save palefailmel/646a079f09c641679c5b to your computer and use it in GitHub Desktop.
Enable PowerShell on a remote PC using WMI
#
# Author: M. Stevenson
# Desc : Enable RDP on a remote computer, given the hostname or IP
# Date : 2014-11-04 - M. Stevenson - Initial Version
#
Function Enable-RDP {
[CmdletBinding()]
param(
[parameter(Mandatory=$true,ValueFromPipeline=$true,HelpMessage="Enter the name/IP of the computer to enable RDP on")]
[string]$computer
)
PROCESS {
if (!(Test-Connection -ComputerName $computer -Count 1 -ea 0)) {
Write-Host "$computer : OFFLINE"
Continue
}
try {
$rdp = gwmi -Class Win32_TerminalServiceSetting `
-Namespace root\CIMV2\TerminalServices `
-ComputerName $computer `
-Authentication 6 `
-ErrorAction Stop
} catch {
Write-Host "$computer : WMIQueryFailed"
Continue
}
if ($rdp.AllowTSConnections -eq 1) {
Write-Host "$computer : RDP Already enabled"
continue
} else {
try {
$result = $rdp.SetAllowTSConnections(1,1)
if ($result.ReturnValue -eq 0) {
Write-Host "$computer : Enabled RDP Successfully"
} else {
Write-Host "$computer : Failed to enable RDP"
}
} catch {
Write-Host "$computer : Failed to enable RDP"
}
}
}
end {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment