Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用C#和Powershell从不属于域的计算机访问Active Directory_C#_Powershell_Active Directory - Fatal编程技术网

使用C#和Powershell从不属于域的计算机访问Active Directory

使用C#和Powershell从不属于域的计算机访问Active Directory,c#,powershell,active-directory,C#,Powershell,Active Directory,我正在尝试从不属于我的域的计算机访问Active Directory。我在使用C#时,大部分都使用了Powershell脚本来获取所需的信息 我需要运行的脚本很简单,如下所示: string script = "Import-Module activedirectory \n" + "(Get-ADUser -Filter * -Server 192.168.1.20| ConvertTo-XML " + "-NoTypeInformation).

我正在尝试从不属于我的域的计算机访问Active Directory。我在使用C#时,大部分都使用了Powershell脚本来获取所需的信息

我需要运行的脚本很简单,如下所示:

string script = "Import-Module activedirectory \n" +
            "(Get-ADUser -Filter * -Server 192.168.1.20| ConvertTo-XML " +
            "-NoTypeInformation).Save(\"C:\\data\\ad.xml\")";
我有一些C#代码来运行脚本,然后它读取一个创建的文件,并将我需要的信息收集到一个文本区域(未显示)

只要计算机是域的一部分,一切都可以正常工作,但是当我试图从域外传递相同的查询时,我的问题就出现了


我已尝试将凭据参数添加到脚本中,但pipeline.Invoke()将不接受需要用户输入的参数。有什么建议吗?

从Windows Server 2003开始,AD的默认安全策略是要求用户身份验证使用安全通道(因此用户凭据不会以明文形式传递)。客户端计算机使用其域凭据设置该安全通道。您必须故意削弱域安全策略,才能从非加入域的计算机进行身份验证。

因此无法从远程计算机查询AD?我可以使用指定的-Credential PSCredential参数从远程Powershell运行脚本。但我不能从C#内部做这件事?我认为你不能直接做。您必须通过安全通道与DC通话,并且只有加入域的计算机才能做到这一点。
RunScript(script);

private void RunScript(string scriptText)
{
    Runspace runspace = RunspaceFactory.CreateRunspace();

    runspace.Open();

    Pipeline pipeline = runspace.CreatePipeline();
    pipeline.Commands.AddScript(scriptText);

    pipeline.Commands.Add("Out-String");

    pipeline.Invoke();

    runspace.Close();
}