[英]check status application pool iis7 with csharp (access-denied)
我需要從同一域中的另一台機器監視 IIS 7 應用程序池中的應用程序狀態。 我的監控應用程序必須使用 C# 並作為 Windows 服務運行。
在我的服務器上,我創建了一個具有管理權限的用戶,並執行成功運行的命令aspnet_regiis -ga machine\\username
。
我的問題是當我嘗試訪問應用程序池時,我仍然收到 COMExcepttion“拒絕訪問”。 我做錯了什么或者我錯過了哪一步?
我使用來自http://patelshailesh.com/index.php/create-a-website-application-pool-programmatically-using-csharp 的代碼作為示例。
int status = 0;
string ipAddress = "10.20.2.13";
string username = "username";
string password = "password";
try
{
DirectoryEntry de = new DirectoryEntry(string.Format("IIS://{0}/W3SVC/AppPools/MyAppPoolName", ipAddress), username, password);
//the exception is thrown here.
status = (int)de.InvokeGet("AppPoolState");
switch (status)
{
case 2:
//Running
break;
case 4:
//Stopped
break;
default:
break;
}
}
catch (Exception ex)
{
}
您找到的代碼似乎適用於 IIS6。 也許您最好使用新的受支持的 IIS7 管理 API。 您可以首先調用ServerManager.OpenRemote來獲取ServerManager對象。
您可能需要處理AuthenticationType ,從 2.0 開始的默認值是 Secure 但您可能需要設置 SSL。 此外,我已經看到來自帳戶的訪問被拒絕消息,其中“用戶必須在下次登錄時更改密碼”被選中。
這在 Windows 7 和 Windows Server 2008 上運行良好(不幸的是不適用於 XP 和 2003 Server)。 我必須通過服務器管理器在 IIS 中添加管理服務角色才能啟用遠程連接。
這是一個關於如何獲取應用程序池狀態的簡短示例。
public ObjectState State
{
get
{
ServerManager server = null;
ObjectState result = ObjectState.Unknown;
try
{
server = ServerManager.OpenRemote(address);
result = server.ApplicationPools[name].State;
}
finally
{
if (server != null)
server.Dispose();
}
return result;
}
}
感謝德里斯。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.