【发布时间】:2012-06-27 18:37:27
【问题描述】:
您好,我正在尝试从 powershell 访问共享点列表。 powershell 脚本是从 c# 应用程序执行的。
如果我从 c# 应用程序运行,我会得到以下异常:
使用“1”参数调用“.ctor”的异常:“Web 应用程序 在
http://pc/websites/Test4/找不到。确认您有 正确输入了 URL。如果 URL 应该提供现有的 内容,系统管理员可能需要添加新的请求 URL 映射到预期的应用程序。”
我的代码:
C#
string cmdArg = "C:\\Scripts\\GroupChangeGroup.ps1 1";
Runspace runspace = RunspaceFactory.CreateRunspace();
runspace.ApartmentState = System.Threading.ApartmentState.STA;
runspace.ThreadOptions = PSThreadOptions.UseCurrentThread;
runspace.Open();
Pipeline pipeline = runspace.CreatePipeline();
Command newcom = new Command("Add-PSSnapin");
newcom.Parameters.Add("Name", "microsoft.exchange.management.powershell.e2010");
pipeline.Commands.Add(newcom);
Command newcom2 = new Command("Add-PSSnapin");
newcom2.Parameters.Add("Name", "Microsoft.SharePOint.POwershell");
pipeline.Commands.Add(newcom2);
pipeline.Commands.AddScript(cmdArg);
pipeline.Commands[0].MergeMyResults(PipelineResultTypes.Error, PipelineResultTypes.Output);
Collection<PSObject> results = pipeline.Invoke();
var error = pipeline.Error.ReadToEnd();
runspace.Close();
Powershell
$site = New-Object Microsoft.SharePoint.SPSite("http://pc/websites/Test4/")
$web = $site.OpenWeb()
$list = $web.Lists["GroupsList - ListInstance1"]
$listitem = $list.Items.GetItemByID(3)
如果我从 PS 执行应用程序没有问题。 (相同的用户上下文)
用户是WSS_ADMIN_WPG的成员
【问题讨论】:
标签: c# sharepoint c#-4.0 powershell sharepoint-2010