小编Rob*_*ute的帖子

在Visual Studio中运行PowerShell脚本时无法获取Active Directory终端服务属性

我遇到了一个奇怪的问题,也许有人可以帮助我.

我试图在使用Windows 10的计算机上使用C#从Active Directory用户检索终端服务属性.我这样做是通过在我的应用程序中运行PowerShell脚本,如下所示:

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;
            }
        }
Run Code Online (Sandbox Code Playgroud)

我得到这个例外:

System.Management.Automation.MethodInvocationException:'带有"1"参数的异常调用"InvokeGet":"未知名称.(HRESULT异常:0x80020006(DISP_E_UNKNOWNNAME))"'

当我在使用Windows Server 2012作为操作系统的计算机上运行Visual Studio 2015中的上述代码时,它工作正常!我确保我的Windows 10机器也安装了RSAT.


奇怪的是,当我从Windows 10机器上的PowerShell控制台运行脚本时,它可以工作!以下是我的PowerShell脚本的确切代码:

$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")
Run Code Online (Sandbox Code Playgroud)

以下是PowerShell的输出:

PowerShell输出

我也尝试在Visual Studio的PowerShell交互式窗口中运行该脚本,这也是有效的.这是截图和输出:(
识别信息审查)

在此输入图像描述


我在这里发现了一个非常相似的帖子,但提供的答案不起作用.
上述帖子的答案表明属性已将名称更改为:

  • msTSAllowLogon
  • msTSHomeDirectory
  • msTSHomeDrive
  • msTSProfilePath

但是,当我使用这些属性名称运行脚本时,我没有收到正确的信息.它们似乎不是同一个属性.以下是我的用户在Active …

c# powershell active-directory visual-studio

3
推荐指数
1
解决办法
945
查看次数

标签 统计

active-directory ×1

c# ×1

powershell ×1

visual-studio ×1