因为需要在web端管理SVN文件夹权限,参考网上资料写了如下方法调用PowerShell,
public static List<PSObject> RunPowershell(string filePath, params string[]
parameters)
{
RunspaceConfiguration runspaceConfiguration = RunspaceConfiguration.Create();
Runspace runspace = RunspaceFactory.CreateRunspace(runspaceConfiguration);
runspace.Open();
runspace.SessionStateProxy.SetVariable("ConfirmPreference", "None");
RunspaceInvoke scriptInvoker = new RunspaceInvoke(runspace);
Pipeline pipeline = runspace.CreatePipeline();
Command scriptCommand = new Command(filePath);
List<CommandParameter> commandParameters = new List<CommandParameter>();
foreach (var parameter in parameters)
{
if (parameter.IndexOf("=") != -1)
{
string[] namevalue = parameter.Split('=');
CommandParameter commandParm = new CommandParameter(namevalue[0], namevalue[1]);
commandParameters.Add(commandParm);
scriptCommand.Parameters.Add(commandParm);
}
else
{
CommandParameter commandParm = new CommandParameter(null, parameter);
commandParameters.Add(commandParm);
scriptCommand.Parameters.Add(commandParm);
}
}
//pipeline.Input.Write("$ConfirmPreference='None'");
pipeline.Commands.Add(scriptCommand);
List<PSObject> psObjects = new List<PSObject>();
psObjects.AddRange(pipeline.Invoke());
if (pipeline.Error.Count > 0)
{
pipeline.Error.ReadToEnd();
throw new Exception("Something is wrong in PowerShell script.");
}
runspace.Close();
return psObjects;
}
但在删除SVN权限是Powershell需要确认的动作,查了很多资料如:http://www.pstips.net/powershell-skip-confirm-prompt.html,知道了要设置环境变量才能解决.但怎么设置摸了很久,MSDN,Stack Overflow,都没有介绍.最后在codeproject看到一篇文章后尝试才解决关键代码如下:
runspace.SessionStateProxy.SetVariable("ConfirmPreference", "None");
希望帮到同样问题的人.