單刀直入:執行包含的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.這可能是一個雙跳問題,如果是的話,我該如何解決它?
提前致謝!