-2

I am trying to PreStage AD accounts from powershell out of C# but this code keeps giving me internal errors. Can Someone please see what i am doing wrong.

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();
}

Exception

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
user2316116
  • 6,726
  • 1
  • 21
  • 35
Ruan33
  • 29
  • 8
  • 1
    a) Is this a game of "guess the error"? b) OC=YRMC? Should that be OU or DC? – TessellatingHeckler Jul 07 '16 at 01:16
  • Its OU, so sorry about that... it was a long day of coding man and i was really tired let me try it and see if it fixes my issue. – Ruan33 Jul 07 '16 at 03:12
  • @TessellatingHeckler this did not fix my issue. I knew it wouldn't because if it was the problem it would have just told me it can't find the path. – Ruan33 Jul 07 '16 at 03:56

1 Answers1

0

The message is obvious and if you want to get more details on exception, you need to set in the .config file:

<serviceDebug includeExceptionDetailInFaults="true" />

See for more details: Turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug> configuration behavior) on the server

To make sure PS code works, try to execute it directly from PS (run powershell command from command prompt)

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

And see if it works. See for more details: https://technet.microsoft.com/en-us/library/ee617245.aspx

Last, but not least, to create an account you don't need to call PS from c#. See Add enabled Computer to Active Directory OU

Community
  • 1
  • 1
user2316116
  • 6,726
  • 1
  • 21
  • 35