简体   繁体   English

从C#执行包含远程服务器上的Sharepoint命令的Powershell

[英]Execute Powershell containing Sharepoint commands on remote server, from C#

Straight to the point: 开门见山:

I'm working on a service that is supposed to run on Sharepoint server. 我正在开发一个应该在Sharepoint服务器上运行的服务。 The service should be able to run a Powershell script locally and also run the same script on an array of Sharepoint servers in the same domain. 该服务应该能够在本地运行Powershell脚本,并在同一域中的Sharepoint服务器阵列上运行相同的脚本。 The service runs as a user who is admin on the local server AND the remote servers. 该服务以本地服务器和远程服务器上的管理员用户身份运行。

The powershell script contains the following: powershell脚本包含以下内容:

Try{
   Add-PSSnapin Microsoft.SharePoint.PowerShell
   Install-SPInfoPathFormTemplate -Path someInfoPath.xsn
}
Catch
{
    Add-Content C:\Temp\log.txt "$($_.Exception)" 
}

I have tried using the Powershell class in C# to invoke the script like this: 我尝试在C#中使用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(); 

This fails without returning any error to my C# program but in the log.txt file I can see this error: ....The Term Install-SPInfoPathFormTemplate is not a known cmdlet.... 这失败而没有向我的C#程序返回任何错误,但是在log.txt文件中我可以看到这个错误:....术语安装-SPInfoPathFormTemplate不是已知的cmdlet ....

This tell me that the Add-PSSnapin Microsoft.SharePoint.PowerShell command is not successfull. 这告诉我Add-PSSnapin Microsoft.SharePoint.PowerShell命令不成功。

So I tried to call the Powershell via a Process.Start() from C# like this: 所以我尝试通过C#中的Process.Start()调用Powershell,如下所示:

ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = false;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.FileName = "powershell.exe";
startInfo.WindowStyle = ProcessWindowStyle.Hidden;
startInfo.Arguments = "Invoke-Command -ComputerName \\machineName -Path C:\Temp\PowershellTest.ps1

   using (Process exeProcess = Process.Start(startInfo))
   {
        exeProcess.WaitForExit();
   }

However the result is the same. 但结果是一样的。

If I try to run the powershell script manually while logged on to a remote desktop it executes perfectly or returns an error, if I make an error on purpose. 如果我尝试在登录到远程桌面时手动运行powershell脚本,它会完美执行或返回错误,如果我故意犯了错误。

I suspect a Kerberos Double-Hop problem. 我怀疑是Kerberos双跳问题。

To the question: 1. Is it possible to run SharePoint InfoPath Services cmdlets ( https://technet.microsoft.com/en-us/library/ee906553.aspx ) remotely at all? 问题:1。是否可以远程运行SharePoint InfoPath Services cmdlet( https://technet.microsoft.com/en-us/library/ee906553.aspx )? 2. Could this be a Double-Hop problem and if so, how can I get around it? 2.这可能是一个双跳问题,如果是这样,我怎么能解决它?

Thanks in advance! 提前致谢!

Ok, so there where multiple errors in the technical setup of this solution. 好的,所以这个解决方案的技术设置中存在多个错误。

First: The service was running from IIS and in the advanced properties of the app pool the setting "Load user profile" was set to false. 第一:服务从IIS运行,在应用程序池的高级属性中,“加载用户配置文件”设置设置为false。 When this was set to true, the powershell was run successfully. 当此设置为true时,powershell已成功运行。

Second: In order to being able to call the powershell on the remote machine and avoid the Double-Hop problem, I had to use PSExec and include the credentials. 第二:为了能够在远程机器上调用powershell并避免双跳问题,我不得不使用PSExec并包含凭据。 (Of course passing the credentials meant I had to encrypt the config file since they were stored there) (当然,传递凭据意味着我必须加密配置文件,因为它们存储在那里)

Last but not least: The user that the app pool was running under did not have the appropiate amount of privileges. 最后但并非最不重要的一点:运行应用程序池的用户没有适当的权限。 This was VERY hard to debug because there PSExec returned a message saying: "Powershell exited with error code 0" but from the log I put in the powershell, I could see that the powershell was not executed. 这非常难以调试,因为PSExec返回一条消息说:“Powershell退出时出现错误代码0”但是从我放入powershell的日志中,我可以看到powershell没有被执行。

Most of the solution to this problem was of a technical nature but still I think it was relevant to share the outcome with you. 这个问题的大多数解决方案都是技术性的,但我认为与您分享结果仍然是相关的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM