Microsoft KB Archive/254808: Difference between revisions

From BetaArchive Wiki
m (Text replacement - "<" to "<")
m (Text replacement - """ to """)
 
(2 intermediate revisions by the same user not shown)
Line 73: Line 73:
' This is a simple script to create a new virtual FTP directory.
' This is a simple script to create a new virtual FTP directory.
'
'
' Call this script with &quot;-?&quot; for usage instructions
' Call this script with "-?" for usage instructions
'
'
'------------------------------------------------------------
'------------------------------------------------------------
Line 88: Line 88:


Set oArgs = WScript.Arguments
Set oArgs = WScript.Arguments
ArgComputers = Array(&quot;LocalHost&quot;)
ArgComputers = Array("LocalHost")


ArgNum = 0
ArgNum = 0
While ArgNum < oArgs.Count
While ArgNum < oArgs.Count


   If (ArgNum + 1) &gt;= oArgs.Count Then
   If (ArgNum + 1) >= oArgs.Count Then
     Call DisplayUsage
     Call DisplayUsage
   End If
   End If


   Select Case LCase(oArgs(ArgNum))
   Select Case LCase(oArgs(ArgNum))
     Case &quot;--computer&quot;, &quot;-c&quot;:
     Case "--computer", "-c":
       ArgNum = ArgNum + 1
       ArgNum = ArgNum + 1
       ArgComputers = Split(oArgs(ArgNum), &quot;,&quot;, -1)
       ArgComputers = Split(oArgs(ArgNum), ",", -1)
     Case &quot;--ftpsite&quot;, &quot;-f&quot;:
     Case "--ftpsite", "-f":
       ArgNum = ArgNum + 1
       ArgNum = ArgNum + 1
       ArgFtpSites = oArgs(ArgNum)
       ArgFtpSites = oArgs(ArgNum)
     Case &quot;--virtualdir&quot;, &quot;-v&quot;:
     Case "--virtualdir", "-v":
       ArgNum = ArgNum + 1
       ArgNum = ArgNum + 1
       ArgVirtualDirs = Split(oArgs(ArgNum), &quot;,&quot;, -1)
       ArgVirtualDirs = Split(oArgs(ArgNum), ",", -1)
     Case &quot;--help&quot;, &quot;-?&quot;
     Case "--help", "-?"
       Call DisplayUsage
       Call DisplayUsage
   End Select
   End Select
Line 123: Line 123:
   While ArgNum <= UBound(ArgVirtualDirs)
   While ArgNum <= UBound(ArgVirtualDirs)
     ArgDirNames(DirIndex) = ArgVirtualDirs(ArgNum)
     ArgDirNames(DirIndex) = ArgVirtualDirs(ArgNum)
     If (ArgNum + 1) &gt; UBound(ArgVirtualDirs) Then
     If (ArgNum + 1) > UBound(ArgVirtualDirs) Then
       WScript.Echo &quot;Error understanding virtual directories&quot;
       WScript.Echo "Error understanding virtual directories"
       Call DisplayUsage
       Call DisplayUsage
     End If
     End If
Line 134: Line 134:
End If
End If


If (ArgFtpSites = &quot;&quot;) Or (IsArray(ArgDirNames) = False Or IsArray(ArgDirPaths) = False) Then
If (ArgFtpSites = "") Or (IsArray(ArgDirNames) = False Or IsArray(ArgDirPaths) = False) Then
   Call DisplayUsage
   Call DisplayUsage
Else
Else
Line 146: Line 146:


Sub Display(Msg)
Sub Display(Msg)
   WScript.Echo Now &amp; &quot;. Error Code: &quot; &amp; Hex(Err) &amp; &quot; - &quot; &amp; Msg
   WScript.Echo Now & ". Error Code: " & Hex(Err) & " - " & Msg
End Sub
End Sub


Sub Trace(Msg)
Sub Trace(Msg)
   WScript.Echo Now &amp; &quot; : &quot; &amp; Msg
   WScript.Echo Now & " : " & Msg
End Sub
End Sub


