如何在SharePoint解决方案中使用c#运行powershell脚本
当我尝试在SharePoint解决方案中运行power shell脚本时,出现以下错误 的类型初始值设定项 “System.Management.Automation.SessionStateScope”引发异常 仅供参考如何在SharePoint解决方案中使用c#运行powershell脚本,c#,powershell,sharepoint,sharepoint-2013,C#,Powershell,Sharepoint,Sharepoint 2013,当我尝试在SharePoint解决方案中运行power shell脚本时,出现以下错误 的类型初始值设定项 “System.Management.Automation.SessionStateScope”引发异常 仅供参考 private void RunPowershellScript(string scriptFile, List<string> parameters) { PowerShell OPowerShell = null; Runspace OSPRu
private void RunPowershellScript(string scriptFile, List<string> parameters)
{
PowerShell OPowerShell = null;
Runspace OSPRunSpace = null;
try
{
RunspaceConfiguration OSPRSConfiguration = RunspaceConfiguration.Create();
PSSnapInException OExSnapIn = null;
//Add a snap in for SharePoint. This will include all the power shell commands for SharePoint
PSSnapInInfo OSnapInInfo = OSPRSConfiguration.AddPSSnapIn("Microsoft.SharePoint.PowerShell", out OExSnapIn);
OSPRunSpace = RunspaceFactory.CreateRunspace(OSPRSConfiguration);
OPowerShell = PowerShell.Create();
OPowerShell.Runspace = OSPRunSpace;
Command Cmd1 = new Command("backup-spsite");
Cmd1.Parameters.Add("identity", "Your Site Coll URL");
Cmd1.Parameters.Add("path", "Back up file path");
OPowerShell.Commands.AddCommand(Cmd1 );
OSPRunSpace.Open();
OPowerShell.Invoke();
OSPRunSpace.Close();
}
catch (Exception Ex)
{
//Handle exception
}
finally
{
if (OSPRunSpace != null)
{
OSPRunSpace.Dispose();
OSPRunSpace = null;
}
if (OPowerShell != null)
{
OPowerShell.Dispose();
OPowerShell = null;
}
}
private void runpowershell脚本(字符串脚本文件,列表参数)
{
PowerShell-OPowerShell=null;
运行空间OSPRunSpace=null;
尝试
{
RunspaceConfiguration OSPRSConfiguration=RunspaceConfiguration.Create();
pssnapin=null;
//添加SharePoint的管理单元。这将包括SharePoint的所有power shell命令
PSSnapInInfo OSnapInInfo=OSPRSConfiguration.AddPSSnapIn(“Microsoft.SharePoint.PowerShell”,out-OExSnapIn);
OSPRunSpace=RunspaceFactory.CreateRunspace(OSPRSConfiguration);
OPowerShell=PowerShell.Create();
OPowerShell.Runspace=OSPRunSpace;
命令Cmd1=新命令(“备份spsite”);
Cmd1.Parameters.Add(“标识”、“您的站点链接URL”);
添加(“路径”,“备份文件路径”);
OPowerShell.Commands.AddCommand(Cmd1);
OSPRunSpace.Open();
OPowerShell.Invoke();
OSPRunSpace.Close();
}
捕获(例外情况除外)
{
//处理异常
}
最后
{
if(OSPRunSpace!=null)
{
OSPRunSpace.Dispose();
OSPRunSpace=null;
}
if(OPowerShell!=null)
{
OPowerShell.Dispose();
OPowerShell=null;
}
}
你能添加一些代码吗?你能检查一下内部异常是什么吗?它可能会进一步解释为什么在web.config文件中将下面标记中的legacyCasModel更改为“false”。你能给我一个示例,这样我就可以参考它了,因为我的解决方案中目前没有任何web.config文件