尝试编程访问IIS
终于有个机会需要编程访问IIS了,目的是对所有的IIS站点的目录分别压缩成一个RAR文件,方便备份到另外的地方去。
尝试了两种方式,一是用js调用ADSI,二是用C#引用System.DirectoryServices调用ADSI。最后用js写了相对完整的代码实现,如下所示。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
var WinRARexe="C:\\Program Files\\WinRAR\\rar.exe"
var WshShell = WScript.CreateObject("WScript.Shell");
var fso = new ActiveXObject("Scripting.FileSystemObject");
if (!fso.FileExists(WinRARexe))
{
WScript.Quit( 0 );
}
var sfn=WScript.ScriptFullName;
var sn=WScript.ScriptName;
/*获得脚本的当前路径,设置为备份的目录*/
var backupPath =sfn.substr(0,sfn.length-sn.length) ;
var tempFileName="";
var w3svc = GetObject( "IIS://localhost/w3svc" );
var e = new Enumerator( w3svc );
for(; ! e.atEnd(); e.moveNext() )
{
var site = e.item();
if(site.Class=="IIsWebServer")
{
WScript.Echo( "Name[" + site.Name + "]\nADsPath[" + site.ADsPath + "]\nServerComment[" + site.ServerComment + "]");
var vird=GetObject(site.ADsPath + "/root");
/*IIS站点目录*/
WScript.Echo("WebSiteRoot[" + vird.Path + "]");
tempFileName=site.ServerComment+"["+site.Name+"]"
WScript.Echo(tempFileName);
/*写命令*/
//WshShell.Run("\"" + WinRARexe + "\" a -r \"" + backupPath + tempFileName + ".rar\" \"" + vird.Path + "\" ",1,true);
}
}
WScript.Quit( 0 );
var WshShell = WScript.CreateObject("WScript.Shell");
var fso = new ActiveXObject("Scripting.FileSystemObject");
if (!fso.FileExists(WinRARexe))
{
WScript.Quit( 0 );
}
var sfn=WScript.ScriptFullName;
var sn=WScript.ScriptName;
/*获得脚本的当前路径,设置为备份的目录*/
var backupPath =sfn.substr(0,sfn.length-sn.length) ;
var tempFileName="";
var w3svc = GetObject( "IIS://localhost/w3svc" );
var e = new Enumerator( w3svc );
for(; ! e.atEnd(); e.moveNext() )
{
var site = e.item();
if(site.Class=="IIsWebServer")
{
WScript.Echo( "Name[" + site.Name + "]\nADsPath[" + site.ADsPath + "]\nServerComment[" + site.ServerComment + "]");
var vird=GetObject(site.ADsPath + "/root");
/*IIS站点目录*/
WScript.Echo("WebSiteRoot[" + vird.Path + "]");
tempFileName=site.ServerComment+"["+site.Name+"]"
WScript.Echo(tempFileName);
/*写命令*/
//WshShell.Run("\"" + WinRARexe + "\" a -r \"" + backupPath + tempFileName + ".rar\" \"" + vird.Path + "\" ",1,true);
}
}
WScript.Quit( 0 );
如果是用C#引用System.DirectoryServices,则如下所示。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System;
using System.Collections.Generic;
using System.DirectoryServices;
using System.Text;
namespace IIScfg
{
class Program
{
static void Main(string[] args)
{
DirectoryEntry siteRoot;
DirectoryEntry w3svc = new DirectoryEntry("IIS://localhost/w3svc");
foreach (DirectoryEntry cc in w3svc.Children)
{
if ((string)cc.InvokeGet("Class") == "IIsWebServer")
{
Console.WriteLine("Name[{0}]\nPath[{1}]", cc.Name, cc.Path);
Console.WriteLine("---{0}", (string)cc.InvokeGet("ServerComment"));
siteRoot = new DirectoryEntry(cc.Path + "/Root");
Console.WriteLine("---{0}", (string)siteRoot.InvokeGet("Path"));
}
}
}
}
}
using System.Collections.Generic;
using System.DirectoryServices;
using System.Text;
namespace IIScfg
{
class Program
{
static void Main(string[] args)
{
DirectoryEntry siteRoot;
DirectoryEntry w3svc = new DirectoryEntry("IIS://localhost/w3svc");
foreach (DirectoryEntry cc in w3svc.Children)
{
if ((string)cc.InvokeGet("Class") == "IIsWebServer")
{
Console.WriteLine("Name[{0}]\nPath[{1}]", cc.Name, cc.Path);
Console.WriteLine("---{0}", (string)cc.InvokeGet("ServerComment"));
siteRoot = new DirectoryEntry(cc.Path + "/Root");
Console.WriteLine("---{0}", (string)siteRoot.InvokeGet("Path"));
}
}
}
}
}
参考的是MSDN中的IIS Programmatic Administration SDK, js写法相对简单,C#调用则要注意用InvokeGet获取属性更好。从长远来看,可以将IIS各个站点的IP、域名、主目录地址等信息都直接列出来,方便管理统计,否则一个一个去点开来看就太麻烦了,也不利于管控。
![](http://www.cnblogs.com/Emoticons/msn/bat.gif)