Set WMI property

Tagged: 

This topic contains 5 replies, has 4 voices, and was last updated by Profile photo of cyn3rgy cyn3rgy 3 months, 3 weeks ago.

  • Author
    Posts
  • #23529
    Profile photo of Nick Chard
    Nick Chard
    Participant

    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?

  • #23533
    Profile photo of Greg Altman
    Greg Altman
    Member

    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)
    
  • #23534
    Profile photo of H Man
    H Man
    Participant

    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'
    
  • #23583
    Profile photo of Nick Chard
    Nick Chard
    Participant

    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)

  • #23589
    Profile photo of Greg Altman
    Greg Altman
    Member

    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

  • #49983
    Profile photo of cyn3rgy
    cyn3rgy
    Participant

    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

You must be logged in to reply to this topic.