Это мой код на C # для вызова сценария powershell
IDictionary<string, object> param5= new Dictionary<String, object>();
param5.Add("Username", "username");
param5.Add("FolderFilePath", FolderName);
param5.Add("Security", "FullControl");
param5.Add("AccessControlType", "Allow");
param5.Add("Value1", "$true");
param5.Add("Value2", "$true");
cnt = Common.PowerShellScriptInvokerWithParam("SetAcl", param5);
Я определил метод PowerShellScriptInvokerWithParam как
public static List<PSObject> PowerShellScriptInvokerWithParam(string scriptName, IDictionary<string, object> parameters)
{
var scriptFile = Application.StartupPath.Replace(@"bin\Debug", "") + @"\PowerShellScripts\" + scriptName + ".ps1";
// Validate parameters
if (string.IsNullOrEmpty(scriptFile)) { throw new ArgumentNullException("scriptFile"); }
if (parameters == null) { throw new ArgumentNullException("parameters"); }
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
using (Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration))
{
runspace.Open();
RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
Pipeline pipeline = runspace.CreatePipeline();
Command scriptCommand = new Command(scriptFile);
Collection<CommandParameter> commandParameters = new Collection<CommandParameter>();
foreach (string scriptParameter in parameters.Keys)
{
CommandParameter commandParm = new CommandParameter(scriptParameter, parameters[scriptParameter]);
commandParameters.Add(commandParm);
scriptCommand.Parameters.Add(commandParm);
}
pipeline.Commands.Add(scriptCommand);
pipeline.Commands.Add("Out-String");
List<PSObject> psObjects;
psObjects = pipeline.Invoke().ToList();
return psObjects;
}
}
Но получаю ошибку как
Exception thrown: 'System.Management.Automation.ParameterBindingArgumentTransformationException' in System.Management.Automation.dll
Powershellсценарий
param([Parameter(Mandatory=$true)][string]$Username,[Parameter(Mandatory=$true)][string]$FolderFilePath,[Parameter(Mandatory=$true)][string]$Security,[Parameter(Mandatory=$true)][string]$AccessControlType,[Parameter(Mandatory=$true)][bool]$Value1,[Parameter(Mandatory=$true)][bool]$Value2)
$acl = Get-Acl $FolderFilePath
$acl.SetAccessRuleProtection($Value1, $Value2)
#For "Security="FullControl","Read","Modify","Read Execute","Read","Write""
$rule = New-Object System.Security.AccessControl.FileSystemAccessRule($Username,$Security, "ContainerInherit, ObjectInherit", "InheritOnly", $AccessControlType)
$acl.AddAccessRule($rule)
Set-Acl $FolderFilePath $acl
Ожидается изменение ACL в указанной папке в соответствии с пользовательским вводом в сценарии. Когда я запускаю скрипт powershell от имени администратора вручную. Это применяет ACL.