Author Posts

March 20, 2015 at 8:30 am

Hello

What is the best way of setting a WMI property?

I am trying to enable remote desktop connections using PowerShell. So far I've used this command:

Get-WMIObject -class "Win32_TerminalServiceSetting" -Namespace root\cimv2\terminalservices

I can see AllowTSConnections = 0, and I want to change this to 1

I tried using Set-WMIInstance

Set-WMIInstance -class "Win32_TerminalServiceSetting" -Namespace root\cimv2\terminalservices -argument @{AllowTSConnections=1}

but this throws an error

format-default : The following exception occurred while retrieving members: "Operation is not valid due to the current state of the object."
    + CategoryInfo          : NotSpecified: [:] [format-default], ExtendedTypeSystemException
    + FullyQualifiedErrorId : CatchFromBaseGetMembers,Microsoft.PowerShell.Commands.FormatDefaultCommand

Is there another way of doing this? I can see the property AllowTSConnections has options of {get;set;} – can I use these to set the value?

March 20, 2015 at 9:32 am

Hi Nick,
This is what I use to enable remote desktop

(Get-WmiObject -Namespace "root\cimv2\TerminalServices" -Class Win32_TerminalServiceSetting -ComputerName XXXX).setallowtsconnections(1)

Or to be a bit more legible you could use

$computer = Get-WmiObject -Namespace "root\cimv2\TerminalServices" -Class Win32_TerminalServiceSetting -ComputerName XXXX
$computer.setallowtsconnections(1)

March 20, 2015 at 9:38 am

Hey Nick

Try this

Function Enable-RemoteDesktop {


[CmdletBinding()]
param(
[parameter(Mandatory=$true)]
[string]$computer = $env:COMPUTERNAME
)

$RDP = Get-WmiObject -Class Win32_TerminalServiceSetting `
            -Namespace root\CIMV2\TerminalServices `
            -Computer $Computer `
            -Authentication 6 `
            -ErrorAction Stop
$result = $RDP.SetAllowTsConnections(1,1)
   if($result.ReturnValue -eq 0) {
   Write-Host "$Computer : Enabled RDP Successfully"
  
 } else {
   Write-Host "$Computer : Failed to enable RDP"
 
} }

Enable-RemoteDesktop -computer 'pc01'

March 23, 2015 at 1:18 am

Hi, and thanks both for your input – that's exactly what I needed. I have one question though.

Both use SetAllowTSConnections() – why does one include one number and the other use two eg. SetAllowTSConnections(1) vs SetAllowTSConnections(1,1)

March 23, 2015 at 7:15 am

Hi Nick,
That's a good question and put me into research mode! I couldn't remember what the Parms were on that so I found this: https://msdn.microsoft.com/en-us/library/aa383644%28v=vs.85%29.aspx?f=255&MSPPError=-2147217396

In a nutsell the second number is to set the "ModifyFirewallException" parameter. I don't use it since I have another little patch o logic that handles firewall rules for our desktops/laptops

August 11, 2016 at 8:43 am

I'd just like to update for others that find this post.. I had the same issue with set-wmiinstance and I had success if I piped the gwmi output through to the swmi command. eg:

Get-WMIObject -class "Win32_TerminalServiceSetting" -Namespace root\cimv2\terminalservices | Set-WMIObject -Arguments @{AllowTSConnections=1}

hope it helps someone