Thursday, September 12, 2013

PowerShell to enable Remote Desktop for Administration on the local machine

I had a teammate request that I enable Remote Desktop for Administration as a portion of my SCVMM Service Template.

You cannot script sconfig – although that is a easy manual way to do it.

If you try any of the Server 2012 cmdlets you will end up mucking with Remote Desktop Services and enabling user access.

Well, it turns out the key is a key.  And it is easiest to tweak it with WMI.

The following script runs on the server that is being modified (localhost is the default).  And it can run using administrator or local system security credentials.

try {
    $RDP = Get-WmiObject -Class Win32_TerminalServiceSetting `
                        -Namespace root\CIMV2\TerminalServices
                        # -Computer $Computer `
                        # -Authentication 6 `
                        # -ErrorAction Stop
} catch {
    "WMIQueryFailed"
    continue
}
if($RDP.AllowTSConnections -eq 1) {
    "RDP Already Enabled"
    continue
} else {
    try {
        $result = $RDP.SetAllowTsConnections(1,1)
        if($result.ReturnValue -eq 0) { "Enabled RDP Successfully" }
        if ($result.ReturnValue -eq 4096) {
                $Job = [WMI]$Result.Job
                while ($Job.JobState -eq 4) {
                    Write-Progress -Id 2 -ParentId 1 $Job.Caption -Status "Executing" -PercentComplete $Job.PercentComplete
                    Start-Sleep 1
                    $Job.PSBase.Get()
                }
        }
    } catch {
        "Failed to enable RDP"
    }
}

3 comments:

Alok Kumar Sharma said...

Hi ,
How to change the name of single specific switch inside Virtual Machine ???

I have two Nic in a Virtual Machine as Nic1,Nic2 and both Nic are connected with different type of switch example (Private and Internal) Switches.

I want to change Switch according to given Mac Address ="00155DEB8011" change only.

Before
PS C:\Windows\system32> get-VMNetworkAdapter -VMName 'Blue2'

Name IsManagementOs VMName SwitchName MacAddress Status IPAddresses
---- -------------- ------ ---------- ---------- ------ -----------
Network Adapter False Blue2 HV4Test 00155DEB8011 {Ok} {169.254.116.7, fe80::904e:a07d:abcf:7407}
Network Adapter False Blue2 HV4Test 00155DEB8014 {Ok} {192.168.123.146, fe80::7d80:d69a:4fd6:8acc}


After Running my Command

PS C:\Windows\system32> Get-VMNetworkAdapter –VMName 'Blue2' | Connect-VMNetworkAdapter –SwitchName 'InternalNetworkSwitch'| Where{$_.MacAddress -eq '00155DEB8011'}

PS C:\Windows\system32> get-VMNetworkAdapter -VMName 'Blue2'

Name IsManagementOs VMName SwitchName MacAddress Status IPAddresses
---- -------------- ------ ---------- ---------- ------ -----------
Network Adapter False Blue2 InternalNetworkSwitch 00155DEB8011 {Ok} {169.254.116.7, fe80::904e:a07d:abcf:7407}
Network Adapter False Blue2 InternalNetworkSwitch 00155DEB8014 {Ok} {192.168.123.146, fe80::7d80:d69a:4fd6:8acc}


But I need the following result as

Name IsManagementOs VMName SwitchName MacAddress Status IPAddresses
---- -------------- ------ ---------- ---------- ------ -----------
Network Adapter False Blue2 InternalNetworkSwitch 00155DEB8011 {Ok} {169.254.116.7, fe80::904e:a07d:abcf:7407}
Network Adapter False Blue2 HV4Test 00155DEB8014 {Ok} {192.168.123.146, fe80::7d80:d69a:4fd6:8acc}





Please correct this command

PS C:\Windows\system32> Get-VMNetworkAdapter –VMName 'Blue2' | Connect-VMNetworkAdapter –SwitchName 'InternalNetworkSwitch'| Where{$_.MacAddress -eq '00155DEB8011'}

BrianEh said...

For one thing, you totally hijacked a blog article that has nothing to do with your question. Instead of commenting on the article that you copied the script from...

Inside the VM, there is no switch name. The switch the VM is connected to is at the hypervisor level.

And your command is in the wrong order (since you are pipelining)

I don't have a setup as I am rebuilding to R2 at the moment.

But try this:

Get-VMNetworkAdapter –VMName 'DATest' | where {$_.MacAddress -eq '00155D00080C'} | Connect-VMNetworkAdapter –SwitchName 'InternalNetworkSwitch'

My last comment is this: This is some pretty basic PowerShell that I just helped you with. I suggest taking some time to learn what the scripts are doing that you are copying from various places.

Bowman said...

Thanks for posting! This script to AllowTSConnections is exactly what I have been looking for