通过脚本为IIS创建通配符映射

使用David Wang提供的脚本代码,可以方便地为IIS创建通配符映射。

ContractedBlock.gif ExpandedBlockStart.gif chglist.vbs
None.gif'
None.gif'
 Allows append/insert/remove of specific elements from an IIS "List" type node
None.gif'
 i.e. ScriptMap, HttpError, ServerBindings
None.gif'
None.gif'
 Origin : http://blogs.msdn.com/David.Wang/archive/2004/12/02/273681.aspx
None.gif'
 Version: December 1 2004
None.gif'
None.gif
Option Explicit
None.gif
On Error Resume Next
None.gif
None.gif
const ERROR_SUCCESS             = 0
None.gif
const ERROR_PATH_NOT_FOUND      = 3
None.gif
const ERROR_INVALID_PARAMETER   = 87
None.gif
const LIST_OP_FIRST             = "FIRST"
None.gif
const LIST_OP_LAST              = "LAST"
None.gif
const LIST_OPTION_REPLACE       = 0
None.gif
const LIST_OPTION_INSERT        = 1
None.gif
const LIST_OPTION_REMOVE        = 2
None.gif
const LIST_OPTION_ALL           = 4
None.gif
const LIST_OPTION_RECURSE       = 8
None.gif
None.gif
Dim CRLF
None.gifCRLF 
= CHR(13& CHR(10)
None.gif
Dim strHelp
None.gifstrHelp 
= "Edit/Replace IIS metabase LIST properties" & CRLF &_
None.gif          CRLF 
&_
None.gif          WScript.ScriptName 
& " PropertyPath ExistValue NewValue [Options]" & CRLF &_
None.gif          CRLF 
&_
None.gif          
"Where:" & CRLF &_
None.gif          
"    PropertyPath IIS metabase property path whose data type is LIST." & CRLF &_
None.gif          
"                 i.e. W3SVC/ScriptMaps, W3SVC/HttpErrors" & CRLF &_
None.gif          
"    ExistValue   Value to case-insensitive literal match against existing" & CRLF &_
None.gif          
"                 LIST elements." & CRLF &_
None.gif          
"        FIRST    - matches the first LIST element." & CRLF &_
None.gif          
"        LAST     - matches the last LIST element." & CRLF &_
None.gif          
"    NewValue     New value that replaces the matched the LIST element." & CRLF &_
None.gif          
"Options:" & CRLF &_
None.gif          
"    /INSERT      Insert  before LIST element matching ." & CRLF &_
None.gif          
"    /REMOVE      Remove LIST element matching ." & CRLF &_
None.gif          
"    /ALL         Operate on ALL matching . Default is first match." & CRLF &_
None.gif          
"    /REGEXP      Use  as RegExp to match. Default is literal." & CRLF &_
None.gif          
"    /RECURSE     Recursively perform the operation underneath ." & CRLF &_
None.gif          
"    /VERBOSE     Give more status/output." & CRLF &_
None.gif          
"    /COMMIT      Actually perform changes. Default only shows." & CRLF &_
None.gif          
""
None.gif
None.gif
dim Debug
None.gifDebug 
= true
None.gif
dim Verbose
None.gifVerbose 
= false
None.gif
dim reMatch
None.gifreMatch 
= false
None.gif
None.gif
Dim strServer
None.gif
Dim strNamespace
None.gif
Dim strSchemaNamespace
None.gif
Dim strNodeSyntax
None.gif
Dim objNode
None.gif
None.gif
Dim nOperationType
None.gif
Dim strNormalizedPath
None.gif
Dim strPropertyPath
None.gif
Dim strPropertyName
None.gif
Dim strPropertyExistValue
None.gif
Dim strPropertyNewValue
None.gif
None.gif
Dim i,j
None.gif
None.gif
'
None.gif'
 Start of script
None.gif'
None.gif
strServer = "localhost"
None.gifstrNamespace 
= "IIS://" & strServer
None.gifstrSchemaNamespace 
= strNamespace & "/" & "Schema"
None.gif
None.gif
'
None.gif'
 Parse the commandline
None.gif'
None.gif
If WScript.Arguments.Count < 3 Then
None.gif    Err.Number 
= ERROR_INVALID_PARAMETER
None.gif    HandleError 
"Insufficient number of arguments." & CRLF &_
None.gif                CRLF 
&_
None.gif                strHelp 
&_
None.gif                
""
None.gif
End If
None.gif
None.gifnOperationType 
= LIST_OPTION_REPLACE
None.gif
None.gif
For i = 0 To WScript.Arguments.Count - 1
None.gif    
Select Case UCase( WScript.Arguments( i ) )
None.gif        
Case "/INSERT"
None.gif            nOperationType 
= nOperationType Or LIST_OPTION_INSERT
None.gif        
Case "/REMOVE"
None.gif            nOperationType 
= nOperationType Or LIST_OPTION_REMOVE
None.gif        
Case "/ALL"
None.gif            nOperationType 
= nOperationType Or LIST_OPTION_ALL
None.gif        
Case "/RECURSE"
None.gif            nOperationType 
= nOperationType Or LIST_OPTION_RECURSE
None.gif        
Case "/COMMIT"
None.gif            Debug 
= false
None.gif        
Case "/VERBOSE"
None.gif            Verbose 
= true
None.gif        
Case "/REGEXP"
None.gif            reMatch 
= true
None.gif        
Case Else
None.gif            
If ( i = 0 ) Then
None.gif                
'
None.gif
                ' Split out PropertyName and its ParentPath from PropertyPath
None.gif
                '
None.gif
                Err.Clear
None.gif                strNormalizedPath 
= NormalizePath( WScript.Arguments( 0 ) )
None.gif                HandleError 
"Failed to normalize PropertyPath."
None.gif
None.gif                j 
= InstrRev( strNormalizedPath, "/"-10 )
None.gif
None.gif                
If ( j = 0 Or j = 1 ) Then
None.gif                    Err.Number 
= ERROR_PATH_NOT_FOUND
None.gif                    HandleError 
"Invalid PropertyPath."
None.gif                
End If
None.gif
None.gif                Err.Clear
None.gif                strPropertyPath 
= NormalizePath( Mid( strNormalizedPath, 1, j - 1 ) )
None.gif                HandleError 
"Failed to retrieve/normalize PropertyPath."
None.gif
None.gif                Err.Clear
None.gif                strPropertyName 
= NormalizePath( Mid( strNormalizedPath, j + 1 ) )
None.gif                HandleError 
"Failed to retrieve/normalize PropertyName."
None.gif            
ElseIf ( i = 1 ) Then
None.gif                
'
None.gif
                ' The existing match value
None.gif
                '
None.gif
                strPropertyExistValue = ReplaceUCase( WScript.Arguments( 1 ) ), "``""""" )
None.gif            
ElseIf ( i = 2 ) Then
None.gif                
'
None.gif
                ' The new replace value
None.gif
                '
None.gif
                strPropertyNewValue = Replace( WScript.Arguments( 2 ), "``""""" )
