CreateWeb.vbs 代码

所属分类: 脚本专栏 / vbs 阅读数: 945
收藏 0 赞 0 分享
'==============================================================================
'
'  The .NET PetShop Blueprint Application WebSite Setup
'
'  File: CreateWeb.vbs
'  Date: November 10, 2001
'
'  Creates a new vdir for this project. Set vName to name of folder on disk 
'  that holds the files.
'
'==============================================================================
'
' Copyright (C) 2001 Microsoft Corporation
'
'==============================================================================
Option Explicit

dim vPath
dim scriptPath
dim vName

vName="PetShop" ' name of web to create

' *****************************************************************************
'
' 1. Create the IIS Virtual Directory
'
' *****************************************************************************
' get current path to folder and add web name to it
scriptPath = left(Wscript.ScriptFullName,len(Wscript.ScriptFullName ) -len(Wscript.ScriptName))
vPath = scriptPath & "Web"

'call to create vDir
CreateVDir(vPath)


' ----------------------------------------------------------------------------
'
' Helper Functions
'
' -----------------------------------------------------------------------------

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Creates a single Virtual Directory (code taken from mkwebdir.vbs and 
' changed for single vDir creation).
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub CreateVDir(vPath)

    Dim vRoot,vDir,webSite
    On Error Resume Next

    ' get the local host default web
    set webSite = findWeb("localhost", "Default Web Site")
    if IsObject(webSite)=False then
        Display "Unable to locate the Default Web Site"
        exit sub
    else
        'display webSite.name
    end if

    ' get the root
    set vRoot = webSite.GetObject("IIsWebVirtualDir", "Root")
    If (Err <> 0) Then
        Display "Unable to access root for " & webSite.ADsPath
        Exit sub
    else
        'display vRoot.name
    End IF

    ' delete existing web if needed
    vRoot.Delete "IIsWebVirtualDir",vName
    vRoot.SetInfo
    Err=0 ' reset error 

    ' create the new web
    Set vDir = vRoot.Create("IIsWebVirtualDir",vName)
    If (Err <> 0) Then
        Display "Unable to create " & vRoot.ADsPath & "/" & vName & "."
        exit sub
    else
        'display vdir.name
    end if

    ' set properties on the new web 
    vDir.AccessRead = true
    vDir.Path = vPath
    vDir.Accessflags = 529
        VDir.AppCreate False
    If (Err <> 0) Then
        Display "Unable to bind path " & vPath & " to " & vRoot.Name & "/" & vName & ". Path may be invalid."
        exit sub
    end If

    ' commit changes
    vDir.SetInfo
    If (Err <> 0) Then
        Display "Unable to save changes for " & vRoot.Name & "/" & vName & "."
        exit sub
    end if

    ' report all ok
    WScript.Echo Now & " " & vName & " virtual directory " & vRoot.Name & "/" & vname & " created successfully."
End Sub

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Finds the specified web.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function findWeb(computer, webname)
    On Error Resume Next

    Dim websvc, site
    dim webinfo
    Dim aBinding, binding

    set websvc = GetObject("IIS://"&computer&"/W3svc")
    if (Err <> 0) then
        exit function
    end if
    ' First try to open the webname.
    set site = websvc.GetObject("IIsWebServer", webname)
    if (Err = 0) and (not isNull(site)) then
        if (site.class = "IIsWebServer") then
            ' Here we found a site that is a web server.
            set findWeb = site
            exit function
        end if
    end if
    err.clear
    for each site in websvc
        if site.class = "IIsWebServer" then
            '
            ' First, check to see if the ServerComment
            ' matches
            '
            If site.ServerComment = webname Then
                set findWeb = site
                exit function
            End If
            aBinding=site.ServerBindings
            if (IsArray(aBinding)) then
                if aBinding(0) = "" then
                    binding = Null
                else
                    binding = getBinding(aBinding(0))
                end if
            else 
                if aBinding = "" then
                    binding = Null
                else
                    binding = getBinding(aBinding)
                end if
            end if
            if IsArray(binding) then
                if (binding(2) = webname) or (binding(0) = webname) then
                    set findWeb = site
                    exit function
                End If
            end if 
        end if
    next
End Function

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Gets binding info.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
function getBinding(bindstr)

    Dim one, two, ia, ip, hn

    one=Instr(bindstr,":")
    two=Instr((one+1),bindstr,":")

    ia=Mid(bindstr,1,(one-1))
    ip=Mid(bindstr,(one+1),((two-one)-1))
    hn=Mid(bindstr,(two+1))

    getBinding=Array(ia,ip,hn)
end function

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Displays error message.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub Display(Msg)
    WScript.Echo Now & ". Error Code: " & Hex(Err) & " - " & Msg
End Sub

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Display progress/trace message.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub Trace(Msg)
    WScript.Echo Now & " : " & Msg  
End Sub

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' Remove the web.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub DeleteWeb(WebServer, WebName)
    ' delete the exsiting web (ignore error if missing)
    On Error Resume Next
    Dim vDir
    display "deleting " & WebName

    WebServer.Delete "IISWebVirtualDir",WebName
    WebServer.SetInfo
    If Err=0 Then
        DISPLAY "WEB " & WebName & " deleted."
    else
        display "can't find " & webname
    End If

End Sub
更多精彩内容其他人还在看

vbscript Split函数用法详解(字符串转数组函数)

本文详细介绍了vbscript中split函数的用法,有关split函数的一些实例,vbscript中split函数的语法介绍,有需要的朋友参考下
收藏 0 赞 0 分享

VBS实现将Excel表格保存为txt文本

希望能够找到个能给excel表另存为TXT的VBS代码,虽然另存为可以选择,但还是需要直接VBS执行这一步另存为TXT格式的,应该如何写代码呢
收藏 0 赞 0 分享

vbs解决处理TXT文本数据相关问题实现代码

使用vbs处理txt数据时,会遇到一些问题,本文将提供详细的解决方法,希望可以帮助你们
收藏 0 赞 0 分享

关于vbs 生成静态页面过程中出现的问题

访问某个aspx页面,然后把服务器返回的页面以filestream 的形式保存为html格式,接下来将介绍实现代码
收藏 0 赞 0 分享

dim函数第三个参数设置截取字符的长度问题

dim函数的第三个参数,也就是截取字符的长度,我在设置这个的时候,出了些问题,提示无效的过程调用或参数,本文详细介绍如何解决
收藏 0 赞 0 分享

邪恶的eval和new Function使用介绍

这些代码可以干什么?可以肯定的是可以干很多猥琐的事
收藏 0 赞 0 分享

WScript.Shell对象SpecialFolders属性未公开文档分享

WshShell对象的SpecialFolders属性返WshSpecialFolders 对象,该对象是一个特殊文件夹集合,其中包含整套Windows特殊文件夹
收藏 0 赞 0 分享

用VBS修改(设置)系统时间和日期的代码

用 VBS 把系统的时间修改到2038年1月19日3时14分07秒之后,这样某些依赖于 Unix 时间戳的程序就会出问题。那么怎样用 VBS 修改系统的时间呢
收藏 0 赞 0 分享

VBS For Next循环的一些细节

For Next 语句是 VBS 中常用的循环语句,你可能已经用过无数次 For Next 循环,但是你是否注意过它的一些细节呢
收藏 0 赞 0 分享

VBS For Next循环的陷阱分享

在《VBS For Next循环的一些细节》中说到了 For Next 循环的控制变量仅仅在循环开始前求值一次,但是其中存在一处陷阱
收藏 0 赞 0 分享
查看更多