2014-06-30 28 views
0

我正在寫一個工具來幾個網站的自動部署到遠程機器,我需要在網站發佈過程中管理一些IIS7池。System.Runtime.InteropServices.COMException試圖管理遠程IIS7池

我寫了一個函數來管理本地池不帶模擬,效果不錯,所以我編輯如下:

[DllImport("advapi32.DLL", SetLastError = true)] 
public static extern int LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken); 

public static void ManagePool(string command, string appPool) 
{ 
    IntPtr admin_token = default(IntPtr); 
    WindowsIdentity wid_current = WindowsIdentity.GetCurrent(); 
    WindowsIdentity wid_admin = null; 
    WindowsImpersonationContext wic = null; 
    try 
    { 
     if (LogonUser("username", "domain", "password, 9, 0, ref admin_token) != 0) 
     { 
      wid_admin = new WindowsIdentity(admin_token); 
      wic = wid_admin.Impersonate(); 

      using (DirectoryEntry appPoolEntry = new DirectoryEntry(appPool)) 
      { 
       appPoolEntry.Invoke(command, null); 
       appPoolEntry.Close(); 
      } 
     } 
    } 
    catch (System.Exception se) 
    { 
     int ret = Marshal.GetLastWin32Error(); 
     log.append("Error code: " + ret.ToString()); 
     log.append(se.Message); 
    } 
    finally 
    { 
     if (wic != null) 
     { 
      wic.Undo(); 
     } 
    } 
} 

,但是當我嘗試調用ManagePool("Stop", "IIS://172.23.231.199/W3SVC/AppPools/My Pool")我得到這個錯誤:

Unknown error (0x80005000) 
    at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) 
    at System.DirectoryServices.DirectoryEntry.Bind() 
    at System.DirectoryServices.DirectoryEntry.get_NativeObject() 
    at System.DirectoryServices.DirectoryEntry.Invoke(String methodName, Object[] args) 

如何遠程管理它們?有沒有辦法讓它工作?

回答

0

我決定用:

IntPtr admin_token = default(IntPtr); 
WindowsIdentity wid_current = WindowsIdentity.GetCurrent(); 
WindowsIdentity wid_admin = null; 
WindowsImpersonationContext wic = null; 
try 
{ 
    if (LogonUser("username", "domain", "password", 9, 0, ref admin_token) != 0) 
    { 
     wid_admin = new WindowsIdentity(admin_token); 
     wic = wid_admin.Impersonate(); 
     ServerManager.OpenRemote("123.123.123.123").ApplicationPools["My Pool"].Start(); 
    } 
} 

感謝所有回信

的幾乎壓倒性的流量:P