How to import Module ActiveDirectory in C#, This is just an example how can you integrate powershell with C#, If you are ITPro code looks simple with the help of powershell.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Management.Automation; | |
using System.Management.Automation.Host; | |
using System.Management.Automation.Provider; | |
using System.Management.Automation.Runspaces; | |
using System.Collections.ObjectModel; | |
namespace PowerShell_Call | |
{ | |
class Program | |
{ | |
public static string GetDN(string args) | |
{ | |
string z="Get-Aduser -identity"+ " " + args; | |
using (PowerShell powershell = PowerShell.Create().AddScript(@"import-module ActiveDirectory").AddScript(z)) | |
{ | |
foreach (PSObject result in powershell.Invoke()) | |
{ | |
return result.Members["DistinguishedName"].Value.ToString(); | |
} | |
} | |
return ""; | |
} | |
static void Main(string[] args) | |
{ | |
string x = "anirban"; | |
string v = GetDN(x); | |
Console.WriteLine("The value is {0}", v); | |
Console.ReadLine(); | |
} | |
} | |
} |
ConsoleApplication and Powershell