返回首页
知识点
访问IIS目录
地址类似:IIS://ComputerName/Service/Website/Directory
ComputerName:即操作的服务器的名字,可以是名字也可以是IP,经常用的就是localhost
Service:即操作的服务器,IIS中有Web,也有FTP,还有SMTP这些服务,我们主要是操作IIS的Web功能,因此此处就是"W3SVC",如果是FTP则应是"MSFTPSVC"
WebSite:一个IIS服务中可以包括很多的站点,这个就用于设置操作的站点。他的值是一个数字,默认是1,表示缺省站点,如果有其它,则从1开始依次类推。
现有问题:目前操作远程机器会报RPC 服务器不可用 的错误,开启服务器的服务后,仍然无法解决,应该是缺少权限
知识点
IIS站点结构
string appPoolPath = @"IIS://" + System.Environment.MachineName + "/W3SVC";
DirectoryEntry entry = new DirectoryEntry(appPoolPath);
string result = "";
foreach (DirectoryEntry item in entry.Children)
{
string name = item.Name;
string schema= item.SchemaClassName;
result += string.Format("Name:{0},Schema:{1}\n", name, schema);
}
Console.WriteLine(result);
输出:
Name: FILTERS,Schema: IIsFilters
Name:APPPOOLS,Schema: IIsApplicationPools
Name:INFO,Schema: IIsWebInfo
Name:1,Schema: IIsWebServer
Name:2,Schema: IIsWebServer
知识点
获取IIS版本
try
{
DirectoryEntry entry = new DirectoryEntry("IIS://" + System.Environment.MachineName + "/W3SVC/INFO");
string result= entry.Properties["MajorIISVersionNumber"].Value.ToString();
Console.WriteLine(result);
}
catch (Exception se)
{
//说明一点:IIS5.0中没有(int)entry.Properties["MajorIISVersionNumber"].Value;属性,
//将抛出异常 证明版本为 5.0
}
输出:7
知识点
获取IIS站点虚拟目录
//获取ID为3的站点跟目录
DirectoryEntry rootfolder = new DirectoryEntry("IIS://" + System.Environment.MachineName + "/W3SVC/3/ROOT");
string result = "";
foreach (DirectoryEntry item in rootfolder.Children)
{
string name = item.Name;
string schema = item.SchemaClassName;
result += string.Format("Name:{0},Schema:{1}\n", name, schema);
}
Console.WriteLine(result);
输出:Name:Aspcn,Schema:IIsWebVirtualDir
知识点
启动IIS应用程序池
DirectoryEntry pool = new DirectoryEntry("IIS://" + System.Environment.MachineName + "/W3SVC/AppPools/iLog");
//DirectoryEntry entry = new DirectoryEntry("IIS://" + System.Environment.MachineName + "/W3SVC/AppPools");
//DirectoryEntry pool = entry.Children.Find("iLog", "IIsApplicationPool");
pool.Invoke("Start", new Object[] { });
pool.CommitChanges();
pool.Close();
如果成功:在应用程序池中,名称为iLog的程序池会启动
知识点
停止IIS应用程序池
DirectoryEntry pool = new DirectoryEntry("IIS://" + System.Environment.MachineName + "/W3SVC/AppPools/iLog");
//DirectoryEntry entry = new DirectoryEntry("IIS://" + System.Environment.MachineName + "/W3SVC/AppPools");
//DirectoryEntry pool = entry.Children.Find("iLog", "IIsApplicationPool");
pool.Invoke("Stop", new Object[] { });
pool.CommitChanges();
pool.Close();
如果成功:在应用程序池中,名称为iLog的程序池会停止
知识点
停止IIS站点
DirectoryEntry web = new DirectoryEntry("IIS://" + System.Environment.MachineName + "/W3SVC/3");
web.Invoke("Stop", new Object[] { });
web.CommitChanges();
web.Close();
如果成功:ID为3的Web站点会停止
知识点
启动IIS站点
DirectoryEntry web = new DirectoryEntry("IIS://" + System.Environment.MachineName + "/W3SVC/3");
web.Invoke("Start", new Object[] { });
web.CommitChanges();
web.Close();
如果成功:ID为3的Web站点会启动
知识点
LADP轻量目录访问协议及相关
LADP 是轻量级目录访问协议 其他名词解释:DC (Domain Component) CN(Common Name) OU(Organizational Unit)
知识点
获取活动目录单一对象
string strPath = "LDAP://10.153.0.61";
string userName = "xys2338";
string password = "qwe123@";
string user = "xys2338";
DirectoryEntry entry = new DirectoryEntry(strPath, userName, password);
DirectorySearcher search = new DirectorySearcher(entry); //创建DirectoryEntry对象的搜索对象
search.SearchRoot = entry;
search.Filter = "(SAMAccountName=" + user + ")"; //过滤条件为登录帐号=user
// search.Filter = string.Format("(&(objectClass=user)(name={0}))",user);
SearchResult sr = search.FindOne(); //查找第一个
string result = "";
if (sr != null) // 说明找到了对象
{
DirectoryEntry userEntry = sr.GetDirectoryEntry();
result = string.Format("Name:{0},Path:{1}", userEntry.Name, userEntry.Path);
}
Console.WriteLine(result);
输出:
Name:CN=xuqiang YS2338,Path:LDAP://10.153.0.61/CN=xuqiang YS2338,OU=partnerusers,DC=h3c,DC=huawei-3com,DC=com
知识点
修改活动目录用户密码
string strPath = "LDAP://10.153.0.61";
string userName = "xys2338";
string password = "qwe123@";
string user = "xys2338"; //xys2338 wkf7785_Wang1020 cys2689
DirectoryEntry entry = new DirectoryEntry(strPath, userName, password);
DirectorySearcher search = new DirectorySearcher(entry); //创建DirectoryEntry对象的搜索对象
search.SearchRoot = entry;
search.Filter = "(SAMAccountName=" + user + ")"; //过滤条件为登录帐号=user
// search.Filter = string.Format("(&(objectClass=user)(name={0}))",user);
SearchResult sr = search.FindOne(); //查找第一个
string result = "";
if (sr != null) // 说明找到了对象
{
DirectoryEntry userEntry = sr.GetDirectoryEntry();
userEntry.Invoke("ChangePassword", new object[] { "qwe123!", "qwe123@" }); //修改域用户密码 正常调用
userEntry.Close();
}
若不报错误,则修改成功
知识点
活动目录循环所有组织单元
string strPath = "LDAP://10.153.0.61";
string userName = "xys2338";
string password = "qwe123@";
DirectoryEntry entry = new DirectoryEntry(strPath, userName, password);
DirectoryEntries entries = entry.Children;
string result = "";
foreach (DirectoryEntry item in entries)
{
result += string.Format("Name:{0},Path:{1}\n", item.Name, item.Path);
}
Console.WriteLine(result);
输出样例:
Name:OU=AppUsers,Path:LDAP://10.153.0.61/OU=AppUsers,DC=h3c,DC=huawei-3com,DC=com
Name:CN=Builtin,Path:LDAP://10.153.0.61/CN=Builtin,DC=h3c,DC=huawei-3com,DC=com
Name:OU=CA,Path:LDAP://10.153.0.61/OU=CA,DC=h3c,DC=huawei-3com,DC=com
Name:OU=citrixtest,Path:LDAP://10.153.0.61/OU=citrixtest,DC=h3c,DC=huawei-3com,DC=com
知识点
获取活动目录指定的OU
//当有OU时也需要DC
string strPath = "LDAP://10.153.0.61/OU=partnerusers,DC=h3c,DC=huawei-3com,DC=com";
string userName = "xys2338";
string password = "qwe123@";
DirectoryEntry entry = new DirectoryEntry(strPath, userName, password);
DirectoryEntries entries = entry.Children;
string result = "";
foreach (DirectoryEntry item in entries)
{
result+=string.Format("Name:{0},Path:{1}\n", item.Name, item.Path);
}
Console.WriteLine(result);
输出样例:
Name:CN=anchanglong KF8666,Path:LDAP://10.153.0.61/CN=anchanglong KF8666,OU=partnerusers,DC=h3c,DC=huawei-3com,DC=com
Name:CN=andongdong HZ0339,Path:LDAP://10.153.0.61/CN=andongdong HZ0339,OU=partnerusers,DC=h3c,DC=huawei-3com,DC=com
Name:CN=Andreas gw3503,Path:LDAP://10.153.0.61/CN=Andreas gw3503,OU=partnerusers,DC=h3c,DC=huawei-3com,DC=com