繁体   English   中英

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

[英]C# Can not run PowerShell script to add user Active Directory

我尝试使用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).

我尝试使用以下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#中运行调试器,则还应该能够查看异常并查看哪个参数:

调试异常错误

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM