c# - 从 C# 在 powershell 中运行 AD 命令行开关时出错

标签 c# powershell active-directory powershell-remoting

我曾尝试在同一台机器上使用 C# 在 powershell 中执行 AD commandlet。这很好用。

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

但是当尝试使用 invoke 命令远程运行相同的命令时,它会给出错误 The term 'Get-ADUser' is not recognized as the name of a cmdlet, function, script file, or operable 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);
                 }


             }

            }
        }
     }
  1. 如何实现远程调用AD commandlets?
  2. 有没有办法使用 InitialSessionState 而不是 WSManConnectionInfo 来远程调用命令。
  3. 如果我使用 invoke-command -computername $DC -ScriptBlock {Remove-ADUser -identity "user1"} -credential $cred - 我得到错误 The term 'Remove-ADUser ' 未被识别为 cmdlet、函数、脚本文件或可运行程序的名称

但是可以使用命令 Remove-ADUser -identity "user1"-server $DC -credential $cred 。如何从 C# 客户端直接在 powershell 中执行 AD 命令?

最佳答案

在执行 AD 命令之前,您需要在远程运行空间中导入 ActiveDirectory 模块,例如:

powershell.Commands.AddCommand("Import-Module").AddArgument("ActiveDirectory");
powershell.Invoke();
powershell.Commands.Clear();

关于c# - 从 C# 在 powershell 中运行 AD 命令行开关时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20159160/

相关文章:

c# - C#程序中的System.IO.Compression.FileSystem.dll

c# - Visual Studio 2010 构建错误 - 来自 HRESULT : 0x800300FA (STG_E_ABNORMALAPIEXIT)) 的异常

RegEx PowerShell 匹配

powershell - 如何添加到 PowerShell 中的管道?

powershell - 使用 PowerShell 仅查找名为 OLD 且具有一定期限的子目录中的文件

c# - 使用 C# .NET Core 3.1 返回具有属性的组成员的 Active Directory

c# - 是否有 C# 不区分大小写的等于运算符?

c# - 在现有 MySQL 数据库上使用 Entity Framework 4.1 Code-First

c# - FindByIdentity 在 ASP.NET webapp 中因 PricipalOperationException 而失败

powershell - 远程注册表项提取器 PowerShell 脚本