None.gif            
Else
None.gif                Err.Number 
= ERROR_INVALID_PARAMETER
None.gif                HandleError 
"Unknown parameter " & WScript.Arguments( i ) & CRLF &_
None.gif                            CRLF 
&_
None.gif                            strHelp 
&_
None.gif                            
""
None.gif            
End If
None.gif    
End Select
None.gif
Next
None.gif
None.gifLogVerbose 
"OpType       = " & nOperationType
None.gifLogVerbose 
"PropertyPath = " & strPropertyPath
None.gifLogVerbose 
"PropertyName = " & strPropertyName
None.gifLogVerbose 
"ExistValue   = " & strPropertyExistValue
None.gifLogVerbose 
"NewValue     = " & strPropertyNewValue
None.gif
None.gif
'
None.gif'
 Check the data type for the given property
None.gif'
 If it is not LIST, do not process any further
None.gif'
None.gif
Err.Clear
None.gif
Set objNode = GetObject( strSchemaNamespace & "/" & strPropertyName )
None.gifHandleError 
"Cannot read schema for property " & strPropertyName
None.gifstrNodeSyntax 
= UCase( objNode.Syntax )
None.gif
None.gifLogVerbose 
"Syntax       = " & strNodeSyntax
None.gifLogVerbose 
""
None.gif
None.gif
Select Case strNodeSyntax
None.gif    
Case "LIST"
None.gif        
'
None.gif
        ' Finally, we are ready to do some real work
None.gif
        '
None.gif
        Err.Clear
None.gif        Err.Number 
= HandleListOps( nOperationType, strPropertyPath, strPropertyName, strPropertyExistValue, strPropertyNewValue, ( nOperationType And LIST_OPTION_RECURSE ) <> 0 )
None.gif        HandleError 
""
None.gif    
Case Else
None.gif        Err.Clear
None.gif        Err.Number 
= ERROR_PATH_NOT_FOUND
None.gif        HandleError 
"Cannot handle " & strPropertyPath & "/" & strPropertyName & " with type " & strNodeSyntax
None.gif
End Select
None.gif
None.gif
'
None.gif'
 End of script
None.gif'
None.gif

None.gif
'
None.gif'
 Sub routines and functions
