[英]Error while running AD commandlets in powershell from C#
I have tried executing AD commandlets in powershell using C# from the same machine. 我曾尝试在同一台计算机上使用C#在powershell中执行AD Commandlet。 This works fine.
这很好。
static void Main(string[] args)
{
InitialSessionState iss = InitialSessionState.CreateDefault();
iss.ImportPSModule(new string[] { "activedirectory" });
Runspace myRunSpace = RunspaceFactory.CreateRunspace(iss);
myRunSpace.Open();
Pipeline pipeLine = myRunSpace.CreatePipeline();
Command myCommand = new Command("Get-ADUser");
myCommand.Parameters.Add("Filter", "sAMAccountName -eq 'user1'");
//myCommand.Parameters.Add("IncludeDeletedObjects");
pipeLine.Commands.Add(myCommand);
//Command restoreCommand = new Command("Restore-ADObject");
//pipeLine.Commands.Add(restoreCommand);
Console.WriteLine("Before Invoke");
Collection<PSObject> commandResults = pipeLine.Invoke();
Console.WriteLine("After Invoke");
foreach (PSObject cmdlet in commandResults)
{
//Console.WriteLine("Inside foreach");
string cmdletName = cmdlet.BaseObject.ToString();
System.Diagnostics.Debug.Print(cmdletName);
Console.WriteLine(cmdletName);
}
Console.ReadLine();
}
But while trying to run the same command remotely using the invoke command it gives the error The term 'Get-ADUser' is not recognized as the name of a cmdlet, function, script file, or operable program.
但是,当尝试使用invoke命令远程运行同一命令时,会出现错误
The term 'Get-ADUser' is not recognized as the name of a cmdlet, function, script file, or operable program.
The following is my program : 以下是我的程序:
static void Main(string[] args)
{
string shellUri = "http://schemas.microsoft.com/powershell/Microsoft.PowerShell";
string userName = "Domain\\Administrator";
string password = "Password";
SecureString securePassword = new SecureString();
foreach (char c in password)
{
securePassword.AppendChar(c);
}
PSCredential credential = new PSCredential(userName, securePassword);
WSManConnectionInfo connectionInfo = new WSManConnectionInfo(false, "machinename", 5985, "/wsman", shellUri, credential);
using (Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo))
{
runspace.Open();
using (PowerShell powershell = PowerShell.Create())
{
powershell.Runspace = runspace;
PSCommand new1 = new PSCommand();
new1.AddCommand("Get-ADUser");
new1.AddParameter("identity", "CN=user1,DC=example,DC=com");
powershell.Commands = new1;
Collection<PSObject> results = powershell.Invoke();
foreach (PSObject obj in results)
{
PSMemberInfoCollection<PSPropertyInfo> propInfos = obj.Properties;
Console.WriteLine("********************");
foreach (PSPropertyInfo propInfo in propInfos)
{
string propInfoValue = (propInfo.Value == null) ? "" : propInfo.Value.ToString();
Console.WriteLine("{0} --> {1}", propInfo.Name, propInfoValue);
}
}
}
}
}
invoke-command -computername $DC -ScriptBlock {Remove-ADUser -identity "user1"} -credential $cred
- i get the error The term 'Remove-ADUser' is not recognized as the name of a cmdlet, function, script file, or operable program
. invoke-command -computername $DC -ScriptBlock {Remove-ADUser -identity "user1"} -credential $cred
我会收到错误消息The term 'Remove-ADUser' is not recognized as the name of a cmdlet, function, script file, or operable program
。 But it is possible to use command Remove-ADUser -identity "user1" -server $DC -credential $cred
.How to directly execute the AD command in powershell from C# client? 但是可以使用命令
Remove-ADUser -identity "user1" -server $DC -credential $cred
如何从C#客户端直接在powershell中执行AD命令?
You need to import the ActiveDirectory module in the remote runspace before executing the AD command eg: 在执行AD命令之前,您需要在远程运行空间中导入ActiveDirectory模块,例如:
powershell.Commands.AddCommand("Import-Module").AddArgument("ActiveDirectory");
powershell.Invoke();
powershell.Commands.Clear();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.