Checking and enabling Remote Desktop with PowerShell – Complete script to Automate

Automate things with PowerShell is great – Today we will see checking and enabling Remote desktop with PowerShell. This is correct need is the root of invention, whenever I get some work I always look to create some script which can save my time.

This time I was working on Server Decommission Project and there are lots of tasks which I need to perform in large numbers of server.

I looked for a script which can help me and you, so first script which I am sharing that is for checking remote desktop is enabled on servers.

$computers = Get-Content "C:TEMPcomputerlist.txt"

foreach ($comp in $computers) {

$status = get-service -Name lanmanserver -ComputerName $comp

if( $status.status -eq "running") {

"$comp is up"

}

else

{ "$comp is down"

}

}

 

For this above script you would require a file computerlist.txt – which will contain the list of servers on which you want to check.  It’s working …

I have see some other commands which you can also use for same work – to check remote desktop from PowerShell remotely to other servers as well

Function Get-RemoteDesktopConfig

{if ((Get-ItemProperty -Path 'HKLM:SystemCurrentControlSetControlTerminal Server').fDenyTSConnections -eq 1)



{"Connections not allowed"}



elseif ((Get-ItemProperty -Path 'HKLM:SystemCurrentControlSetControlTerminal ServerWinStationsRDP-Tcp').UserAuthentication -eq 1)

{"Only Secure Connections allowed"}



else     {"All Connections allowed"}



}

 

Now lets look upon some scripts to enable the remote desktop with powershell

So in this below powershell script we are changing the registry value to enable and disable the Remote Desktop with PowerShell which we all are aware and you might have done that manually in the past…

Function Set-RemoteDesktopConfig



{Param ([switch]$LowSecurity, [switch]$disable)

if ($Disable) {

set-ItemProperty -Path 'HKLM:SystemCurrentControlSetControlTerminal Server'`

-name "fDenyTSConnections" -Value 1 -erroraction silentlycontinue

if (-not $?) {new-ItemProperty -Path 'HKLM:SystemCurrentControlSetControlTerminal Server' `

-name "fDenyTSConnections"  -Value 1 -PropertyType dword }

set-ItemProperty -Path 'HKLM:SystemCurrentControlSetControlTerminal ServerWinStationsRDP-Tcp' `

-name "UserAuthentication" -Value 1 -erroraction silentlycontinue

if (-not $?) {new-ItemProperty -Path 'HKLM:SystemCurrentControlSetControlTerminal ServerWinStationsRDP-Tcp'

-name "UserAuthentication" -Value 1 -PropertyType dword}

}

else {

set-ItemProperty -Path 'HKLM:SystemCurrentControlSetControlTerminal Server' `

-name "fDenyTSConnections" -Value 0 -erroraction silentlycontinue

if (-not $?) {new-ItemProperty -Path 'HKLM:SystemCurrentControlSetControlTerminal Server' `

-name "fDenyTSConnections" -Value 0 -PropertyType dword }

if ($LowSecurity) {

set-ItemProperty -Path 'HKLM:SystemCurrentControlSetControlTerminal ServerWinStationsRDP-Tcp'`

-name "UserAuthentication" -Value 0 -erroraction silentlycontinue

if (-not $?) {new-ItemProperty -Path 'HKLM:SystemCurrentControlSetControlTerminal ServerWinStationsRDP-Tcp'`

-name "UserAuthentication" -Value 0 -PropertyType dword}

}

}



}

I would also like to hear back from you and let me know did it worked for you? Let me know more queries which may help me to move and share more things for you all.

0 responses on "Checking and enabling Remote Desktop with PowerShell – Complete script to Automate"

Leave a Message

About AIK IT Support

Start Building Your Career with Solid Platform and its the time to Grab the Opportunity and Start Today

top
© AIK IT Support All rights reserved.