Sub DisplayUsage()
Sub DisplayUsage()
   WScript.Echo &quot;Usage: mkftpdir [--computer|-c COMPUTER1,COMPUTER2]&quot;
   WScript.Echo "Usage: mkftpdir [--computer|-c COMPUTER1,COMPUTER2]"
   WScript.Echo &quot;               <--ftpsite|-f FTPSITE1&gt;&quot;
   WScript.Echo "               <--ftpsite|-f FTPSITE1>"
   WScript.Echo &quot;               <--virtualdir|-v NAME1,PATH1,NAME2,PATH2,...&gt;&quot;
   WScript.Echo "               <--virtualdir|-v NAME1,PATH1,NAME2,PATH2,...>"
   WScript.Echo &quot;               [--help|-?]&quot;
   WScript.Echo "               [--help|-?]"


   WScript.Echo &quot;&quot;
   WScript.Echo ""
   WScript.Echo &quot;Note, FTPSITE is the Ftp Site on which the directory will be created.&quot;
   WScript.Echo "Note, FTPSITE is the Ftp Site on which the directory will be created."
   WScript.Echo &quot;The name can be specified as one of the following, in the priority specified:&quot;
   WScript.Echo "The name can be specified as one of the following, in the priority specified:"
   WScript.Echo &quot; Server Number (i.e. 1, 2, 10, etc.)&quot;
   WScript.Echo " Server Number (i.e. 1, 2, 10, etc.)"
   WScript.Echo &quot; Server Description (i.e &quot;&quot;My Server&quot;&quot;)&quot;
   WScript.Echo " Server Description (i.e ""My Server"")"
   WScript.Echo &quot; Server Host name (i.e. &quot;&quot;ftp.domain.com&quot;&quot;)&quot;
   WScript.Echo " Server Host name (i.e. ""ftp.domain.com"")"
   WScript.Echo &quot; IP Address (i.e., 127.0.0.1)&quot;
   WScript.Echo " IP Address (i.e., 127.0.0.1)"
   WScript.Echo &quot;&quot;
   WScript.Echo ""
   WScript.Echo &quot;&quot;
   WScript.Echo ""
   WScript.Echo &quot;Example : mkftpdir -c MyComputer -f &quot;&quot;Default Ftp Site&quot;&quot;&quot;
   WScript.Echo "Example : mkftpdir -c MyComputer -f ""Default Ftp Site"""
   WScript.Echo &quot;           -v &quot;&quot;dir1&quot;&quot;,&quot;&quot;c:\inetpub\ftproot\dir1&quot;&quot;,&quot;&quot;dir2&quot;&quot;,&quot;&quot;c:\inetpub\ftproot\dir2&quot;&quot;&quot;
   WScript.Echo "           -v ""dir1"",""c:\inetpub\ftproot\dir1"",""dir2"",""c:\inetpub\ftproot\dir2"""


   WScript.Quit
   WScript.Quit
Line 182: Line 182:
   Set ftpSite = findFtp(ComputerName, FtpSiteName)
   Set ftpSite = findFtp(ComputerName, FtpSiteName)
   If IsObject(ftpSite) Then
   If IsObject(ftpSite) Then
     Set vRoot = ftpSite.GetObject(&quot;IIsFtpVirtualDir&quot;, &quot;Root&quot;)
     Set vRoot = ftpSite.GetObject("IIsFtpVirtualDir", "Root")
     Trace &quot;Accessing root for &quot; &amp; ftpSite.ADsPath
     Trace "Accessing root for " & ftpSite.ADsPath
     If (Err <&gt; 0) Then
     If (Err <> 0) Then
       Display &quot;Unable to access root for &quot; &amp; ftpSite.ADsPath
       Display "Unable to access root for " & ftpSite.ADsPath
     Else
     Else
       DirNum = 0
       DirNum = 0
