Jump to content

Applet to control IIS web server


billmez
 Share

Recommended Posts

Hello All,

The following code is an applet to control or delete the virtual web servers in IIS5 or IIS6, and is part of a comercial application suite for web dedicated servers that I am writing, and would like to share with the community.

It needs the beta, and windows server 2000, 2003, or W2K pro or XP pro with IIS installed.

Enjoy- :lmao:

billmez

; W3Servers
; Applet to control virtual web servers in IIS5 or IIS6
; Author Bill Mezian, billmez - Jan06
;
#Include <GuiListView.au3>
#include <GUIConstants.au3>

_W3Servers()

Func _W3Servers()
Dim $RetMsg, $LastIndex, $dspIP,$GotServers, $Mval, $SLine, $SIndex, $w3svc, $Stitle, $Svip
Dim $Deny_Delete
Dim $DA

; pipe separated list of web sites not allowed to delete
; if $DAsites is = "" deletion will not be allowed and button
; will be inactive
Dim $DAsites = "Default Web Site|MyDomain.com|YourDomain.com"
$DA = StringSplit($DAsites, "|")
Dim $DAcount = UBound($DA) - 1
If Not $DA[$DAcount] Then
    $DAcount = 0
EndIf

; == GUI generated with Koda ==
Dim $WForm1 = GUICreate("WWW Server Control", 410, 385, -1, -1)
Dim $Label1 = GUICtrlCreateLabel("Click the Get Servers button to retrieve a list of virtual web servers.  Select the desired row and click on a service control buton to apply the action.", 30, 5, 350, 25)
Dim $WButton1 = GUICtrlCreateButton("Get Servers", 15, 295, 120, 21)
Dim $WButton2 = GUICtrlCreateButton("Refresh List", 145, 295, 120, 21)
Dim $WButton3 = GUICtrlCreateButton("Exit", 275, 295, 120, 21)
Dim $Group1 = GUICtrlCreateGroup("Service Control Buttons", 5, 322, 400, 53)
GUICtrlSetFont ($Group1,9, 600,-1,"Tahoma")
Dim $WButton4 = GUICtrlCreateButton("Start Selected", 15, 345, 120, 21)
Dim $WButton5 = GUICtrlCreateButton("Stop Selected", 145, 345, 120, 21)
Dim $WButton6 = GUICtrlCreateButton("Delete Selected", 275, 345, 120, 21)
    If Not $DAcount Then
        GUICtrlSetState ($WButton6,$GUI_DISABLE)
    EndIf
Global $WList1 = GUICtrlCreateListView("Index|Server Identity|IP Address|Port|State|Redirect", 5, 40, 400, 245, -1, BitOR($LVS_EX_FULLROWSELECT, $LVS_EX_GRIDLINES, $LVS_SORTASCENDING))
_GUICtrlListViewSetColumnWidth($WList1, 0, 43)
_GUICtrlListViewSetColumnWidth($WList1, 1, 145)
_GUICtrlListViewSetColumnWidth($WList1, 2, 72)
_GUICtrlListViewSetColumnWidth($WList1, 3, 35)
_GUICtrlListViewSetColumnWidth($WList1, 4, 50)
_GUICtrlListViewSetColumnWidth($WList1, 5, 80)
GUISetState(@SW_SHOW)

;If Not $GotServers Then
;           _getServers()
;       EndIf
While 1
    $wmsg = GuiGetMsg()
    Select
    Case $wmsg = $GUI_EVENT_CLOSE Or $wmsg = $WButton3
            GUISetState(@SW_HIDE, $WForm1)
            GUIDelete($WForm1)
            $Edservers = ""
                Return 1
    ;ExitLoop
    Case $wmsg = $WButton1
        If Not $GotServers Then
            _getServers()
        Else
            _RefreshServers()
        EndIf
    
    Case $wmsg = $WButton2
        _RefreshServers()
    Case $wmsg = $WButton4
        Dim $SFields = StringSplit(GUICtrlRead(GUICtrlRead($WList1)), "|")
                $SIndex = $SFields[1]
                    If $SIndex > 0 Then
                        _StartServer($SIndex)
                        _RefreshServers()
                    EndIf
    Case $wmsg = $WButton5
        Dim $SFields = StringSplit(GUICtrlRead(GUICtrlRead($WList1)), "|")
                $SIndex = $SFields[1]
                    If $SIndex > 0 Then
                        _StopServer($SIndex)
                        _RefreshServers()
                    EndIf
    Case $wmsg = $WButton6
        If $GotServers Then
            
            Dim $SFields = StringSplit(GUICtrlRead(GUICtrlRead($WList1)), "|")
                $SIndex = $SFields[1]
                    If $SIndex > 0 Then
                        $Stitle = $SFields[2]
                        $Svip = $SFields[3]
                        
            If $DAcount > 0 Then            
                For $i = 1 To $DAcount
                    If StringInStr($DA[$i], $Stitle) Then
                        $Deny_Delete = 1
                        MsgBox(4112, "Error!", "You May Not Delete the "& $Stitle & " Web Site!")
                        ExitLoop
                    EndIf
                Next
    If Not $Deny_Delete Then
        $Mval = MsgBox(4116, "Confirm Action!", "You have selected to delete the following WWW server:" & @crlf &  "Index: " & $SFields[1] & "," & " Server Title: " & $Stitle & ", IP Address: " & $Svip & @CRLF & "Sellect YES to delete this server, or NO to return to the form and make changes")
    
        If $Mval = 6 Then
            
            _DeleteServer($SIndex, $Stitle)
            _RefreshServers()
        
        ElseIf $Mval = 7 Then
            ContinueLoop
        EndIf           
                
    EndIf
    EndIf           
        Endif
    EndIf
    
    Case Else
    ;;;;;;;
    EndSelect
