-
Notifications
You must be signed in to change notification settings - Fork 726
/
Copy pathEnable-RemoteDesktop.ps1
169 lines (148 loc) · 7.12 KB
/
Enable-RemoteDesktop.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
function Enable-RemoteDesktop {
<#
.SYNOPSIS
The function Enable-RemoteDesktop will enable RemoteDesktop on a local or remote machine.
.DESCRIPTION
The function Enable-RemoteDesktop will enable RemoteDesktop on a local or remote machine.
.PARAMETER ComputerName
Specifies the computername
.PARAMETER Credential
Specifies the credential to use
.PARAMETER CimSession
Specifies one or more existing CIM Session(s) to use
.EXAMPLE
PS C:\> Enable-RemoteDesktop -ComputerName DC01
.EXAMPLE
PS C:\> Enable-RemoteDesktop -ComputerName DC01 -Credential (Get-Credential -cred "FX\SuperAdmin")
.EXAMPLE
PS C:\> Enable-RemoteDesktop -CimSession $Session
.EXAMPLE
PS C:\> Enable-RemoteDesktop -CimSession $Session1,$session2,$session3
.NOTES
Francois-Xavier Cat
@lazywinadmin
lazywinadmin.com
github.com/lazywinadmin
.LINK
https://github.com/lazywinadmin/PowerShell
#>
#Requires -RunAsAdministrator
[CmdletBinding(DefaultParameterSetName = 'CimSession',
SupportsShouldProcess = $true)]
param
(
[Parameter(ParameterSetName = 'Main',
ValueFromPipeline = $true,
ValueFromPipelineByPropertyName = $true)]
[Alias('CN', '__SERVER', 'PSComputerName')]
[String[]]$ComputerName,
[Parameter(ParameterSetName = 'Main')]
[Alias('RunAs')]
[pscredential]
[System.Management.Automation.Credential()]
$Credential = [System.Management.Automation.PSCredential]::Empty,
[Parameter(ParameterSetName = 'CimSession')]
[Microsoft.Management.Infrastructure.CimSession[]]$CimSession
)
BEGIN {
# Helper Function
function Get-DefaultMessage {
<#
.SYNOPSIS
Helper Function to show default message used in VERBOSE/DEBUG/WARNING
.DESCRIPTION
Helper Function to show default message used in VERBOSE/DEBUG/WARNING
and... HOST in some case.
This is helpful to standardize the output messages
.PARAMETER Message
Specifies the message to show
.NOTES
Francois-Xavier Cat
lazywinadmin.com
@lazywinadmin
#>
PARAM ($Message)
$DateFormat = Get-Date -Format 'yyyy/MM/dd-HH:mm:ss:ff'
$FunctionName = (Get-Variable -Scope 1 -Name MyInvocation -ValueOnly).MyCommand.Name
Write-Output "[$DateFormat][$FunctionName] $Message"
} #Get-DefaultMessage
}
PROCESS {
IF ($PSBoundParameters['CimSession']) {
FOREACH ($Cim in $CimSession) {
$CIMComputer = $($Cim.ComputerName).ToUpper()
IF ($PSCmdlet.ShouldProcess($CIMComputer, "Enable Remote Desktop via Win32_TerminalServiceSetting")) {
TRY {
# Parameters for Get-CimInstance
$CIMSplatting = @{
Class = "Win32_TerminalServiceSetting"
NameSpace = "root\cimv2\terminalservices"
CimSession = $Cim
Authentication = 'PacketPrivacy'
ErrorAction = 'Stop'
ErrorVariable = "ErrorProcessGetCimInstance"
}
# Parameters for Invoke-CimMethod
$CIMInvokeSplatting = @{
MethodName = "SetAllowTSConnections"
Arguments = @{
AllowTSConnections = 1
ModifyFirewallException = 1
}
ErrorAction = 'Stop'
ErrorVariable = "ErrorProcessInvokeCim"
}
Write-Verbose -Message (Get-DefaultMessage -Message "$CIMComputer - CIMSession - Enable Remote Desktop (and Modify Firewall Exception")
Get-CimInstance @CIMSplatting | Invoke-CimMethod @CIMInvokeSplatting
}
CATCH {
Write-Warning -Message (Get-DefaultMessage -Message "$CIMComputer - CIMSession - Something wrong happened")
IF ($ErrorProcessGetCimInstance) { Write-Warning -Message (Get-DefaultMessage -Message "$CIMComputer - Issue with Get-CimInstance") }
IF ($ErrorProcessInvokeCim) { Write-Warning -Message (Get-DefaultMessage -Message "$CIMComputer - Issue with Invoke-CimMethod") }
Write-Warning -Message $Error[0].Exception.Message
} #CATCH
FINALLY {
$CIMSplatting.Clear()
$CIMInvokeSplatting.Clear()
} #FINALLY
} #$PSCmdlet.ShouldProcess
} #FOREACH ($Cim in $CimSessions)
} #IF ($PSBoundParameters['CimSession'])
ELSE {
FOREACH ($Computer in $ComputerName) {
$Computer = $Computer.ToUpper()
IF ($PSCmdlet.ShouldProcess($Computer, "Enable Remote Desktop via Win32_TerminalServiceSetting")) {
TRY {
Write-Verbose -Message (Get-DefaultMessage -Message "$Computer - Test-Connection")
IF (Test-Connection -Computer $Computer -count 1 -quiet) {
$Splatting = @{
Class = "Win32_TerminalServiceSetting"
NameSpace = "root\cimv2\terminalservices"
ComputerName = $Computer
Authentication = 'PacketPrivacy'
ErrorAction = 'Stop'
ErrorVariable = 'ErrorProcessGetWmi'
}
IF ($PSBoundParameters['Credential']) {
$Splatting.credential = $Credential
}
# Enable Remote Desktop
Write-Verbose -Message (Get-DefaultMessage -Message "$Computer - Get-WmiObject - Enable Remote Desktop")
(Get-WmiObject @Splatting).SetAllowTsConnections(1, 1) | Out-Null
# Disable requirement that user must be authenticated
#(Get-WmiObject -Class Win32_TSGeneralSetting @Splatting -Filter TerminalName='RDP-tcp').SetUserAuthenticationRequired(0) Out-Null
}
} #TRY
CATCH {
Write-Warning -Message (Get-DefaultMessage -Message "$Computer - Something wrong happened")
IF ($ErrorProcessGetWmi) { Write-Warning -Message (Get-DefaultMessage -Message "$Computer - Issue with Get-WmiObject") }
Write-Warning -MEssage $Error[0].Exception.Message
} #CATCH
FINALLY {
$Splatting.Clear()
} #FINALLY
} #$PSCmdlet.ShouldProcess
} #FOREACH
} #ELSE (Not CIM)
} #PROCESS
}