Line 191: Line 191:
         While DirNum < UBound(DirNames)
         While DirNum < UBound(DirNames)
           'Create the new virtual directory
           'Create the new virtual directory
           Set vDir = vRoot.Create(&quot;IIsFtpVirtualDir&quot;, DirNames(DirNum))
           Set vDir = vRoot.Create("IIsFtpVirtualDir", DirNames(DirNum))
           If (Err <&gt; 0) Then
           If (Err <> 0) Then
             Display &quot;Unable to create &quot; &amp; vRoot.ADsPath &amp; &quot;/&quot; &amp; DirNames(DirNum) &amp; &quot;.&quot;
             Display "Unable to create " & vRoot.ADsPath & "/" & DirNames(DirNum) & "."
           Else
           Else
             'Set the new virtual directory path
             'Set the new virtual directory path
             vDir.AccessRead = True
             vDir.AccessRead = True
             vDir.Path = DirPaths(DirNum)
             vDir.Path = DirPaths(DirNum)
             If (Err <&gt; 0) Then
             If (Err <> 0) Then
               Display &quot;Unable to bind path &quot; &amp; DirPaths(DirNum) &amp; &quot; to &quot; &amp; vRootName &amp; &quot;/&quot; &amp; DirNames(DirNum) &amp; &quot;. Path may be invalid.&quot;
               Display "Unable to bind path " & DirPaths(DirNum) & " to " & vRootName & "/" & DirNames(DirNum) & ". Path may be invalid."
             Else
             Else
               'Save the changes
               'Save the changes
               vDir.SetInfo
               vDir.SetInfo
               If (Err <&gt; 0) Then
               If (Err <> 0) Then
                 Display &quot;Unable to save configuration for &quot; &amp; vRootName &amp; &quot;/&quot; &amp; DirNames(DirNum) &amp; &quot;.&quot;
                 Display "Unable to save configuration for " & vRootName & "/" & DirNames(DirNum) & "."
               Else
               Else
                 Trace &quot;Ftp virtual directory &quot; &amp; vRootName &amp; &quot;/&quot; &amp; DirNames(DirNum) &amp; &quot; created successfully.&quot;
                 Trace "Ftp virtual directory " & vRootName & "/" & DirNames(DirNum) & " created successfully."
               End If
               End If
             End If
             End If
Line 216: Line 216:
     End If
     End If
   Else
   Else
     Display &quot;Unable to find &quot; &amp; FtpSiteName &amp; &quot; on &quot; &amp; ComputerName
     Display "Unable to find " & FtpSiteName & " on " & ComputerName
   End If
   End If
   Trace &quot;Done.&quot;
   Trace "Done."
End Sub
End Sub


Line 225: Line 225:
   Dim one, two, ia, ip, hn
   Dim one, two, ia, ip, hn
    
    
   one = InStr(bindstr, &quot;:&quot;)
   one = InStr(bindstr, ":")
   two = InStr((one + 1), bindstr, &quot;:&quot;)
   two = InStr((one + 1), bindstr, ":")
    
    
   ia = Mid(bindstr, 1, (one - 1))
   ia = Mid(bindstr, 1, (one - 1))
