0

我正在尝试从 ac# 控制台应用程序在远程共享点服务器上运行 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();
    }
4

1 回答 1

1

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

于 2012-11-08T16:35:48.197 回答