【问题标题】:Launching SharePoint Management Shell (PowerShell) From C#从 C# 启动 SharePoint 命令行管理程序 (PowerShell)
【发布时间】:2012-12-01 22:12:14
【问题描述】:

我正在尝试从 C# 代码以编程方式启动 SharePoint 命令行管理程序,但当我从“开始”菜单中选择它时,我得到了不同的行为。我尝试了两种不同的方法,结果相同:

尝试 #1:

var startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.Verb = "runas";
startInfo.FileName = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\PowerShell.exe";
startInfo.Arguments = "-NoExit \"  & 'C:\\Program Files\\Common Files\\Microsoft Shared\\Web Server Extensions\\14\\CONFIG\\POWERSHELL\\Registration\\sharepoint.ps1' \"";
Process.Start(startInfo);

尝试 #2:

var startInfo = new ProcessStartInfo();
startInfo.UseShellExecute = true;
startInfo.Verb = "runas";
startInfo.FileName = "C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\\Microsoft SharePoint 2010 Products\\SharePoint 2010 Management Shell.lnk";
Process.Start(startInfo);

这两种方法都会启动一个 PowerShell 窗口并显示以下错误消息:

文件 C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\CO NFIG\POWERSHELL\Registration\SharePoint.ps1 不能 加载是因为在此系统上禁用了脚本的执行。 有关更多详细信息,请参阅“get-help about_signing”。在行:1 字符:3 + &

当我从“开始”菜单启动它时,它运行良好。关于为什么这种行为不同的任何想法?在尝试 #1 中,我运行与链接相同的可执行文件和参数。在尝试 #2 中,我尝试运行链接本身。

谢谢!

【问题讨论】:

    标签: c# sharepoint powershell


    【解决方案1】:

    这个问题通常是由 x86 和 x64 PowerShell 引起的,它们都需要将 ExecutionPolicy 设置为 Restricted 以外的其他值才能运行脚本。也许您的 PowerShell 控制台是 x64,而您的 C# 程序正在以 x86 运行,反之亦然。无论哪种方式,将附加参数传递给 PowerShell -ExecutionPolicy ByPass 以跳过此特定问题。

    【讨论】:

    • 太棒了! x86 vs x64 是问题所在。我添加了您的标志,并将我的应用程序设置为编译为 x64(无论如何这是必要的),瞧,它可以工作。我会在几分钟后标记为答案。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2019-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-13
    • 2018-04-11
    • 1970-01-01
    相关资源
    最近更新 更多