单刀直入:执行包含的SharePoint PowerShell的远程服务器上的命令,从C#
我的工作是应该到SharePoint服务器上运行的服务。 该服务应该能够在本地运行Powershell脚本,并且可以在同一个域中的Sharepoint服务器阵列上运行相同的脚本。 该服务以本地服务器和远程服务器上的admin用户身份运行。
PowerShell脚本包含以下内容:
WSManConnectionInfo connectionInfo = new WSManConnectionInfo();
connectionInfo.ComputerName = machineAddress;
Runspace runspace = RunspaceFactory.CreateRunspace(connectionInfo);
runspace.Open();
using (PowerShell ps = PowerShell.Create())
{
ps.Runspace = runspace;
ps.AddScript("Invoke-Expression C:\Temp\PowershellTest.ps1");
var results = ps.Invoke();
Console.WriteLine(results);
}
runspace.Close();
这个失败不返回任何错误我的C#:
Try{
Add-PSSnapin Microsoft.SharePoint.PowerShell
Install-SPInfoPathFormTemplate -Path someInfoPath.xsn
}
Catch
{
Add-Content C:\Temp\log.txt "$($_.Exception)"
}
我已经用C#PowerShell的类调用这样的脚本尝试程序,但在log.txt文件中,我可以看到此错误:....术语Install-SPInfoPathFormTemplate不是已知的cmdlet ....
这告诉我,Add-PSSn apin Microsoft.SharePoint.PowerShell命令不成功。
于是,我就从C#调用通过的Process.Start()PowerShell的是这样的:
但是结果是一样的。
如果我尝试在登录到远程桌面时手动运行powershell脚本,它会执行完美或者返回错误,如果我故意发生错误。
我怀疑Kerberos双跳问题。
问题: 1.是否可以远程运行SharePoint InfoPath Services cmdlet(https://technet.microsoft.com/en-us/library/ee906553.aspx)? 2.这可能是一个双跳问题,如果是的话,我该如何解决它?
提前致谢!