WEnd
Exit
EndFunc
Func _getServers()
    
Local $WSstate = _ArrayCreate(5,"Starting", "Started", "Stopping", "Stopped")
$w3svc = ObjGet("IIS://" & @ComputerName & "/w3svc")
        If @error <> 0 Then
            MsgBox(4112, "Error!", "Unable to open: "&"IIS://" & @ComputerName & "/w3svc")
            Return 0
        EndIf

Dim $InstID, $InstName, $InstState, $re_dir, $r_dir
    For $WebServer in $w3svc
        $r_dir = ""
        If $WebServer.Class = "IIsWebServer" Then
            $InstID =  $WebServer.Name
            $InstName = $WebServer.ServerComment
            $InstState = $WebServer.ServerState
            $Bindings = $WebServer.ServerBindings
            
                
                    Dim $Sip = StringSplit($Bindings[0], ":")
                    If Not $Sip[1] Then
                        $dspIP = "Default"
                    Else
                        $dspIP = $Sip[1]
                    EndIf
                    $IIsWebVirtualDirObj = ObjGet("IIS://localhost/W3SVC/" & $InstID & "/Root") 
                    $redir = $IIsWebVirtualDirObj.HttpRedirect
                    if $redir Then
                    $re_dir = StringSplit ($redir, ",")
                    $r_dir = $re_dir[1]
                    EndIf
                ;next
            ;If $InstID Then;<> $LastIndex Then 
    GUICtrlCreateListViewItem($InstID & "|" & $InstName & "|" & $dspIP & "|" &  $Sip[2]  & "|" &  $WSstate[$InstState] & "|" &  $r_dir,$WList1)
            ;EndIf
                ;$LastIndex = $WebServer.name
                ;next
                    EndIf
                next
                
$GotServers = 1
EndFunc     
Func _RefreshServers()
    _GUICtrlListViewDeleteAllItems($WList1)
    _getServers()
EndFunc
Func _StartServer($Sindx)
    If $Sindx Then
    $Web_Server = ObjGet("IIS://" & @ComputerName & "/w3svc/" & $Sindx)
            $Web_Server.Start()
            If @error <> 0 Then
                MsgBox(4112, "Error!", "Unable to start web server index: " & $Sindx & "!")
                
            Else
                MsgBox(4160, "Information", "Web server index " & $Sindx & " started succesfully!")
            EndIf
        EndIf
EndFunc
Func _StopServer($Sindx)
    If $Sindx Then
    $Web_Server = ObjGet("IIS://" & @ComputerName & "/w3svc/" & $Sindx)
            $Web_Server.Stop()
            If @error <> 0 Then
                MsgBox(4112, "Error!", "Unable to stop web server index: " & $Sindx & "!")
                
            Else
                MsgBox(4160, "Information", "Web server index " & $Sindx & " stopped succesfully!")
            EndIf
        EndIf
EndFunc
Func _DeleteServer($Sindx, $InstName)
    
$Web_Server = ObjGet("IIS://" & @ComputerName & "/w3svc")   

    If $Sindx Then
        $rnRet = $Web_Server.Delete("IIsWebServer", $Sindx)
        
    If Not $rnRet Then
        MsgBox(4160, "Information", "Web server " & $InstName & " at index: " & $Sindx & " deleted succesfully!")
    EndIf
        
    Else    
        MsgBox(4112, "Error!", "Unable to delete web server. Missing server index!")
        
    EndIf
EndFunc
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...