?
Solved

Changing the settings in IIS using vbscipt

Posted on 2005-05-09
10
Medium Priority
?
484 Views
Last Modified: 2011-10-03
Below is the function that Iam using to change the settings in IIS. Basically what Iam trying to do is change the path for the "Default Web Site"  and assign a new port number to it using vbscript. Its givin an error at
Set WebServer = w3svc.Create("IIsWebServer", site.Name)  ///Iam getting an Error Here with Err = 500
Please someone help me out with this!!! Thanks@

computer = "LocalHost"
webName = "Default Web Site"

Function ChangeWebSettings(computer, webname)
      On Error Resume Next

      Dim websvc, site
      dim webinfo
      Dim aBinding, binding

      set websvc = GetObject("IIS://"&computer&"/W3svc")
      if (Err <> 0) then
            exit function
      end if
      for each site in websvc
            if site.class = "IIsWebServer" then
                  If site.ServerComment = webname Then
                        set findWeb = site
                  End If
            end if
      next

      PortNum = "81"
      Set WebServer = w3svc.Create("IIsWebServer", site.Name)  ///Iam getting an Error Here with Err = 500
      if (Err <> 0) then
            Trace "error WebServer"
            exit function
      end if
      BindingString = ":" & PortNum & ":"

      WebServer.ServerBindings = BindingString
      WebServer.ServerComment = ArgWebSites
      WebServer.SetInfo

      ' Now create the root directory object.
      Trace "Setting the home directory..."
      Set NewDir = WebServer.Create("IIsWebVirtualDir", "ROOT")
      NewDir.Path = ArgRootDirectory
      NewDir.AccessRead = true
      Err.Clear
      NewDir.SetInfo
      NewDir.AppCreate (True)

      If (Err.Number = 0) Then
            Trace "Home directory set."
      Else
            Display "Error setting home directory."
      End If
      
End Function
0
Comment
Question by:jackrocks
  • 6
  • 2
  • 2
10 Comments
 
LVL 37

Expert Comment

by:meverest
ID: 13964475
Hello,

this is probably more an ASP code question than IIS, but I think there might be a couple of problems in your script that might be the cause of trouble.

firstly, I think that the second argument to w3svc.Create() is supposed to be numeric form, maybe even integer.  Whatever 'site.name' is, I would not expect it to be numeric - which might throw that kind of error.

Secondly, there are some variables used later on that do not seem to be defined.   "ArgRootDirectory" for one - I can;t see what the value is supposed to be.  Does ASP throw an error for undefined variables, or does it assume null?  I am not sure, I am not an ASP coder.

Cheers,  Mike.
0
 
LVL 34

Expert Comment

by:Dave_Dietz
ID: 13964765
If I remember correctly the scope of the iteration variable in a foreach loop is the loop itself.

It is entirely possible that when you use site.Name in "Set WebServer = w3svc.Create("IIsWebServer", site.Name)" you are actually using an uninitialized variable.

Dave Dietz

0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 13966398

If you're using IIS 6 you would be better using the CreateWebSite method and assigning the port number through a constructed server binding. The Site ID created in IIS 6 is a hash of the server comment, so not something you need to statically define as with IIS 4 and 5.

Here is some sample code:

Const SERVER_IP = "your server IP"
Const HTTP_PORT = "81"

strServer = "localhost"
strDomainName = "<domain name for site>"
strWebRoot = "<path to home folder>"

Set objLocator = CreateObject("WbemScripting.SWbemLocator")
Set objProvider = objLocator.ConnectServer(strServer, "root/MicrosoftIISv2")
Set objService = objProvider.Get("IIsWebService='W3SVC'")

' Constructing the Binding

colBindings = Array(0)
Set colBindings(0) = objProvider.Get("ServerBinding").SpawnInstance_()
colBindings(0).IP = SERVER_IP
colBindings(0).Port = HTTP_PORT
colBindings(0).Hostname = strDomainName

' Create the Site

strSiteObjectPath = objService.CreateNewSite(strServerComment, colBindings, strWebRoot)

If it's not IIS 6 then the syntax is roughly correct - the service object expects the service type and numeric site ID as an argument for the Create method when using "IIsWebServer" - but the Site ID must be unique - it's the metabase identifier for each site regardless of port used.

With that in mind, if you are trying to apply a new port for the Default Web Server then what you need to do is rewrite the Server Binding List for the default website not attempt to create a new site since the identifier for the default Web Site (1) is taken.

Hope that all makes sense.

Chris
0
NEW Veeam Backup for Microsoft Office 365 1.5

With Office 365, it’s your data and your responsibility to protect it. NEW Veeam Backup for Microsoft Office 365 eliminates the risk of losing access to your Office 365 data.

 
LVL 71

Expert Comment

by:Chris Dent
ID: 13966478