Line 242: Line 242:
   Dim aBinding, binding
   Dim aBinding, binding


   Set ftpsvc = GetObject(&quot;IIS://&quot; &amp; computer &amp; &quot;/MsFtpSvc&quot;)
   Set ftpsvc = GetObject("IIS://" & computer & "/MsFtpSvc")
   If (Err <&gt; 0) Then
   If (Err <> 0) Then
     Exit Function
     Exit Function
   End If
   End If
   ' First try to open the ftpname.
   ' First try to open the ftpname.
   Set site = ftpsvc.GetObject(&quot;IIsFtpServer&quot;, ftpname)
   Set site = ftpsvc.GetObject("IIsFtpServer", ftpname)
   If (Err = 0) And (Not IsNull(site)) Then
   If (Err = 0) And (Not IsNull(site)) Then
     If (site.Class = &quot;IIsFtpServer&quot;) Then
     If (site.Class = "IIsFtpServer") Then
       ' Here we found a site that is a ftp server.
       ' Here we found a site that is a ftp server.
       Set findFtp = site
       Set findFtp = site
Line 257: Line 257:
   Err.Clear
   Err.Clear
   For Each site In ftpsvc
   For Each site In ftpsvc
     If site.Class = &quot;IIsFtpServer&quot; Then
     If site.Class = "IIsFtpServer" Then
       ' First, check to see if the ServerComment matches
       ' First, check to see if the ServerComment matches
       If site.ServerComment = ftpname Then
       If site.ServerComment = ftpname Then
Line 265: Line 265:
       aBinding = site.ServerBindings
       aBinding = site.ServerBindings
       If (IsArray(aBinding)) Then
       If (IsArray(aBinding)) Then
         If aBinding(0) = &quot;&quot; Then
         If aBinding(0) = "" Then
           binding = Null
           binding = Null
         Else
         Else
Line 271: Line 271:
         End If
         End If
       Else
       Else
         If aBinding = &quot;&quot; Then
         If aBinding = "" Then
           binding = Null
           binding = Null
         Else
         Else

Latest revision as of 13:52, 21 July 2020

Knowledge Base


Article ID: 254808

Article Last Modified on 8/8/2007



APPLIES TO

  • Microsoft Internet Information Services 5.0



This article was previously published under Q254808

SUMMARY

Microsoft Internet Information Services (IIS) version 5.0 includes several sample Windows Scripting Host (WSH) administration scripts in the %SystemDrive%\InetPub\AdminScripts folder. These sample scripts perform various administrative functions. This article gives a sample script to create FTP virtual directories to complement the existing set of scripts.

MORE INFORMATION

Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific requirements.

For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:

WSH Sample ADSI Script

  1. Save the following code as Mkftpdir.vbs in your %SystemDrive%\InetPub\AdminScripts folder:

    '------------------------------------------------------------
    '
    ' This is a simple script to create a new virtual FTP directory.
    '
    ' Call this script with "-?" for usage instructions
    '
    '------------------------------------------------------------
    
    ' Force explicit declaration of all variables.
    Option Explicit
    
    On Error Resume Next
    
    Dim oArgs, ArgNum
    
    Dim ArgComputer, ArgFtpSites, ArgVirtualDirs, ArgDirNames(), ArgDirPaths(), DirIndex
    Dim ArgComputers
    
    Set oArgs = WScript.Arguments
    ArgComputers = Array("LocalHost")
    
    ArgNum = 0
    While ArgNum < oArgs.Count
    
      If (ArgNum + 1) >= oArgs.Count Then
        Call DisplayUsage
      End If
    
      Select Case LCase(oArgs(ArgNum))
        Case "--computer", "-c":
          ArgNum = ArgNum + 1
          ArgComputers = Split(oArgs(ArgNum), ",", -1)
        Case "--ftpsite", "-f":
          ArgNum = ArgNum + 1
          ArgFtpSites = oArgs(ArgNum)
        Case "--virtualdir", "-v":
          ArgNum = ArgNum + 1
          ArgVirtualDirs = Split(oArgs(ArgNum), ",", -1)
        Case "--help", "-?"
          Call DisplayUsage
      End Select
    
      ArgNum = ArgNum + 1
    Wend
    
    ArgNum = 0
    DirIndex = 0
    
    ReDim ArgDirNames((UBound(ArgVirtualDirs) + 1) \ 2)
    ReDim ArgDirPaths((UBound(ArgVirtualDirs) + 1) \ 2)
    
    If IsArray(ArgVirtualDirs) Then
      While ArgNum <= UBound(ArgVirtualDirs)
        ArgDirNames(DirIndex) = ArgVirtualDirs(ArgNum)
        If (ArgNum + 1) > UBound(ArgVirtualDirs) Then
          WScript.Echo "Error understanding virtual directories"
          Call DisplayUsage
        End If
        ArgNum = ArgNum + 1
        ArgDirPaths(DirIndex) = ArgVirtualDirs(ArgNum)
        ArgNum = ArgNum + 1
        DirIndex = DirIndex + 1
      Wend
    End If
    
    If (ArgFtpSites = "") Or (IsArray(ArgDirNames) = False Or IsArray(ArgDirPaths) = False) Then
      Call DisplayUsage
    Else
      Dim compIndex
      For compIndex = 0 To UBound(ArgComputers)
        Call ASTCreateVirtualFtpDir(ArgComputers(compIndex), ArgFtpSites, ArgDirNames, ArgDirPaths)
      Next
    End If
    
    '------------------------------------------------------------
    
    Sub Display(Msg)
      WScript.Echo Now & ". Error Code: " & Hex(Err) & " - " & Msg
    End Sub
    
    Sub Trace(Msg)
      WScript.Echo Now & " : " & Msg
    End Sub
    
    Sub DisplayUsage()
      WScript.Echo "Usage: mkftpdir [--computer|-c COMPUTER1,COMPUTER2]"
      WScript.Echo "                <--ftpsite|-f FTPSITE1>"
      WScript.Echo "                <--virtualdir|-v NAME1,PATH1,NAME2,PATH2,...>"
      WScript.Echo "                [--help|-?]"
    
      WScript.Echo ""
      WScript.Echo "Note, FTPSITE is the Ftp Site on which the directory will be created."
      WScript.Echo "The name can be specified as one of the following, in the priority specified:"
      WScript.Echo " Server Number (i.e. 1, 2, 10, etc.)"
      WScript.Echo " Server Description (i.e ""My Server"")"
      WScript.Echo " Server Host name (i.e. ""ftp.domain.com"")"
      WScript.Echo " IP Address (i.e., 127.0.0.1)"
      WScript.Echo ""
      WScript.Echo ""
      WScript.Echo "Example : mkftpdir -c MyComputer -f ""Default Ftp Site"""
      WScript.Echo "           -v ""dir1"",""c:\inetpub\ftproot\dir1"",""dir2"",""c:\inetpub\ftproot\dir2"""
    
      WScript.Quit
    End Sub
    
    '------------------------------------------------------------
    
    Sub ASTCreateVirtualFtpDir(ComputerName, FtpSiteName, DirNames, DirPaths)
      Dim computer, ftpSite, FtpSiteID, vRoot, vDir, DirNum
      On Error Resume Next
      
      Set ftpSite = findFtp(ComputerName, FtpSiteName)
      If IsObject(ftpSite) Then
        Set vRoot = ftpSite.GetObject("IIsFtpVirtualDir", "Root")
        Trace "Accessing root for " & ftpSite.ADsPath
        If (Err <> 0) Then
          Display "Unable to access root for " & ftpSite.ADsPath
        Else
          DirNum = 0
          If (IsArray(DirNames) = True) And (IsArray(DirPaths) = True) And (UBound(DirNames) = UBound(DirPaths)) Then
            While DirNum < UBound(DirNames)
              'Create the new virtual directory
              Set vDir = vRoot.Create("IIsFtpVirtualDir", DirNames(DirNum))
              If (Err <> 0) Then
                Display "Unable to create " & vRoot.ADsPath & "/" & DirNames(DirNum) & "."
              Else
                'Set the new virtual directory path
                vDir.AccessRead = True
                vDir.Path = DirPaths(DirNum)
                If (Err <> 0) Then
                  Display "Unable to bind path " & DirPaths(DirNum) & " to " & vRootName & "/" & DirNames(DirNum) & ". Path may be invalid."
                Else
                  'Save the changes
                  vDir.SetInfo
                  If (Err <> 0) Then
                    Display "Unable to save configuration for " & vRootName & "/" & DirNames(DirNum) & "."
                  Else
                    Trace "Ftp virtual directory " & vRootName & "/" & DirNames(DirNum) & " created successfully."
                  End If
                End If
              End If
              Err = 0
              DirNum = DirNum + 1
            Wend
          End If
        End If
      Else
        Display "Unable to find " & FtpSiteName & " on " & ComputerName
      End If
      Trace "Done."
    End Sub
    
    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
    
    Function findFtp(computer, ftpname)
      On Error Resume Next
    
      Dim ftpsvc, site
      Dim ftpinfo
      Dim aBinding, binding
    
      Set ftpsvc = GetObject("IIS://" & computer & "/MsFtpSvc")
      If (Err <> 0) Then
        Exit Function
      End If
      ' First try to open the ftpname.
      Set site = ftpsvc.GetObject("IIsFtpServer", ftpname)
      If (Err = 0) And (Not IsNull(site)) Then
        If (site.Class = "IIsFtpServer") Then
          ' Here we found a site that is a ftp server.
          Set findFtp = site
          Exit Function
        End If
      End If
      Err.Clear
      For Each site In ftpsvc
        If site.Class = "IIsFtpServer" Then
          ' First, check to see if the ServerComment matches
          If site.ServerComment = ftpname Then
            Set findFtp = 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) = ftpname) Or (binding(0) = ftpname) Then
              Set findFtp = site
              Exit Function
            End If
          End If
        End If
      Next
    End Function
                            
  2. Call this script with the following syntax for full usage instructions:

    CSCRIPT %SystemDrive%\InetPub\AdminScripts\MKFTPDIR.VBS -?

For more information on Microsoft scripting technologies, see the following Web site:

Keywords: kbhowto KB254808