Permalink
Browse files

Update RemoteDesktop Functions

  • Loading branch information...
lazywinadmin committed Mar 6, 2015
1 parent b4932cf commit 4a32256b78c0a0a01d8de1612fe7564f0e68ba05
@@ -0,0 +1,42 @@
+function Disable-RemoteDesktop
+{
+ <#
+ .SYNOPSIS
+ Function remotely disable RDP
+ .DESCRIPTION
+ Function remotely disable RDP
+ .NOTES
+ Francois-Xavier Cat
+ @lazywinadm
+ www.lazywinadmin.com
+
+ # MSDN/TechNet doc
+ https://msdn.microsoft.com/en-us/library/aa383644%28v=vs.85%29.aspx
+ #>
+
+PARAM(
+ [String[]]$ComputerName
+ )
+ FOREACH ($Computer in $ComputerName)
+ {
+ TRY{
+ IF (Test-Connection -Computer $Computer -count 1 -quiet)
+ {
+ $Splatting = @{
+ ComputerName = $Computer
+ NameSpace = rootcimv2TerminalServices
+ }
+ # Enable Remote Desktop
+ (Get-WmiObject -Class Win32_TerminalServiceSetting @Splatting).SetAllowTsConnections(0,0) | Out-Null
+
+ # Disable requirement that user must be authenticated
+ #(Get-WmiObject -Class Win32_TSGeneralSetting @Splatting -Filter TerminalName='RDP-tcp').SetUserAuthenticationRequired(0) Out-Null
+ }
+ }
+ CATCH{
+ Write-Warning -Message Something wrong happened
+ Write-Warning -MEssage $Error[0].Exception.Message
+ }
+ }#FOREACH
+
+}#Function
@@ -0,0 +1,39 @@
+function Enable-RemoteDesktop
+{
+ <#
+ .SYNOPSIS
+ Function remotely enable RDP
+ .DESCRIPTION
+ Function remotely enable RDP
+ .NOTES
+ Francois-Xavier Cat
+ @lazywinadm
+ www.lazywinadmin.com
+ #>
+
+PARAM(
+ [String[]]$ComputerName
+ )
+ FOREACH ($Computer in $ComputerName)
+ {
+ TRY{
+ IF (Test-Connection -Computer $Computer -count 1 -quiet)
+ {
+ $Splatting = @{
+ ComputerName = $Computer
+ NameSpace = rootcimv2TerminalServices
+ }
+ # Enable Remote Desktop
+ (Get-WmiObject -Class Win32_TerminalServiceSetting @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
+ }
+ }
+ CATCH{
+ Write-Warning -Message Something wrong happened
+ Write-Warning -MEssage $Error[0].Exception.Message
+ }
+ }#FOREACH
+
+}#Function
@@ -1,5 +1,3 @@
-#Requires -Version 3
-
function Get-NetworkLevelAuthentication
{
<#
@@ -165,143 +163,4 @@ function Get-NetworkLevelAuthentication
}
Write-Verbose -Message "END - Script is completed"
}
-}
-
-
-function Set-NetworkLevelAuthentication
-{
-<#
-.SYNOPSIS
- This function will set the NLA setting on a local machine or remote machine
-
-.DESCRIPTION
- This function will set the NLA setting on a local machine or remote machine
-
-.PARAMETER ComputerName
- Specify one or more computers
-
-.PARAMETER EnableNLA
- Specify if the NetworkLevelAuthentication need to be set to $true or $false
-
-.PARAMETER Credential
- Specify the alternative credential to use. By default it will use the current one.
-
-.EXAMPLE
- Set-NetworkLevelAuthentication -EnableNLA $true
-
- ReturnValue PSComputerName
- ----------- --------------
- XAVIERDESKTOP
-
-.NOTES
- DATE : 2014/04/01
- AUTHOR : Francois-Xavier Cat
- WWW : http://lazywinadmin.com
- Twitter : @lazywinadm
-
- Article : http://lazywinadmin.com/2014/04/powershell-getset-network-level.html
- GitHub : http://www.oddjack.com/?certs=lazywinadmin/PowerShell
-#>
- #Requires -Version 3.0
- [CmdletBinding()]
- PARAM (
- [Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName)]
- [String[]]$ComputerName = $env:ComputerName,
-
- [Parameter(Mandatory)]
- [Bool]$EnableNLA,
-
- [Alias("RunAs")]
- [System.Management.Automation.Credential()]
- $Credential = [System.Management.Automation.PSCredential]::Empty
- )#Param
- BEGIN
- {
- TRY
- {
- IF (-not (Get-Module -Name CimCmdlets))
- {
- Write-Verbose -Message 'BEGIN - Import Module CimCmdlets'
- Import-Module CimCmdlets -ErrorAction 'Stop' -ErrorVariable ErrorBeginCimCmdlets
-
- }
- }
- CATCH
- {
- IF ($ErrorBeginCimCmdlets)
- {
- Write-Error -Message "BEGIN - Can't find CimCmdlets Module"
- }
- }
- }#BEGIN
-
- PROCESS
- {
- FOREACH ($Computer in $ComputerName)
- {
- TRY
- {
- # Building Splatting for CIM Sessions
- $CIMSessionParams = @{
- ComputerName = $Computer
- ErrorAction = 'Stop'
- ErrorVariable = 'ProcessError'
- }
-
- # Add Credential if specified when calling the function
- IF ($PSBoundParameters['Credential'])
- {
- $CIMSessionParams.credential = $Credential
- }
-
- # Connectivity Test
- Write-Verbose -Message "PROCESS - $Computer - Testing Connection..."
- Test-Connection -ComputerName $Computer -count 1 -ErrorAction Stop -ErrorVariable ErrorTestConnection | Out-Null
-
- # CIM/WMI Connection
- # WsMAN
- IF ((Test-WSMan -ComputerName $Computer -ErrorAction SilentlyContinue).productversion -match 'Stack: 3.0')
- {
- Write-Verbose -Message "PROCESS - $Computer - WSMAN is responsive"
- $CimSession = New-CimSession @CIMSessionParams
- $CimProtocol = $CimSession.protocol
- Write-Verbose -message "PROCESS - $Computer - [$CimProtocol] CIM SESSION - Opened"
- }
-
- # DCOM
- ELSE
- {
- # Trying with DCOM protocol
- Write-Verbose -Message "PROCESS - $Computer - Trying to connect via DCOM protocol"
- $CIMSessionParams.SessionOption = New-CimSessionOption -Protocol Dcom
- $CimSession = New-CimSession @CIMSessionParams
- $CimProtocol = $CimSession.protocol
- Write-Verbose -message "PROCESS - $Computer - [$CimProtocol] CIM SESSION - Opened"
- }
-
- # Getting the Information on Terminal Settings
- Write-Verbose -message "PROCESS - $Computer - [$CimProtocol] CIM SESSION - Get the Terminal Services Information"
- $NLAinfo = Get-CimInstance -CimSession $CimSession -ClassName Win32_TSGeneralSetting -Namespace root\cimv2\terminalservices -Filter "TerminalName='RDP-tcp'"
- $NLAinfo | Invoke-CimMethod -MethodName SetUserAuthenticationRequired -Arguments @{ UserAuthenticationRequired = $EnableNLA } -ErrorAction 'Continue' -ErrorVariable ErrorProcessInvokeWmiMethod
- }
-
- CATCH
- {
- Write-Warning -Message "PROCESS - Error on $Computer"
- $_.Exception.Message
- if ($ErrorTestConnection) { Write-Warning -Message "PROCESS Error - $ErrorTestConnection" }
- if ($ProcessError) { Write-Warning -Message "PROCESS Error - $ProcessError" }
- if ($ErrorProcessInvokeWmiMethod) { Write-Warning -Message "PROCESS Error - $ErrorProcessInvokeWmiMethod" }
- }#CATCH
- } # FOREACH
- }#PROCESS
- END
- {
- if ($CimSession)
- {
- Write-Verbose -Message "END - Close CIM Session(s)"
- Remove-CimSession $CimSession
- }
- Write-Verbose -Message "END - Script is completed"
- }
-}
+}
@@ -0,0 +1,137 @@
+function Set-NetworkLevelAuthentication
+{
+<#
+.SYNOPSIS
+ This function will set the NLA setting on a local machine or remote machine
+
+.DESCRIPTION
+ This function will set the NLA setting on a local machine or remote machine
+
+.PARAMETER ComputerName
+ Specify one or more computers
+
+.PARAMETER EnableNLA
+ Specify if the NetworkLevelAuthentication need to be set to $true or $false
+
+.PARAMETER Credential
+ Specify the alternative credential to use. By default it will use the current one.
+
+.EXAMPLE
+ Set-NetworkLevelAuthentication -EnableNLA $true
+
+ ReturnValue PSComputerName
+ ----------- --------------
+ XAVIERDESKTOP
+
+.NOTES
+ DATE : 2014/04/01
+ AUTHOR : Francois-Xavier Cat
+ WWW : http://lazywinadmin.com
+ Twitter : @lazywinadm
+
+ Article : http://lazywinadmin.com/2014/04/powershell-getset-network-level.html
+ GitHub : http://www.oddjack.com/?certs=lazywinadmin/PowerShell
+#>
+ #Requires -Version 3.0
+ [CmdletBinding()]
+ PARAM (
+ [Parameter(ValueFromPipeline,ValueFromPipelineByPropertyName)]
+ [String[]]$ComputerName = $env:ComputerName,
+
+ [Parameter(Mandatory)]
+ [Bool]$EnableNLA,
+
+ [Alias("RunAs")]
+ [System.Management.Automation.Credential()]
+ $Credential = [System.Management.Automation.PSCredential]::Empty
+ )#Param
+ BEGIN
+ {
+ TRY
+ {
+ IF (-not (Get-Module -Name CimCmdlets))
+ {
+ Write-Verbose -Message 'BEGIN - Import Module CimCmdlets'
+ Import-Module CimCmdlets -ErrorAction 'Stop' -ErrorVariable ErrorBeginCimCmdlets
+
+ }
+ }
+ CATCH
+ {
+ IF ($ErrorBeginCimCmdlets)
+ {
+ Write-Error -Message "BEGIN - Can't find CimCmdlets Module"
+ }
+ }
+ }#BEGIN
+
+ PROCESS
+ {
+ FOREACH ($Computer in $ComputerName)
+ {
+ TRY
+ {
+ # Building Splatting for CIM Sessions
+ $CIMSessionParams = @{
+ ComputerName = $Computer
+ ErrorAction = 'Stop'
+ ErrorVariable = 'ProcessError'
+ }
+
+ # Add Credential if specified when calling the function
+ IF ($PSBoundParameters['Credential'])
+ {
+ $CIMSessionParams.credential = $Credential
+ }
+
+ # Connectivity Test
+ Write-Verbose -Message "PROCESS - $Computer - Testing Connection..."
+ Test-Connection -ComputerName $Computer -count 1 -ErrorAction Stop -ErrorVariable ErrorTestConnection | Out-Null
+
+ # CIM/WMI Connection
+ # WsMAN
+ IF ((Test-WSMan -ComputerName $Computer -ErrorAction SilentlyContinue).productversion -match 'Stack: 3.0')
+ {
+ Write-Verbose -Message "PROCESS - $Computer - WSMAN is responsive"
+ $CimSession = New-CimSession @CIMSessionParams
+ $CimProtocol = $CimSession.protocol
+ Write-Verbose -message "PROCESS - $Computer - [$CimProtocol] CIM SESSION - Opened"
+ }
+
+ # DCOM
+ ELSE
+ {
+ # Trying with DCOM protocol
+ Write-Verbose -Message "PROCESS - $Computer - Trying to connect via DCOM protocol"
+ $CIMSessionParams.SessionOption = New-CimSessionOption -Protocol Dcom
+ $CimSession = New-CimSession @CIMSessionParams
+ $CimProtocol = $CimSession.protocol
+ Write-Verbose -message "PROCESS - $Computer - [$CimProtocol] CIM SESSION - Opened"
+ }
+
+ # Getting the Information on Terminal Settings
+ Write-Verbose -message "PROCESS - $Computer - [$CimProtocol] CIM SESSION - Get the Terminal Services Information"
+ $NLAinfo = Get-CimInstance -CimSession $CimSession -ClassName Win32_TSGeneralSetting -Namespace root\cimv2\terminalservices -Filter "TerminalName='RDP-tcp'"
+ $NLAinfo | Invoke-CimMethod -MethodName SetUserAuthenticationRequired -Arguments @{ UserAuthenticationRequired = $EnableNLA } -ErrorAction 'Continue' -ErrorVariable ErrorProcessInvokeWmiMethod
+ }
+
+ CATCH
+ {
+ Write-Warning -Message "PROCESS - Error on $Computer"
+ $_.Exception.Message
+ if ($ErrorTestConnection) { Write-Warning -Message "PROCESS Error - $ErrorTestConnection" }
+ if ($ProcessError) { Write-Warning -Message "PROCESS Error - $ProcessError" }
+ if ($ErrorProcessInvokeWmiMethod) { Write-Warning -Message "PROCESS Error - $ErrorProcessInvokeWmiMethod" }
+ }#CATCH
+ } # FOREACH
+ }#PROCESS
+ END
+ {
+ if ($CimSession)
+ {
+ Write-Verbose -Message "END - Close CIM Session(s)"
+ Remove-CimSession $CimSession
+ }
+ Write-Verbose -Message "END - Script is completed"
+ }
+}

0 comments on commit 4a32256

Please sign in to comment.