C# 如何在c中运行新的ADComputer powershell

C# 如何在c中运行新的ADComputer powershell,c#,powershell,active-directory,C#,Powershell,Active Directory,我试图将powershell中的AD帐户从C中预存出来,但这段代码一直给我内部错误。有人能看看我做错了什么吗 PowerShell ps = PowerShell.Create(); ps.Commands.AddCommand("Import-Module").AddArgument("ActiveDirectory"); ps.Invoke(); ps.Commands.Clear(); ps.Commands.AddCommand("New-ADComputer"); ps.AddPara

我试图将powershell中的AD帐户从C中预存出来,但这段代码一直给我内部错误。有人能看看我做错了什么吗

PowerShell ps = PowerShell.Create();
ps.Commands.AddCommand("Import-Module").AddArgument("ActiveDirectory");
ps.Invoke();
ps.Commands.Clear();
ps.Commands.AddCommand("New-ADComputer");
ps.AddParameter("-Name", "'TESTESTS'");
ps.AddParameter("-SamAccountName", "'TESTESTS'");
ps.AddParameter("-Path", "'OU=Computers,OC=YRMC,DC=myorginization,DC=local'");

try
{
    ps.Invoke();
    Console.ReadKey();
}
catch (Exception e)
{
    Console.WriteLine(e.ToString());
    Console.Read();
}
例外情况

System.Management.Automation.CmdletInvocationException: The server was unable to process the request due to an internal error.  For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug> configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework 3.0 SDK documentation and inspect the server trace logs. ---> Microsoft.ActiveDirectory.Management.ADException: The server was unable to process the request due to an internal error.  For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug> configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework 3.0 SDK documentation and inspect the server trace logs. ---> System.ServiceModel.FaultException: The server was unable to process the request due to an internal error.  For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug> configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework 3.0 SDK documentation and inspect the server trace logs.
   --- End of inner exception stack trace ---
   at Microsoft.ActiveDirectory.Management.AdwsConnection.ThrowException(AdwsFault adwsFault, FaultException faultException)
   at Microsoft.ActiveDirectory.Management.AdwsConnection.Create(ADAddRequest request)
   at Microsoft.ActiveDirectory.Management.ADWebServiceStoreAccess.Microsoft.ActiveDirectory.Management.IADSyncOperations.Add(ADSessionHandle handle, ADAddRequest request)
   at Microsoft.ActiveDirectory.Management.ADActiveObject.Create()
   at Microsoft.ActiveDirectory.Management.Commands.ADNewCmdletBase`3.ADNewCmdletBaseProcessCSRoutine()
   at Microsoft.ActiveDirectory.Management.CmdletSubroutinePipeline.Invoke()
   at Microsoft.ActiveDirectory.Management.Commands.ADCmdletBase`1.ProcessRecord()
   --- End of inner exception stack trace ---
   at System.Management.Automation.Runspaces.PipelineBase.Invoke(IEnumerable input)
   at System.Management.Automation.PowerShell.Worker.ConstructPipelineAndDoWork(Runspace rs, Boolean performSyncInvoke)
   at System.Management.Automation.PowerShell.Worker.CreateRunspaceIfNeededAndDoWork(Runspace rsToUse, Boolean isSync)
   at System.Management.Automation.PowerShell.CoreInvokeHelper[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.CoreInvoke[TInput,TOutput](PSDataCollection`1 input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.CoreInvoke[TOutput](IEnumerable input, PSDataCollection`1 output, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.Invoke(IEnumerable input, PSInvocationSettings settings)
   at System.Management.Automation.PowerShell.Invoke()
   at Test_Methods.Program.Main(String[] args) in C:\Users\rubotha\documents\visual studio 2015\Projects\Test Methods\Test Methods\Program.cs:line 45

这条消息很明显,如果要获取有关异常的更多详细信息,需要在.config文件中设置:

<serviceDebug includeExceptionDetailInFaults="true" />
看看它是否有效。有关更多详细信息,请参阅:


最后,但并非最不重要的是,要创建一个帐户,您不需要从c调用PS。看

a这是一个猜错的游戏吗?b OC=YRMC?应该是OU还是DC?是OU,很抱歉。。。这是一个漫长的编码工作日,我真的很累。让我试试,看看它是否解决了我的问题。@TessellingEckler这并没有解决我的问题。我知道不会,因为如果是问题,它会告诉我它找不到路径。太棒了,我最终使用了c方法,它成功了。谢谢你的帮助!
New-ADComputer -Name 'TESTESTS' -SamAccountName 'TESTESTS' -Path 'OU=Computers,OC=YRMC,DC=myorginization,DC=local'