If you're attempting to debug code you really should get rid of the nasty On Error Resume Next statement - it suppresses all useful information and is generally not a nice way to handle errors.

This is a heavily commented and modified version of your original code:

computer = "LocalHost"
webName = "Default Web Site"
      
Function ChangeWebSettings(computer, webname)

      Dim websvc, site, objSite
      dim webinfo
      Dim aBinding, binding

      set websvc = GetObject("IIS://"&computer&"/W3svc")

      ' This bit attempts to find the site ID by matching the server comments
      ' objSite represents a direct connection to that site

      for each site in websvc
            if site.class = "IIsWebServer" then
                  If site.ServerComment = webname Then
                        Set objSite = site
                  End If
            end if
      next

      ' site.Name is 1 in the original code, you cannot create IIsWebServer
      ' for that site.Name, it already exists.

      ' Set WebServer = w3svc.Create("IIsWebServer", site.Name)  ///Iam getting an Error Here with Err = 500
      ' if (Err <> 0) then
      '      Trace "error WebServer"
      '      exit function
      ' end if

      ' Creates a binding in the form "All Unassinged:81:All Unassigned"

      PortNum = "81"
      BindingString = ":" & PortNum & ":"

      objSite.ServerBindings = BindingString
      objSite.ServerComment = ArgWebSites
      objSite.SetInfo

      ' Root also exists so this will throw up an error with the create method as well.

      Trace "Setting the home directory..."
      ' Set NewDir = WebServer.Create("IIsWebVirtualDir", "ROOT")

      Set objVDir = ("IIS://" & strServer & "/W3SVC/" & objSite.Name & "/ROOT")

      objVDir.Path = ArgRootDirectory
      objVDir.AccessRead = true
      objVDir.SetInfo
      
      ' This creates an InProcess application, this might exist as well...
      ' Are you sure you don't want it running as another user (Out of Process)?

      objVDir.AppCreate (True)

      ' If (Err.Number = 0) Then
      '       Trace "Home directory set."
      ' Else
      '       Display "Error setting home directory."
      ' End If
End Function
0
 
LVL 71

Accepted Solution

by:
Chris Dent earned 1000 total points
ID: 13966515

And finally... the cleaned up and very short version...

strComputer = "LocalHost"
strWebName = "Default Web Site"
      
Function ChangeWebSettings(strComputer, strWebname)

      Dim objWebSvc, objSite
      Dim strbinding
      Dim arrBindings

      ' Binding Construction - it normally expects an array, so just in case

      strPortNum = "81"
      strBinding = ":" & strPortNum & ":"
      arrBindings(0) = strBinding

      Set objWebSvc = GetObject("IIS://" & strComputer & "/W3svc")

      For Each objSite In objWebSvc
            If objSite.class = "IIsWebServer" Then
                  If objSite.ServerComment = strWebName Then
                        objSite.ServerBindings = arrBindings
                        objSite.ServerComment = ArgWebSites
                        objSite.SetInfo

                        Set objVDir = ("IIS://" & strServer & "/W3SVC/" & objSite.Name & "/ROOT")
                        objVDir.Path = ArgRootDirectory
                        objVDir.AccessRead = True
                        objVDir.SetInfo
                        objVDir.AppCreate (True)

                  End If
            End If
      Next
End Function
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 13966857

Incidently, using this kind of method rather than attempting to reconnect to the site again after finding it gets around the problem mentioned by Dave: that you could attempt to use an uninitialized variable.

And if any of the information I've provided doesn't achieve what you'd like then just say... I have a fairly huge collection of scripts for IIS and get some kind of bizarre enjoyment out of writing them.

Chris
0
 
LVL 34

Expert Comment

by:Dave_Dietz
ID: 13971437
Wow.

"I have a fairly huge collection of scripts for IIS and get some kind of bizarre enjoyment out of writing them" - have you considered seeking professional help for this condition?  ;-)

I'm glad we've got a scripting guru amongst us.  I can tell you what all of this does, but I'm not a script programmer by any means......

Dave Dietz
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 13971477

hehe I thought posting on here was considered some kind of professional help... it's almost like a support group ;)
0
 
LVL 37

Expert Comment

by:meverest
ID: 13973709
the way I see it, this *is* a support group - nothing more, nothing less.

Cheers!
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 13975968

Glad I could help anyway and thankyou for the points :-D
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Here are the symptoms: You start receiving calls from users that one of your legacy web apps isn't coming up, so you log into your IIS 5 server to check it out.  When you pull up the services, you notice that the WWW Publishing service isn't runn…
Running classic asp applications under Windows Server 2008 R2 (x64) and IIS 7 is not as easy as one may think. It took me a while to figure it out while getting error 8002801d a few times. After you install the OS you will need to install the fol…
Screencast - Getting to Know the Pipeline
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses
Course of the Month15 days, 16 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question