尝试编程访问IIS

终于有个机会需要编程访问IIS了,目的是对所有的IIS站点的目录分别压缩成一个RAR文件,方便备份到另外的地方去。

尝试了两种方式,一是用js调用ADSI,二是用C#引用System.DirectoryServices调用ADSI。最后用js写了相对完整的代码实现,如下所示。

ExpandedBlockStart.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  ); 


 如果是用C#引用System.DirectoryServices,则如下所示。

ExpandedBlockStart.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 " ));
                }
            }
        }
    }
}


参考的是MSDN中的IIS Programmatic Administration SDK, js写法相对简单,C#调用则要注意用InvokeGet获取属性更好。从长远来看,可以将IIS各个站点的IP、域名、主目录地址等信息都直接列出来,方便管理统计,否则一个一个去点开来看就太麻烦了,也不利于管控。

 

转载于:https://www.cnblogs.com/StevenLi/archive/2010/04/27/1722215.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值