[英]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.