3

我正在尝试从 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”。在 line:1 char:3 + & <<<< ' C:\Program Files\Common Files\Microsoft Shared\Web Server Extensio ns\14\CONFIG\POWERSHELL\Registration\sharepoint.ps1 ' + CategoryInfo : NotSpecified: (: ) [], PSSecurityException + FullyQualifiedErrorId : RuntimeException

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

谢谢!

4

1 回答 1

2

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

于 2012-12-13T22:37:53.423 回答