Unable to get Active Directory Terminal Services attributes while running PowerShell script in Visual Studio

Viewed 2193

I have run into a strange problem, and maybe someone can help me.

I am attempting to retrieve the Terminal Services attributes from an Active Directory user using C# on a machine using Windows 10. I am doing this by running a PowerShell script inside my application like so:

var script = $@"Import-module ActiveDirectory
                $user=[ADSI]""LDAP://192.111.222.33:389/CN=SomePerson,DC=Domain,DC=local""
                $user.psbase.Username = ""administrator""
                $user.psbase.Password = ""adminPassword""                 
                $user.psbase.invokeget(""TerminalServicesProfilePath"")";

using (var runspace = RunspaceFactory.CreateRunspace())
        {
            runspace.Open();
            using (var pipeline = runspace.CreatePipeline())
            {
                pipeline.Commands.AddScript(script);
                var test = pipeline.Invoke();
                Console.WriteLine("Success: ");
                return true;
            }
        }

I am getting this exception:

System.Management.Automation.MethodInvocationException: 'Exception calling "InvokeGet" with "1" argument(s): "Unknown name. (Exception from HRESULT: 0x80020006 (DISP_E_UNKNOWNNAME))"'

When I run the above code in Visual Studio 2015 on a machine using Windows Server 2012 as the OS it works just fine! I made sure that my Windows 10 machine has RSAT installed as well.


What is strange is that when I run the script from a PowerShell console on my Windows 10 machine, it works! Here is the exact code of my PowerShell script:

$user = [ADSI]"LDAP://192.111.222.33:389/CN=SomePerson,DC=Domain,DC=local"
$user.psbase.Username = "administrator"
$user.psbase.Password = "adminPassword"
Write-Output "Terminal Services profile path:"
Write-Output $user.psbase.invokeget("TerminalServicesProfilePath")

And here is the output from PowerShell:

PowerShell output

I also tried running the script in Visual Studio's PowerShell Interactive Window, and that works as well. Here is a screenshot and the output of that:
(Identifying info censored)

enter image description here


I found a very similar post to mine Here, but the provided answer does not work.
The answer to the above post states that the properties have changed names to:

  • msTSAllowLogon
  • msTSHomeDirectory
  • msTSHomeDrive
  • msTSProfilePath

But when I run my scripts with those property names, I do not receive the proper information back. They seem to not be the same properties. Here are a couple of screenshots of my user in Active Directory Users and Computers:

SomePerson Prop

You can see the property that I am attempting to retrieve above.
When I look at the attibutes of the user on the Attribute Editor tab, you can see msTSProfilePath, and it holds a different value:

enter image description here

Running the script with msTSProfilePath returns the property seen above in the Attribute Editor window (????????).


Additional Info:

I have tested this against two separate Active Directory Domains:

  1. One with Forest and Domain function levels of Windows Server 2012 with the DC running on a server with Windows Server 2012 Version 6.2 (Build 9200)
  2. The second with Forest and Domain Function levels of Windows Server 2012 R2 and running on Windows Server 2012 R2 Version 6.3 (Build 9600)

I ran this code on another Windows 10 machine and the problem persists.

Thank you!

2 Answers
Related