用VBScript建虚拟目录

I've written some functions that set up a vdir and create an application at that point ... and back it out at uninstall.  It's all adapted from the links and info in the discussion forum, thanks to all! There are a lot of places for customization -- the attributes for the virtual directory and the application, the type of app (I'm using an out-of-proc app), etc. All you'll need to do is place this in your script, tweak it for your install, and call the CreateVDir function in one of your sections; call un.DeleteVDir from your Uninstall section.

 

 

The Script

LangString ERR_VDIREXISTS ${LANG_ENGLISH} "A virtual directory named ${VDIRNAME} already exists. The new virtual directory will not be created."
 
;--------------------------------
; CreateVDir Function
Function CreateVDir
 
;Open a VBScript File in the temp dir for writing
DetailPrint "Creating $TEMP\createVDir.vbs";
FileOpen $0 "$TEMP\createVDir.vbs" w
 
;Write the script:
;Create a virtual dir named ${VDIRNAME} pointing to $INSTDIR\web with proper attributes
FileWrite $0 "On Error Resume Next$\n$\n"
FileWrite $0 "Set Root = GetObject($\"IIS://LocalHost/W3SVC/1/ROOT$\")$\n"
FileWrite $0 "Set Dir = Root.Create($\"IIsWebVirtualDir$\", $\"${VDIRNAME}$\")$\n$\n"
FileWrite $0 "If (Err.Number <> 0) Then$\n"
StrCpy $1 $(ERR_VDIREXISTS) ;To substitute a LangString in the vbs copy it before
FileWrite $0 " MsgBox $\"$1$\"$\n"
FileWrite $0 " Wscript.Quit (Err.Number)$\n"
FileWrite $0 "End If$\n$\n"
FileWrite $0 "Dir.Path = $\"$INSTDIR\web$\"$\n"
FileWrite $0 "Dir.AccessRead = True$\n"
FileWrite $0 "Dir.AccessWrite = False$\n"
FileWrite $0 "Dir.AccessScript = True$\n"
FileWrite $0 "Dir.AppFriendlyName = $\"${VDIRNAME}$\"$\n"
FileWrite $0 "Dir.EnableDirBrowsing = False$\n"
FileWrite $0 "Dir.ContentIndexed = False$\n"
FileWrite $0 "Dir.DontLog = True$\n"
FileWrite $0 "Dir.EnableDefaultDoc = True$\n"
FileWrite $0 "Dir.DefaultDoc = $\"default.asp$\"$\n"
FileWrite $0 "Dir.AspBufferingOn = True$\n"
FileWrite $0 "Dir.AspAllowSessionState = True$\n"
FileWrite $0 "Dir.AspSessionTimeout = 30$\n"
FileWrite $0 "Dir.AspScriptTimeout = 900$\n"
FileWrite $0 "Dir.SetInfo$\n$\n"
;Create the application object
FileWrite $0 "Set IISObject = GetObject($\"IIS://LocalHost/W3SVC/1/ROOT/${VDIRNAME}$\")$\n$\n"
FileWrite $0 "IISObject.AppCreate2(1) 'Create an out-of-process web application$\n"
FileWrite $0 "If (Err.Number <> 0) Then$\n"
FileWrite $0 " MsgBox $\"Error trying to create the application at 'IIS://LocalHost/W3SVC/1/ROOT/${VDIRNAME}'$\"$\n"
FileWrite $0 " WScript.Quit (Err.Number)$\n"
FileWrite $0 "End If$\n"
 
FileClose $0
 
DetailPrint "Executing $TEMP\createVDir.vbs"
nsExec::Exec /TIMEOUT=20000 '"$SYSDIR\cscript.exe" "$TEMP\createVDir.vbs"'
DetailPrint "Virtual Directory ${VDIRNAME} successfully created."
Delete "$TEMP\createVDir.vbs"
 
FunctionEnd
 
;--------------------------------
; CreateVDir Function
Function un.DeleteVDir
 
;Open a VBScript File in the temp dir for writing
DetailPrint "Creating $TEMP\deleteVDir.vbs";
FileOpen $0 "$TEMP\deleteVDir.vbs" w
 
;Write the script:
;Create a virtual dir named ${VDIRNAME} pointing to $INSTDIR\web with proper attributes
FileWrite $0 "On Error Resume Next$\n$\n"
;Delete the application object
FileWrite $0 "Set IISObject = GetObject($\"IIS://LocalHost/W3SVC/1/ROOT/${VDIRNAME}$\")$\n$\n"
FileWrite $0 "IISObject.AppDelete 'Delete the web application$\n"
FileWrite $0 "If (Err.Number <> 0) Then$\n"
FileWrite $0 " MsgBox $\"Error trying to delete the application at [IIS://LocalHost/W3SVC/1/ROOT/${VDIRNAME}]$\"$\n"
FileWrite $0 " WScript.Quit (Err.Number)$\n"
FileWrite $0 "End If$\n$\n"
 
FileWrite $0 "Set IISObject = GetObject($\"IIS://LocalHost/W3SVC/1/ROOT$\")$\n$\n"
FileWrite $0 "IIsObject.Delete $\"IIsWebVirtualDir$\", $\"${VDIRNAME}$\"$\n"
FileWrite $0 "If (Err.Number <> 0) Then$\n"
FileWrite $0 " MsgBox $\"Error trying to delete the virtual directory '${VDIRNAME}' at 'IIS://LocalHost/W3SVC/1/ROOT'$\"$\n"
FileWrite $0 " Wscript.Quit (Err.Number)$\n"
FileWrite $0 "End If$\n$\n"
 
FileClose $0
 
DetailPrint "Executing $TEMP\deleteVDir.vbs"
nsExec::Exec /TIMEOUT=20000 '"$SYSDIR\cscript.exe" "$TEMP\deleteVDir.vbs"'
DetailPrint "Virtual Directory ${VDIRNAME} successfully removed."
Delete "$TEMP\deleteVDir.vbs"
 
FunctionEnd

 

A Revision

I revised the script to respond to errors, and allow re-install of the same virtual directory, as long as it is in the same place and has the same parameters. This means that a user can re-run the Setup program and install over the top of the previous installation, as long as all the parameters are the same. --TW, Orbit Tech.

;--------------------------------
; CreateVDir Function
Function CreateVDir
 
;Open a VBScript File in the temp dir for writing
DetailPrint "Creating $TEMP\createVDir.vbs";
FileOpen $0 "$TEMP\createVDir.vbs" w
 
;Write the script:
;Create a virtual dir named ${VDIRNAME} pointing to $INSTDIR\web with proper attributes
FileWrite $0 "On Error Resume Next$\n"
FileWrite $0 "Set Root = GetObject($\"IIS://LocalHost/W3SVC/1/ROOT$\")$\n"
FileWrite $0 "Set Dir = Root.Create($\"IIsWebVirtualDir$\", $\"${VDIRNAME}$\")$\n"
FileWrite $0 "If (Err.Number <> 0) Then$\n"
FileWrite $0 "  If (Err.Number <> -2147024713) Then$\n"
FileWrite $0 "    message = $\"Error $\" & Err.Number$\n"
FileWrite $0 "    message = message & $\" trying to create IIS virtual directory.$\" & chr(13)$\n"
FileWrite $0 "    message = message & $\"Please check your IIS settings (inetmgr).$\"$\n"
FileWrite $0 "    MsgBox message, vbCritical, $\"${PRODUCT_NAME}$\"$\n"
FileWrite $0 "    WScript.Quit (Err.Number)$\n"
FileWrite $0 "  End If$\n"
FileWrite $0 "  ' Error -2147024713 means that the virtual directory already exists.$\n"
FileWrite $0 "  ' We will check if the parameters are the same: if so, then OK.$\n"
FileWrite $0 "  ' If not, then fail and display a message box.$\n"
FileWrite $0 "  Set Dir = GetObject($\"IIS://LocalHost/W3SVC/1/ROOT/${VDIRNAME}$\")$\n"
FileWrite $0 "  If (Dir.Path <> $\"$INSTDIR\web$\") Then$\n"
FileWrite $0 "    message = $\"Virtual Directory ${VDIRNAME} already exists in a different folder ($\" + Dir.Path + $\").$\" + chr(13)$\n"
FileWrite $0 "    message = message + $\"Please delete the virtual directory using the IIS console (inetmgr), and install again.$\"$\n"
FileWrite $0 "    MsgBox message, vbCritical, $\"${PRODUCT_NAME}$\"$\n"
FileWrite $0 "    Wscript.Quit (Err.Number)$\n"
FileWrite $0 "  End If$\n"
FileWrite $0 "  If (Dir.AspAllowSessionState <> True  Or  Dir.AccessScript <> True) Then$\n"
FileWrite $0 "    message = $\"Virtual Directory ${VDIRNAME} already exists and has incompatible parameters.$\" + chr(13)$\n"
FileWrite $0 "    message = message + $\"Please delete the virtual directory using the IIS console (inetmgr), and install again.$\"$\n"
FileWrite $0 "    MsgBox message, vbCritical, $\"${PRODUCT_NAME}$\"$\n"
FileWrite $0 "    Wscript.Quit (Err.Number)$\n"
FileWrite $0 "  End If$\n"
FileWrite $0 "  Wscript.Quit (0)$\n"
FileWrite $0 "End If$\n"
FileWrite $0 "Dir.Path = $\"$INSTDIR\web$\"$\n"
FileWrite $0 "Dir.AccessRead = True$\n"
FileWrite $0 "Dir.AccessWrite = False$\n"
FileWrite $0 "Dir.AccessScript = True$\n"
FileWrite $0 "Dir.AppFriendlyName = $\"${VDIRNAME}$\"$\n"
FileWrite $0 "Dir.EnableDirBrowsing = False$\n"
FileWrite $0 "Dir.ContentIndexed = False$\n"
FileWrite $0 "Dir.DontLog = True$\n"
FileWrite $0 "Dir.EnableDefaultDoc = True$\n"
FileWrite $0 "Dir.DefaultDoc = $\"default.aspx$\"$\n"
FileWrite $0 "Dir.AspBufferingOn = True$\n"
FileWrite $0 "Dir.AspAllowSessionState = True$\n"
FileWrite $0 "Dir.AspSessionTimeout = 30$\n"
FileWrite $0 "Dir.AspScriptTimeout = 900$\n"
FileWrite $0 "Dir.SetInfo$\n"
FileWrite $0 "Set IISObject = GetObject($\"IIS://LocalHost/W3SVC/1/ROOT/${VDIRNAME}$\")$\n"
FileWrite $0 "IISObject.AppCreate2(2) 'Create a process-pooled web application$\n"
FileWrite $0 "If (Err.Number <> 0) Then$\n"
FileWrite $0 " message = $\"Error $\" & Err.Number$\n"
FileWrite $0 " message = message & $\" trying to create the virtual directory at 'IIS://LocalHost/W3SVC/1/ROOT/${VDIRNAME}'$\" & chr(13)$\n"
FileWrite $0 " message = message & $\"Please check your IIS settings (inetmgr).$\"$\n"
FileWrite $0 " MsgBox message, vbCritical, $\"${PRODUCT_NAME}$\"$\n"
FileWrite $0 " WScript.Quit (Err.Number)$\n"
FileWrite $0 "End If$\n"
 
FileClose $0
 
DetailPrint "Executing $TEMP\createVDir.vbs"
nsExec::Exec /TIMEOUT=20000 '"$SYSDIR\cscript.exe" "$TEMP\createVDir.vbs"'
Pop $1
StrCmp $1 "0" CreateVDirOK
DetailPrint "Error $1 in CreateVDir.vbs"
Abort "Failed to create IIS Virtual Directory"
 
CreateVDirOK:
DetailPrint "Successfully created IIS virtual directory"
Delete "$TEMP\createVDir.vbs"
FunctionEnd
 
;--------------------------------
; DeleteVDir Function
Function un.DeleteVDir
 
;Open a VBScript File in the temp dir for writing
DetailPrint "Creating $TEMP\deleteVDir.vbs";
FileOpen $0 "$TEMP\deleteVDir.vbs" w
 
;Write the script:
;Create a virtual dir named ${VDIRNAME} pointing to $INSTDIR\web with proper attributes
FileWrite $0 "On Error Resume Next$\n$\n"
;Delete the application object
FileWrite $0 "Set IISObject = GetObject($\"IIS://LocalHost/W3SVC/1/ROOT/${VDIRNAME}$\")$\n$\n"
FileWrite $0 "IISObject.AppDelete 'Delete the web application$\n"
FileWrite $0 "If (Err.Number <> 0) Then$\n"
FileWrite $0 " MsgBox $\"Error trying to delete the application at [IIS://LocalHost/W3SVC/1/ROOT/${VDIRNAME}]$\", vbCritical, $\"${PRODUCT_NAME}$\"$\n"
FileWrite $0 " WScript.Quit (Err.Number)$\n"
FileWrite $0 "End If$\n$\n"
 
FileWrite $0 "Set IISObject = GetObject($\"IIS://LocalHost/W3SVC/1/ROOT$\")$\n$\n"
FileWrite $0 "IIsObject.Delete $\"IIsWebVirtualDir$\", $\"${VDIRNAME}$\"$\n"
FileWrite $0 "If (Err.Number <> 0) Then$\n"
FileWrite $0 " MsgBox $\"Error trying to delete the virtual directory '${VDIRNAME}' at 'IIS://LocalHost/W3SVC/1/ROOT'$\", vbCritical, $\"${PRODUCT_NAME}$\"$\n"
FileWrite $0 " Wscript.Quit (Err.Number)$\n"
FileWrite $0 "End If$\n$\n"
 
FileClose $0
 
DetailPrint "Executing $TEMP\deleteVDir.vbs"
nsExec::Exec /TIMEOUT=20000 '"$SYSDIR\cscript.exe" "$TEMP\deleteVDir.vbs"'
Pop $1
StrCmp $1 "0" +2
DetailPrint "Error $1 in deleteVDir.vbs"
goto DeleteVDirEnd
DetailPrint "Virtual Directory ${VDIRNAME} successfully removed."
Delete "$TEMP\deleteVDir.vbs"
DeleteVDirEnd:
FunctionEnd

 

A Revision

This is again an another revision, this script just checks if the virtual directory exists, if exists it overwrites the parameters of it, or if it doesn;t exist then it creates one and sets the params -- Harshavardhana Reddy N.

Function CreateVDir
Var /Global tVAR
;Open a VBScript File in the temp dir for writing
DetailPrint "Creating $TEMP\createVDir.vbs";
FileOpen $0 "$TEMP\createVDir.vbs" w
 
;Write the script:
;Create a virtual dir named ${VDIRNAME} pointing to $INSTDIR\web with proper attributes
FileWrite $0 "Option Explicit$\n"
FileWrite $0 "Dim IISFlag$\n"
FileWrite $0 "On Error Resume Next$\n"
FileWrite $0 "Set Root = GetObject($\"IIS://LocalHost/W3SVC/1/ROOT$\")$\n"
FileWrite $0 "If Dir Is Nothing Then$\n"
FileWrite $0 "Set Dir = Root.Create($\"IIsWebVirtualDir$\", $\"${VDIRNAME}$\")$\n"
FileWrite $0 "End If$\n"
 
ReadRegStr $tVAR HKLM "Software\company\product" "somekey"
${GetRoot} $tVAR $R0
StrCpy $R0 "$R0\"
FileWrite $0 "Dir.Path = $\"$R0$\"$\n"
FileWrite $0 "Dir.AccessRead = True$\n"
FileWrite $0 "Dir.AccessWrite = False$\n"
FileWrite $0 "Dir.AccessScript = True$\n"
FileWrite $0 "Dir.AppFriendlyName = $\"${VDIRNAME}$\"$\n"
FileWrite $0 "Dir.EnableDirBrowsing = False$\n"
FileWrite $0 "Dir.ContentIndexed = False$\n"
FileWrite $0 "Dir.DontLog = True$\n"
FileWrite $0 "Dir.EnableDefaultDoc = True$\n"
FileWrite $0 "Dir.DefaultDoc = $\"default.aspx$\"$\n"
FileWrite $0 "Dir.AspBufferingOn = True$\n"
FileWrite $0 "Dir.AspAllowSessionState = True$\n"
FileWrite $0 "Dir.AspSessionTimeout = 30$\n"
FileWrite $0 "Dir.AspScriptTimeout = 900$\n"
FileWrite $0 "Dir.SetInfo$\n"
FileWrite $0 "If IISFlag = 1 Then$\n"
FileWrite $0 "Set IISObject = GetObject($\"IIS://LocalHost/W3SVC/1/ROOT/${VDIRNAME}$\")$\n"
FileWrite $0 "IISObject.AppCreate2(2) 'Create a process-pooled web application$\n"
FileWrite $0 "End If$\n"
FileClose $0
 
DetailPrint "Executing $TEMP\createVDir.vbs"
nsExec::Exec /TIMEOUT=20000 '"$SYSDIR\cscript.exe" "$TEMP\createVDir.vbs"'
Pop $1
StrCmp $1 "0" CreateVDirOK
DetailPrint "Error $1 in CreateVDir.vbs"
Abort "Failed to create IIS Virtual Directory"
 
CreateVDirOK:
DetailPrint "Successfully created IIS virtual directory"
Delete "$TEMP\createVDir.vbs"
FunctionEnd
posted on 2008-09-25 21:29  美丽心情11  阅读(422)  评论(0编辑  收藏  举报