Permalink
Browse files

Update

  • Loading branch information...
lazywinadmin committed Mar 10, 2015
1 parent 28489ef commit f0f11af520ce5c194f3080c0886ea45e1d4a6021
@@ -1,39 +1,109 @@
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

<#
.SYNOPSIS
A brief description of the Enable-RemoteDesktop function.
.DESCRIPTION
A detailed description of the Enable-RemoteDesktop function.
.PARAMETER ComputerName
A description of the ComputerName parameter.
.PARAMETER Credential
A description of the Credential parameter.
.PARAMETER CimSession
A description of the CimSession parameter.
.EXAMPLE
PS C:\> Enable-RemoteDesktop -ComputerName $value1 -Credential $value2
.NOTES
Francois-Xavier Cat
@lazywinadm
www.lazywinadmin.com
#>
[CmdletBinding()]
PARAM (
[Parameter(ParameterSetName = "Main")]
[Alias("CN", "__SERVER", "PSComputerName")]
[String[]]$ComputerName,

[Parameter(ParameterSetName = "Main")]
[Alias("RunAs")]
[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.
Typically called inside another function in the BEGIN Block
#>
PARAM ($Message)
Write-Output "[$(Get-Date -Format 'yyyy/MM/dd-HH:mm:ss:ff')][$((Get-Variable -Scope 1 -Name MyInvocation -ValueOnly).MyCommand.Name)] $Message"
}#Get-DefaultMessage
}
PROCESS
{
FOREACH ($Computer in $ComputerName)
{
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"
}

IF (-not $PSBoundParameters['CimSession'])
{
$Splatting.ComputerName = $Computer

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
}
IF ($PSBoundParameters['CimSession'])
{
$Splatting.CimSession = $CimSession
Write-Verbose -Message (Get-DefaultMessage -Message "$Computer - CIMSession - Enable Remote Desktop (and Modify Firewall Exception")
Get-CimInstance @Splatting | Invoke-CimMethod -MethodName SetAllowTSConnections -Arguments @{
AllowTSConnections = 1;
ModifyFirewallException = 1}
}
}
}
CATCH
{
Write-Warning -Message (Get-DefaultMessage -Message "$Computer - Something wrong happened")
Write-Warning -MEssage $Error[0].Exception.Message
}
FINALLY
{
$Splatting.Clear()
}
}#FOREACH

}#PROCESS
}#Function
@@ -22,20 +22,43 @@
.NOTES
Additional information about the function.
#>
[CmdletBinding()]
PARAM (
[Parameter(ParameterSetName = "Main")]
[Alias("CN","__SERVER","PSComputerName")]
[String[]]$ComputerName = $env:ComputerName,

[Parameter(ParameterSetName="Main")]
[Alias("RunAs")]
[System.Management.Automation.Credential()]
$Credential = [System.Management.Automation.PSCredential]::Empty,

[Parameter(ParameterSetName = "CimSession")]
[Microsoft.Management.Infrastructure.CimSession]$CimSession
)
BEGIN
{
# Default Verbose/Debug message
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.
Typically called inside another function in the BEGIN Block
#>
PARAM ($Message)
Write-Output "[$(Get-Date -Format 'yyyy/MM/dd-HH:mm:ss:ff')][$((Get-Variable -Scope 1 -Name MyInvocation -ValueOnly).MyCommand.Name)] $Message"
}#Get-DefaultMessage
}
PROCESS
{
FOREACH ($Computer in $ComputerName)
{
TRY
{
Write-Verbose -Message (Get-DefaultMessage -Message $Computer)
IF (Test-Connection -ComputerName $Computer -Count 1 -Quiet)
{
# Define Hashtable to hold our properties
@@ -46,32 +69,48 @@

IF ($PSBoundParameters['CimSession'])
{
Write-Verbose -Message (Get-DefaultMessage -Message "$Computer - CimSession")
# Using cim session already opened
$Query = Get-CIMInstance @Splatting -CimSession $CimSession
}
ELSE
{
# Credential specified
IF ($PSBoundParameters['Credential']) { $Splatting.Credential = $Credential }
IF ($PSBoundParameters['Credential'])
{
Write-Warning -Message (Get-DefaultMessage -Message "$Computer - Credential specified $($Credential.username)")
$Splatting.Credential = $Credential
}

# Set the ComputerName into the splatting
$Splatting.ComputerName = $ComputerName

Write-Verbose -Message (Get-DefaultMessage -Message "$Computer - Get-WmiObject")
$Query = Get-WmiObject @Splatting
}

# Prepare output
$Properties = @{
ComputerName = $Computer
OperatingSystem = $Query.Caption
}

# Output
New-Object -TypeName PSObject -Property $Properties
}
}
CATCH
{

Write-Warning -Message (Get-DefaultMessage -Message "$Computer - Issue to connect")
Write-Verbose -Message $Error[0].Exception.Message
}#CATCH
FINALLY
{
$Splatting.Clear()
}
}
}#FOREACH
}#PROCESS
END
{
Write-Warning -Message (Get-DefaultMessage -Message "Script completed")
}
}
}#Function
@@ -52,14 +52,21 @@
{
IF (Test-Connection -ComputerName $Computer -Count 1 -Quiet)
{
IF ($PSBoundParameters['Credential'])
{

}
# Some Action here
}#IF Test-Connection
}
CATCH
{

}#CATCH

FINALLY
{

}#FINALLY
}#FOREACH
}#PROCESS
END

0 comments on commit f0f11af

Please sign in to comment.