【问题标题】:C# Remote Powershell SharePoint 2010C# 远程 Powershell SharePoint 2010
【发布时间】:2012-10-25 18:09:36
【问题描述】:

我正在尝试从 c# 控制台应用程序在远程共享点服务器上运行 powershell 命令。这是我到目前为止的代码。它运行没有错误,但什么也不做。我做错了什么?

谢谢

服务器名、用户名密码、url被取出

    public static string RunScript()
    {

       Runspace remoteRunspace = null;
       openRunspace("http://server/wsman",
            "http://schemas.microsoft.com/powershell/Microsoft.PowerShell",
            @"domain\user",
            "password",
            ref remoteRunspace);
       try
       {
           StringBuilder stringBuilder = new StringBuilder();
           using (PowerShell powershell = PowerShell.Create())
           {

               powershell.Runspace = remoteRunspace;
               powershell.AddScript("Add-PsSnapin Microsoft.SharePoint.PowerShell");
               powershell.AddScript("enable-SPFeature -identity \"2dfc204b-e9da-4c6c-8b4f-c2f7c593ad4e\" -url sharepointsite -Confirm:$False");
               powershell.Invoke();
               Collection<PSObject> results = powershell.Invoke();
               remoteRunspace.Close();
               foreach (PSObject obj in results)
               {
                   stringBuilder.AppendLine(obj.ToString());
               }
           }

           return stringBuilder.ToString();
       }
       catch (Exception e)
       {

           return "";

       }


     }

    public static void  openRunspace(string uri, string schema, string username, string livePass, ref Runspace remoteRunspace)
    {
        System.Security.SecureString password = new System.Security.SecureString();
        foreach (char c in livePass.ToCharArray())
        {
            password.AppendChar(c);
        }
        PSCredential psc = new PSCredential(username, password);
        WSManConnectionInfo rri = new WSManConnectionInfo(new Uri(uri), schema, psc);
        rri.AuthenticationMechanism = AuthenticationMechanism.Kerberos;
        rri.ProxyAuthentication = AuthenticationMechanism.Negotiate;
        remoteRunspace = RunspaceFactory.CreateRunspace(rri);
        remoteRunspace.Open();
    }

【问题讨论】:

  • 你给Invoke打了2次电话,为什么?

标签: c# sharepoint powershell sharepoint-2010 powershell-2.0


【解决方案1】:

您可以尝试检查您是否有权访问 SharePoint 数据库,要正确添加管理单元,您需要配置数据库的 ShellAccess 权限(我记得)和场管理员权限。可能您的脚本无法添加管理单元,因此无法对 SharePoint 对象模型执行任何操作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-07
    • 1970-01-01
    • 2020-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-27
    相关资源
    最近更新 更多