None.gif'
None.gif
Sub HandleError( errorDescription )
None.gif    
If ( Err.Number <> 0 ) Then
None.gif        
If ( IsEmpty( errorDescription ) ) Then
None.gif            LogEcho Err.Description
None.gif        
Else
None.gif            LogEcho errorDescription
None.gif        
End If
None.gif
None.gif        WScript.Quit Err.Number
None.gif    
End If
None.gif
End Sub
None.gif
None.gif
Function NormalizePath( strInput )
None.gif    
'
None.gif
    ' Replace all \ with /
None.gif
    '
None.gif
    strInput = Replace( strInput, "\""/"1-1 )
None.gif
None.gif    
'
None.gif
    ' Replace all // with /
None.gif
    '
None.gif
    Do
None.gif        strInput 
= Replace( strInput, "//""/"1-1 )
None.gif    
Loop While ( Instr( strInput, "//" ) <> 0 )
None.gif
None.gif    
'
None.gif
    ' Removing leading and trailing /
None.gif
    '
None.gif
    If ( Left( strInput, 1 ) = "/" ) Then
None.gif        strInput 
= Right( strInput, Len( strInput ) - 1 )
None.gif    
End If
None.gif
None.gif    
If ( Right( strInput, 1 ) = "/" ) Then
None.gif        strInput 
= Left( strInput, Len( strInput ) - 1 )
None.gif    
End If
None.gif
None.gif    NormalizePath 
= strInput
None.gif
End Function
None.gif
None.gif
Function HandleListOps( OpType, strPropertyPath, strPropertyName, strPropertyExistValue, strPropertyNewValue, bRecurse )
None.gif    
On Error Resume Next
None.gif    
Dim objNode, objNodeAttribute
None.gif    
Dim objList
None.gif    
Dim objElement
None.gif    
Dim objNewArray
None.gif    
Dim PerformedOperation
None.gif    
Dim Operation
None.gif    
Dim re
None.gif    
Dim reMatched
None.gif    
Dim i, j
None.gif
None.gif    Err.Clear
None.gif    
Set objNode = GetObject( strNamespace & "/" & strPropertyPath )
None.gif    objList 
= objNode.Get( strPropertyName )
None.gif
None.gif    
If ( Err.Number <> 0 Or IsEmpty( objList ) ) Then
None.gif        LogEcho 
"Failed to retrieve " & strPropertyPath & "/" & strPropertyName
None.gif        HandleListOps 
= Err.Number
None.gif        
Exit Function
None.gif    
End If
None.gif
None.gif
None.gif    Err.Clear
None.gif    
Set objNodeAttribute = objNode.GetPropertyAttribObj(strPropertyName)
None.gif    HandleError 
"Failed to retrieve Attributes for " & strPropertyPath & "/" & strPropertyName
None.gif
None.gif    
If ( objNodeAttribute.IsInherit = true ) Then
None.gif        LogEcho strPropertyPath 
& "/" & strPropertyName & " (Inherited)"
None.gif
None.gif        
If ( bRecurse = true ) Then
None.gif            LogEcho( 
"Ignoring inherited property for Recursive Modification" )
None.gif            
Exit Function
None.gif        
End If
None.gif    
Else
None.gif        LogEcho strPropertyPath 
& "/" & strPropertyName
None.gif    
End If
None.gif
None.gif
None.gif    
'
None.gif
    ' j is the count of elements in objNewArray
None.gif
    ' So that we can resize it to the right size in the end
None.gif
    '
None.gif
    j = 0
None.gif
None.gif    
'
None.gif
    ' Size objNewArray to maximum possible size up-front, later shrink it
None.gif
    '
None.gif
    Redim objNewArray( UBound( objList ) + UBound( objList ) + 1 )
None.gif
None.gif    
'
None.gif
    ' PerformedOperation indicates whether something has matched and already
None.gif
    ' operated upon, in this session.  Start with 'not yet' = 0
None.gif
    '
None.gif
    PerformedOperation = 0
None.gif
None.gif    
'
None.gif
    ' Setup the RegExp match based on the existing value to search for
None.gif
    '
None.gif
    Set re = new RegExp
None.gif    re.Pattern 
= strPropertyExistValue
None.gif    re.IgnoreCase 
= true
None.gif    re.Global 
= true
None.gif
None.gif    
'
None.gif
    ' Do this test outside of IF conditional because on error resume next
None.gif
    ' turns off failures due to incorrect Pattern
None.gif
    '
None.gif
    Err.Clear
None.gif    reMatched 
= re.Test( objElement )
None.gif    
If ( Err.Number <> 0 Or reMatch = false ) Then
None.gif        reMatched 
= false
None.gif    
End If
None.gif
None.gif    LogVerbose 
"Original:"
None.gif
None.gif    
For i = LBound( objList ) To UBound( objList )
None.gif        objElement 
= objList( i )
None.gif        
'LogVerbose i & "(" & j & ")" & ": " & objElement
None.gif

None.gif        
If ( ( ( ( strPropertyExistValue = LIST_OP_FIRST ) And ( i = LBound( objList ) ) ) Or _
None.gif               ( ( strPropertyExistValue 
= LIST_OP_LAST  ) And ( i = UBound( objList ) ) ) Or _
None.gif               ( ( reMatch 
= false ) And ( InstrUCase( objElement ), strPropertyExistValue ) > 0 ) ) Or _
None.gif               ( reMatched 
= true ) _
None.gif             ) _
None.gif             
And _
None.gif             ( ( ( OpType 
And LIST_OPTION_ALL ) <> 0 ) Or ( PerformedOperation = 0 ) ) _
None.gif           ) 
Then
None.gif            Operation 
= "Replace "
None.gif
None.gif            
If ( ( OpType And LIST_OPTION_REMOVE ) <> 0 ) Then
None.gif                
'Don't copy this element for deletion
None.gif
                Operation = "Remove "
