【问题标题】:Accessing sharepoint from Powershell-script from c# application从 C# 应用程序的 Powershell 脚本访问共享点
【发布时间】: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


    【解决方案1】:

    SharePoint 不适用于 .Net 4.0。 PowerShell 与 .Net 3.5 一起运行,这就是它与 PS 一起使用的原因。

    您应该切换回 .Net 3.5 SP1。

    另外,请务必将您的项目设置为不使用 X86 编译设置运行。

    将其设置为 AnyCPU 或 x64。

    【讨论】:

    • 我将框架更改为 3.5,并将目标 CPU 设置为 x64,但没有成功。我不认为是框架造成了麻烦,因为共享点操作在 powershellscript 中? ...
    • 尝试在你的脚本中添加$farm = Get-Farm 并检查它是否加载正确。
    • 我认为您的意思是 $farm = Get-SPFarm?如果我只在 ps 脚本中执行它,它就可以工作。如果我从 c# 启动脚本,则会出现以下故障:“Get-SPFarm”一词未被识别为 cmdlet、函数、脚本文件或可运行程序的名称。检查名称的拼写,如果包含路径,请验证路径是否正确,然后重试。
    • 在脚本中,您是否包含 Add-PSSnapin Microsoft.SharePOint.POwershell ?如果没有,你应该这样做。对于脚本的内容,这不是强制性的,但它可能有助于诊断问题。
    • 我已经根据您的评论编辑了我的问题代码,但我仍然收到同样的错误!
    猜你喜欢
    • 2015-06-28
    • 1970-01-01
    • 2018-12-05
    • 1970-01-01
    • 2011-01-28
    • 1970-01-01
    • 2014-01-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多