如何在 C# 中运行 New-ADComputer powershell

Posted

技术标签:

【中文标题】如何在 C# 中运行 New-ADComputer powershell【英文标题】:How to run New-ADComputer powershell in c# 【发布时间】:2016-11-09 03:55:44 【问题描述】:

我正在尝试从 C# 的 powershell 中 PreStage AD 帐户,但此代码不断给我内部错误。有人可以看看我做错了什么。

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

【问题讨论】:

a) 这是一个“猜错”的游戏吗? b) OC=YRMC?应该是 OU 还是 DC? 它的 OU,对此感到非常抱歉......这是编码人漫长的一天,我真的很累让我试试看它是否能解决我的问题。 @TessellatingHeckler 这并没有解决我的问题。我知道它不会,因为如果是问题,它会告诉我它找不到路径。 【参考方案1】:

消息很明显,如果您想获得更多关于异常的详细信息,您需要在 .config 文件中进行设置:

<serviceDebug includeExceptionDetailInFaults="true" />

查看详情:Turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug> configuration behavior) on the server

为确保 PS 代码正常工作,请尝试直接从 PS 执行它(从命令提示符运行 powershell 命令)

New-ADComputer -Name 'TESTESTS' -SamAccountName 'TESTESTS' -Path 'OU=Computers,OC=YRMC,DC=myorginization,DC=local'

然后看看它是否有效。详情见:https://technet.microsoft.com/en-us/library/ee617245.aspx

最后但同样重要的是,要创建一个您不需要从 c# 调用 PS 的帐户。见Add enabled Computer to Active Directory OU

【讨论】:

太棒了,我最终使用了 c# 方法并且它有效。感谢您的帮助!

以上是关于如何在 C# 中运行 New-ADComputer powershell的主要内容,如果未能解决你的问题,请参考以下文章

如何在 C# 中运行 powershell 命令

如何在 C# 中检查代码是不是在 AOT 中运行?

如何使用c#在cmd中运行命令[重复]

如何在 C# 中使用 TaskScheduler 设置“仅在登录时运行”和“运行方式”?

如何使用 C# 在 OSX 中正确运行 chmod?

如何停止在 C# 类中运行的计时器