Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何从c shsarp(c#)调用powershell脚本来设置文件夹中的ACL_C#_Windows_Powershell_Scripting - Fatal编程技术网

如何从c shsarp(c#)调用powershell脚本来设置文件夹中的ACL

如何从c shsarp(c#)调用powershell脚本来设置文件夹中的ACL,c#,windows,powershell,scripting,C#,Windows,Powershell,Scripting,这是我调用powershell脚本的c#代码 IDictionary<string, object> param5= new Dictionary<String, object>(); param5.Add("Username", "username"); param5.Add("FolderFilePath", FolderName); param5.Add("Securit

这是我调用powershell脚本的c#代码

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

您是否以管理员身份运行了C#应用程序?是否有关于此错误的更多堆栈跟踪信息?很高兴看到哪个方法生成了它,等等。@NIck在调试时,我在``cnt=Common.PowerShellScriptInvokerWithParam(“SetAcl”,param5)处发现了上述错误```@EylM是的,我正在以管理员模式运行visual studio。您是否以管理员身份运行C#应用程序?是否有关于此错误的更多堆栈跟踪信息?很高兴看到哪个方法生成了它,等等。@NIck在调试时,我在``cnt=Common.PowerShellScriptInvokerWithParam(“SetAcl”,param5)处发现了上述错误```@EylM是的,我正在管理员模式下运行visual studio
Exception thrown: 'System.Management.Automation.ParameterBindingArgumentTransformationException' in System.Management.Automation.dll
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