Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从C运行PowerShell脚本时,添加PSSnapin的路径不正确#_C#_Powershell_Sharepoint_Path_Pssnapin - Fatal编程技术网

C# 从C运行PowerShell脚本时,添加PSSnapin的路径不正确#

C# 从C运行PowerShell脚本时,添加PSSnapin的路径不正确#,c#,powershell,sharepoint,path,pssnapin,C#,Powershell,Sharepoint,Path,Pssnapin,我使用C#工具运行PowerShell脚本,如下所示: 使用(PowerShell pshell=PowerShell.Create()) { pshell.AddCommand(scriptFullPath); pshell.AddParameter(“用户名”,用户); pshell.AddParameter(“密码”,pass); PSDataCollection outputCollection=新PSDataCollection(); PSInvocationSettings=新PSI

我使用C#工具运行PowerShell脚本,如下所示:

使用(PowerShell pshell=PowerShell.Create())
{
pshell.AddCommand(scriptFullPath);
pshell.AddParameter(“用户名”,用户);
pshell.AddParameter(“密码”,pass);
PSDataCollection outputCollection=新PSDataCollection();
PSInvocationSettings=新PSInvocationSettings();
settings.ErrorActionPreference=ActionPreference.Stop;
调用(null、outputCollection、设置);
}
在我需要来自其他程序集的特殊cmdlet之前,脚本中几乎所有内容都可以正常工作。Add PSSnapin命令将始终失败,原因是:

Exception: The Windows PowerShell snap-in 'Microsoft.SharePoint.Powershell' is not installed on this computer.
Exception: Cannot bind parameter 'Path' to the target. Exception setting "Path": "Cannot find path 'D:\dev\tool\Microsoft.SharePoint.dll' because it does not exist."
跑步时

$snapin=Get-PSSnapin | Where对象{$\.Name-eq“Microsoft.SharePoint.Powershell”}
如果($snapin-eq$null)
{
写入主机“加载SharePoint Powershell管理单元”
在“Microsoft.SharePoint.Powershell”中添加PSSnapin
添加类型-路径“Microsoft.SharePoint.dll”
添加类型-路径“Microsoft.SharePoint.Runtime.dll”
}
当直接在PowerShell窗口中运行脚本时,一切正常,因此我猜这与未从C#工具转发的路径或范围有关。使用AddCommand的参数useLocalScope或其他参数并没有产生任何结果(尽管我不确定这是否与路径有关)


如何使脚本正常工作并查找外部程序集?

SharePoint PowerShell管理单元仅64位可用。您的C#工具可能作为x86进程运行,因此会出现错误“未安装”。此外,您可能必须以“管理员”身份运行该程序,因为某些命令需要该程序才能运行

第二个错误是,您是对的,默认情况下SharePoint没有设置路径变量。解决方法是指定.dll的完整路径(并更改安装的版本号),例如

Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\15\ISAPI\Microsoft.SharePoint.dll"