C#无法运行PowerShell脚本添加用户Active Directory

时间:2018-12-11 16:21:36

标签: c# powershell

我尝试使用C#运行powershell脚本将其添加到活动目录中。

这是我的C#代码,用于将参数传递给Powershell脚本。

public void AddUserNotExpire(string fNm, string lNm, string uNm, string pWd, string gpUsr)
        {


            RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
            Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
            runspace.Open();
            RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
            Pipeline pipeline = runspace.CreatePipeline();
            string scriptfile = @"C:\Users\Administrator\Documents\addusernotexpire.ps1";
            Command myCommand = new Command(scriptfile);
            CommandParameter p1 = new CommandParameter("fname", fNm);
            CommandParameter p2 = new CommandParameter("lname", lNm);
            CommandParameter p3 = new CommandParameter("uname", uNm);
            CommandParameter p4 = new CommandParameter("pWd", pWd);
            CommandParameter p5 = new CommandParameter("grpUser", gpUsr);
            myCommand.Parameters.Add(p1);
            myCommand.Parameters.Add(p2);
            myCommand.Parameters.Add(p3);
            myCommand.Parameters.Add(p4);
            myCommand.Parameters.Add(p5);
            pipeline.Commands.Add(myCommand);

            // invoke execution on the pipeline (ignore output)
            pipeline.Invoke();
        }

这是Powershell脚本文件名addusernotexpire.ps1是从C#调用的

[CmdletBinding()]
param (
    [string] $fname,
    [string] $lname,
    [string] $uname,
    [string] $pWd,
    [string] $grpUser 
)

if (Get-ADUser -F {SamAccountName -eq $uname})
{
         #If user does exist, give a warning
    Write-Warning "A user account with username $uname already exist in Active Directory."
}
else
{
    New-ADUser `
      -Name $fname `
      -Surname $lname `
      -DisplayName "$fname $lname" `
      -SamAccountName $uname `
      -UserPrincipalName "$uname@test.com" `
      -Enabled $True `
      -Path "OU=$grpUser,DC=test,DC=com" `
      -AccountPassword $pWd| ConvertTo-SecureString -AsPlainText -Force

} 

当我运行C#代码时,它显示出这样的错误。如何解决?

Exception thrown: 'System.Management.Automation.ParameterBindingException' in System.Management.Automation.dll
Exception thrown: 'System.Management.Automation.ParameterBindingException' in System.Management.Automation.dll
Exception thrown: 'System.Management.Automation.ParameterBindingException' in mscorlib.dll
Exception thrown: 'System.Management.Automation.ParameterBindingException' in System.Management.Automation.dll
Exception thrown: 'System.Management.Automation.ParameterBindingException' in System.Management.Automation.dll
The thread 0x15ac has exited with code 0 (0x0).
The thread 0x1be0 has exited with code 0 (0x0).
The thread 0x1690 has exited with code 0 (0x0).

1 个答案:

答案 0 :(得分:0)

我尝试使用以下C#代码对其进行测试:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using System.Management.Automation.Runspaces;
using System.Text;
using System.Threading.Tasks;

namespace PowershellCallTest
{
    class Program
    {
        static void Main(string[] args)
        {
            AdduserNotExpire(fNm: "first", lNm: null, uNm: "username", pWd: "myPass", gpUser: "myGroup");
        }

        private static void AdduserNotExpire(string fNm, string lNm, string uNm, string pWd, string gpUser)
        {
            RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
            Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
            runspace.Open();
            RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
            Pipeline pipeline = runspace.CreatePipeline();
            string scriptfile = @"C:\temp\addusernotexpire.ps1";
            Command myCommand = new Command(scriptfile);
            CommandParameter p1 = new CommandParameter("fname", fNm);
            CommandParameter p2 = new CommandParameter("lname", lNm);
            CommandParameter p3 = new CommandParameter("uname", uNm);
            CommandParameter p4 = new CommandParameter("pWd", pWd);
            CommandParameter p5 = new CommandParameter("grpUser", gpUser);
            myCommand.Parameters.Add(p1);
            myCommand.Parameters.Add(p2);
            myCommand.Parameters.Add(p3);
            myCommand.Parameters.Add(p4);
            myCommand.Parameters.Add(p5);
            pipeline.Commands.Add(myCommand);

            // invoke execution on the pipeline (ignore output)
            pipeline.Invoke();
        }
    }
}

简化的Powerhsell代码:

[CmdletBinding()]
param (
    [string] $fname,
    [string] $lname,
    [string] $uname,
    [string] $pWd,
    [string] $grpUser 

Write-Warning "A user account with username $uname already exist"

我的环境似乎很好。

要获取类似“ System.Management.Automation.ParameterBindingException”的错误

我必须更改绑定,例如:

CommandParameter p1 = new CommandParameter("unknownparam", fNm);

所以我的假设是:

您可能没有调用正确的Powershell脚本,这就是为什么出现此错误的原因。您能否检查一下计算机上Powershell中的参数是否相同?

如果您使用C#运行调试器,那么您还应该能够查看异常并查看哪个参数:

Debug Exception Error