Microsoft KB Archive/254808: Difference between revisions
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 | ' Call this script with "-?" for usage instructions | ||
' | ' | ||
'------------------------------------------------------------ | '------------------------------------------------------------ | ||
Line 88: | Line 88: | ||
Set oArgs = WScript.Arguments | Set oArgs = WScript.Arguments | ||
ArgComputers = Array( | ArgComputers = Array("LocalHost") | ||
ArgNum = 0 | ArgNum = 0 | ||
While ArgNum < oArgs.Count | While ArgNum < oArgs.Count | ||
If (ArgNum + 1) | 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 | Case "--computer", "-c": | ||
ArgNum = ArgNum + 1 | ArgNum = ArgNum + 1 | ||
ArgComputers = Split(oArgs(ArgNum), | ArgComputers = Split(oArgs(ArgNum), ",", -1) | ||
Case | Case "--ftpsite", "-f": | ||
ArgNum = ArgNum + 1 | ArgNum = ArgNum + 1 | ||
ArgFtpSites = oArgs(ArgNum) | ArgFtpSites = oArgs(ArgNum) | ||
Case | Case "--virtualdir", "-v": | ||
ArgNum = ArgNum + 1 | ArgNum = ArgNum + 1 | ||
ArgVirtualDirs = Split(oArgs(ArgNum), | ArgVirtualDirs = Split(oArgs(ArgNum), ",", -1) | ||
Case | 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) | If (ArgNum + 1) > UBound(ArgVirtualDirs) Then | ||
WScript.Echo | WScript.Echo "Error understanding virtual directories" | ||
Call DisplayUsage | Call DisplayUsage | ||
End If | End If | ||
Line 134: | Line 134: | ||
End If | End If | ||
If (ArgFtpSites = | 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 & | WScript.Echo Now & ". Error Code: " & Hex(Err) & " - " & Msg | ||
End Sub | End Sub | ||
Sub Trace(Msg) | Sub Trace(Msg) | ||
WScript.Echo Now & | WScript.Echo Now & " : " & Msg | ||
End Sub | End Sub | ||
Sub DisplayUsage() | Sub DisplayUsage() | ||
WScript.Echo | WScript.Echo "Usage: mkftpdir [--computer|-c COMPUTER1,COMPUTER2]" | ||
WScript.Echo | WScript.Echo " <--ftpsite|-f FTPSITE1>" | ||
WScript.Echo | WScript.Echo " <--virtualdir|-v NAME1,PATH1,NAME2,PATH2,...>" | ||
WScript.Echo | WScript.Echo " [--help|-?]" | ||
WScript.Echo | WScript.Echo "" | ||
WScript.Echo | WScript.Echo "Note, FTPSITE is the Ftp Site on which the directory will be created." | ||
WScript.Echo | WScript.Echo "The name can be specified as one of the following, in the priority specified:" | ||
WScript.Echo | WScript.Echo " Server Number (i.e. 1, 2, 10, etc.)" | ||
WScript.Echo | WScript.Echo " Server Description (i.e ""My Server"")" | ||
WScript.Echo | WScript.Echo " Server Host name (i.e. ""ftp.domain.com"")" | ||
WScript.Echo | WScript.Echo " IP Address (i.e., 127.0.0.1)" | ||
WScript.Echo | WScript.Echo "" | ||
WScript.Echo | WScript.Echo "" | ||
WScript.Echo | WScript.Echo "Example : mkftpdir -c MyComputer -f ""Default Ftp Site""" | ||
WScript.Echo | 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( | Set vRoot = ftpSite.GetObject("IIsFtpVirtualDir", "Root") | ||
Trace | Trace "Accessing root for " & ftpSite.ADsPath | ||
If (Err < | If (Err <> 0) Then | ||
Display | 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( | Set vDir = vRoot.Create("IIsFtpVirtualDir", DirNames(DirNum)) | ||
If (Err < | If (Err <> 0) Then | ||
Display | 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 < | If (Err <> 0) Then | ||
Display | 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 < | If (Err <> 0) Then | ||
Display | Display "Unable to save configuration for " & vRootName & "/" & DirNames(DirNum) & "." | ||
Else | Else | ||
Trace | 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 | Display "Unable to find " & FtpSiteName & " on " & ComputerName | ||
End If | End If | ||
Trace | 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, | one = InStr(bindstr, ":") | ||
two = InStr((one + 1), bindstr, | 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( | Set ftpsvc = GetObject("IIS://" & computer & "/MsFtpSvc") | ||
If (Err < | 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( | 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 = | 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 = | 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) = | If aBinding(0) = "" Then | ||
binding = Null | binding = Null | ||
Else | Else | ||
Line 271: | Line 271: | ||
End If | End If | ||
Else | Else | ||
If aBinding = | If aBinding = "" Then | ||
binding = Null | binding = Null | ||
Else | Else |
Latest revision as of 13:52, 21 July 2020
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
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
- 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