Avatar of HelderConde
HelderConde

asked on 

Automate site creation with "Connect As" Authorization settings

Dear all,

I'm using an automated site creation VBS that allows me to create site in IIS with:

1. proper Site Name
2. proper IP bindings and host name values (multiple, which is not available via iisweb script)
3. create folder and assign folder to IIS site.

This VBS is running perfectly, but now I need a new functionality which I've been unable to accomplish: note that I'm using a folder that is in a network share, with certain NTFS permissions. So, I need that script to allow me to add "Connect As" values/credentials, so that the site can access content from the share. Right now, I need to set that up manually - which is definitely not very wise in a some-hundred-website environment.

Perhaps some of you could help me tweak that VBS script or even suggest a whole new approach for me to automate this process.

Thanks in advance!

Helder Conde
Domain = "mydomain.com"
 
FolderName= "\\MySharedStorage\websites\MyDomainFolder"
 
'This part creates a new folder on the disk
dim filesys, folder, path 
path = FolderName
set filesys=CreateObject("Scripting.FileSystemObject") 
If Not filesys.FolderExists(path) Then 
Set folder = filesys.CreateFolder(path) 
End If
 
 
Server1 = "192.168.0.1"
 
'Make connections to WMI, to the IIS namespace on MyMachine, and to the Web service.
set locatorObj = CreateObject("WbemScripting.SWbemLocator")
set providerObj = locatorObj.ConnectServer(Server1, "root/MicrosoftIISv2")
set serviceObj = providerObj.Get("IIsWebService='W3SVC'")
 
' Build binding object, which is a required parameter of the CreateNewSite method.
' Use the SpawnInstance WMI method since we are creating a new instance of an object.
Bindings = Array(0)
 
Set Bindings(0) = providerObj.get("ServerBinding").SpawnInstance_()
Bindings(0).IP = SERVER1
Bindings(0).Port = "80"
Bindings(0).Hostname = Domain
 
Redim Preserve Bindings(1)
Set Bindings(1) = providerObj.get("ServerBinding").SpawnInstance_()
Bindings(1).IP = SERVER1
Bindings(1).Port = "80"
Bindings(1).Hostname = "www." & Domain
 
 
' Create the new Web site using the CreateNewSite method of the IIsWebService object.
Dim strSiteObjPath
strSiteObjPath = serviceObj.CreateNewSite(Domain, Bindings, FolderName)
If Err Then
WScript.Echo "*** Error Creating Site: " & Hex(Err.Number) & ": " & Err.Description & " ***"
WScript.Quit(1)
End If
 
' strSiteObjPath is in the format of IIsWebServer='W3SVC/1180970907'
' To parse out the absolute path, W3SVC/1180970907, use the SWbemObjectPath WMI object.
Set objPath = CreateObject("WbemScripting.SWbemObjectPath")
objPath.Path = strSiteObjPath
strSitePath = objPath.Keys.Item("")
 
' Set some properties on the root virtual directory which was created by CreateNewSite.
Set vdirObj = providerObj.Get("IIsWebVirtualDirSetting='" & strSitePath & "/ROOT'")
vdirObj.AuthFlags = 5 ' AuthNTLM + AuthAnonymous
vdirObj.EnableDefaultDoc = True
vdirObj.DirBrowseFlags = &H4000003E ' date, time, size, extension, longdate
vdirObj.AccessFlags = 513 ' read, script
vdirObj.AppFriendlyName = "Default Application"
 
' Save the new settings to the metabase
vdirObj.Put_()
 
' CreateNewSite does not start the server, so start it now.
Set serverObj = providerObj.Get(strSiteObjPath)
serverObj.Start
 
WScript.Echo "A New site called " & DOMAIN & " was created with the path and unique site identification number of " & strSitePath

Open in new window

Microsoft IIS Web ServerShell Scripting

Avatar of undefined
Last Comment
HelderConde

8/22/2022 - Mon