None.gif            
Else
None.gif                objNewArray( j ) 
= strPropertyNewValue
None.gif                j 
= j + 1
None.gif
None.gif                
If ( ( OpType And LIST_OPTION_INSERT ) <> 0 ) Then
None.gif                    Operation 
= "Insert "
None.gif                    objNewArray( j ) 
= objElement
None.gif                    j 
= j + 1
None.gif                
End If
None.gif            
End If
None.gif
None.gif            PerformedOperation 
= 1
None.gif        
Else
None.gif            Operation 
= ""
None.gif            objNewArray( j ) 
= objElement
None.gif            j 
= j + 1
None.gif        
End If
None.gif
None.gif        LogVerbose Operation 
& objElement
None.gif    
Next
None.gif
None.gif    
'
None.gif
    ' Resize the final array to the correct size prior to SetInfo
None.gif
    '
None.gif
    ReDim Preserve objNewArray( j - 1 )
None.gif
None.gif    LogVerbose 
"New:"
None.gif
None.gif    
For i = LBound( objNewArray ) To UBound( objNewArray )
None.gif        LogDebug i 
& "" & objNewArray( i )
None.gif    
Next
None.gif
None.gif    
If ( Debug = false ) Then
None.gif        
If ( PerformedOperation = 1 ) Then
None.gif            Err.Clear
None.gif            objNode.Put strPropertyName, objNewArray
None.gif            objNode.SetInfo
None.gif            HandleError 
"Failed to SetInfo " & strPropertyPath & "/" & strPropertyName
None.gif            LogEcho 
"SUCCESS: Updated " & strPropertyPath & "/" & strPropertyName
None.gif        
Else
None.gif            LogEcho 
"SUCCESS: Nothing to update"
None.gif        
End If
None.gif    
Else
None.gif        
If ( PerformedOperation = 1 ) Then
None.gif            LogEcho 
"DEBUG: Matched. Did not SetInfo"
None.gif        
Else
None.gif            LogEcho 
"SUCCESS: No Match. Did not SetInfo"
None.gif        
End If
None.gif    
End If
None.gif
None.gif    
If ( bRecurse = true ) Then
None.gif        
For Each objElement In objNode
None.gif            LogEcho 
""
None.gif            HandleListOps 
= HandleListOps( OpType, NormalizePath( Mid( objElement.AdsPath, Len( strNamespace ) + 1 ) ), strPropertyName, strPropertyExistValue, strPropertyNewValue, bRecurse )
None.gif        
Next
None.gif    
End If
None.gif
None.gif    HandleListOps 
= 0
None.gif
End Function
None.gif
None.gif
Sub LogEcho( str )
None.gif    WScript.Echo str
None.gif
End Sub
None.gif
None.gif
Sub LogDebug( str )
None.gif    
If ( Debug = true ) Then
None.gif        LogEcho str
None.gif    
End If
None.gif
End Sub
None.gif
None.gif
Sub LogVerbose( str )
None.gif    
If ( Verbose = true ) Then
None.gif        LogEcho str
None.gif    
End If
None.gif
End Sub

使用示例:
chglist.vbs W3SVC/1/root/ScriptMaps "" "*,C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\aspnet_isapi.dll,0" /INSERT /COMMIT
注:其中1是站点的id。

原文: http://blogs.msdn.com/David.Wang/archive/2004/12/02/273681.aspx

转载于:https://www.cnblogs.com/dudu/archive/2007/02/28/659557.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值