Jump to content

IIS COM Object Scripting


 Share

Recommended Posts

This is kind of OT, but I know there are some IIS geniuses on this forum...

I am scripting a server setup in AutoIT, and have made use of several Microsoft TechNet .vbs script, translated into AutoIT to do things.

In the course of loading the server, a couple dozen things get put under the Default WebSite, as seen from the IIS manager GUI. I need to change the Anonymous User/Password setting for a couple of these virtual directories.

The first one I tried failed to get the object for the directory so I started troubleshooting with:

$sMsg = "List of objects visible in IIS WMI Provider (//./root/MicrosoftIISv2):" & @CRLF

$oIIS = ObjGet("winmgmts://" & @ComputerName & "/root/MicrosoftIISv2")

;Get Virtural Directories
$n = 0
$sMsg &= @CRLF & "Virtual Directories (IISWebVirtualDir):" & @CRLF & @CRLF
$colVirDirs = $oIIS.ExecQuery ("Select * from IISWebVirtualDir")
For $oVirDir In $colVirDirs
    $n += 1
    $sMsg &= $n & ". Name: "
    $sMsg &= $oVirDir.Name & @CRLF
Next
MsgBox(64, "Test", $sMsg)

Turns out this script lists SOME of the virtual directories, but not all. One virtual directory I created manually by right-clicking the Default Web Site in the GUI, selecting New, then Virtual Directory, giving it a name and a valid folder path, and it shows up in the GUI, but not in the list from the script.

Now, this is not an AutoIT issue. If I run Microsoft's .vbs script unmodified:

strComputer = "."
Set objWMIService = GetObject _
    ("winmgmts:{authenticationLevel=pktPrivacy}\\" _
        & strComputer & "\root\microsoftiisv2")

Set colItems = objWMIService.ExecQuery("Select * from IIsWebVirtualDir")
 
For Each objItem in colItems
    Wscript.Echo "Application Isolated: " & objItem.AppIsolated
    Wscript.Echo "Application Package ID: " & objItem.AppPackageID
    Wscript.Echo "Application Package Name: " & objItem.AppPackageName
    Wscript.Echo "Application Root: " & objItem.AppRoot
    Wscript.Echo "Installation Date: " & objItem.InstallDate
    Wscript.Echo "Name: " & objItem.Name
Next

I get the same short list of virtual directories, still not including the one I manually created (and some others).

Anybody got an idea why?

P.S. My manually created virtual directory shows up in Metabase.xml as:

<IIsWebVirtualDir   Location ="/LM/W3SVC/1/ROOT/MyContent"
        AccessFlags="AccessRead | AccessScript"
        AppFriendlyName="MyContent"
        AppIsolated="2"
        AppRoot="/LM/W3SVC/1/Root/MyContent"
        DirBrowseFlags="EnableDirBrowsing | DirBrowseShowDate | DirBrowseShowTime | DirBrowseShowSize | DirBrowseShowExtension | DirBrowseShowLongDate | EnableDefaultDoc"
        Path="G:\My_Data\"
    >

So it, and others, show up in the GUI, show up in Metabase.xml, but don't get listed by the Microsoft or AutoIT script...?

:)

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

How does the list of virtual directories jive with the list generated by this command?

cscript C:\Inetpub\AdminScripts\adsutil.vbs enum w3svc/1/root
Good question. I'll find out Monday morning and post the result.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

How does the list of virtual directories jive with the list generated by this command?

cscript C:\Inetpub\AdminScripts\adsutil.vbs enum w3svc/1/root
OK, the adsutil script sees all the directories that are missing from the other scripts.

Thanks for the tip. :D

Moreover, details are read by changing it to:

cscript C:\Inetpub\AdminScripts\adsutil.vbs enum w3svc/1/root/MyContent

I am left not understanding why the other scripts don't see the same information... :)

As I said before, I can see everything in Metabase.xml, but the original scripts don't. The original scripts are fussy about things like the difference between a "Virtual Directory" and a "Web Directory", but the adsutil.vbs lists either as though they were the same thing.

The short answer is I'm going to try translating the object call in adsutil.vbs to AutoIT. But, for the sake of education, do you have any idea why the other scripts appear broken?

:D

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

It has been too long ago since I dealt with this to remeber the exact details, but I had a similar problem once that was caused by a special character (an '&' perhaps) in one of the metabase field strings. I made a copy of the metabase, removed this character and everything was read properly.

Dale

Free Internet Tools: DebugBar, AutoIt IE Builder, HTTP UDF, MODIV2, IE Developer Toolbar, IEDocMon, Fiddler, HTML Validator, WGet, curl

MSDN docs: InternetExplorer Object, Document Object, Overviews and Tutorials, DHTML Objects, DHTML Events, WinHttpRequest, XmlHttpRequest, Cross-Frame Scripting, Office object model

Automate input type=file (Related)

Alternative to _IECreateEmbedded? better: _IECreatePseudoEmbedded  Better Better?

IE.au3 issues with Vista - Workarounds

SciTe Debug mode - it's magic: #AutoIt3Wrapper_run_debug_mode=Y

Doesn't work needs to be ripped out of the troubleshooting lexicon. It means that what you tried did not produce the results you expected. It begs the questions 1) what did you try?, 2) what did you expect? and 3) what happened instead?

Reproducer: a small (the smallest?) piece of stand-alone code that demonstrates your trouble

Link to comment
Share on other sites

It has been too long ago since I dealt with this to remeber the exact details, but I had a similar problem once that was caused by a special character (an '&' perhaps) in one of the metabase field strings. I made a copy of the metabase, removed this character and everything was read properly.

Dale

Didn't find any invalid characters in Metabase.xml, and I still don't know why those winmgmts: calls fail, but using the IIS: objects as in adsutil.vb script:

; Test IIS Functions

; Declare COM Object error handler:
Global $oComError = ObjEvent("AutoIt.Error", "_ComErrFunc")

; Get virtual directories
$sTarget = InputBox("Test", "Path from root: ", "")
If @error Then $sTarget = ""
$sResults = _IIS_Enum($sTarget)

; Display results
If @error Then
    MsgBox(16, "Test", "Function returned @error = " & @error & "  @extended = " & @extended)
Else
    $sResultsFile = @ScriptDir & "\Results.txt"
    $hResultsFile = FileOpen($sResultsFile, 2) ; 2 = Overwrite
    FileWrite($hResultsFile, $sResults)
    FileClose($hResultsFile)
    Run("notepad.exe " & $sResultsFile, @TempDir, @SW_SHOW)
EndIf


;--------------------------------------
; Funciton _IIS_Enum()
;   Enumerate IIS paths, properties, and settings
;   Call with:  _IIS_Enum($sPath)
;       Where:  $sPath is the virtual directory, relative to root
;           i.e. "MyDir", or "MyDir/SubDir"
;   On success returns a message string
;   On failure sets @error and @extended
;--------------------------------------
Func _IIS_Enum($sPath = "")
    Local $sIISPath = "IIS://" & @ComputerName & "/w3svc/1/root"
    If $sPath <> "" Then $sIISPath = $sIISPath & "/" & $sPath
    Local $sMsg, $oIISPath, $oIISSchema, $sPropName, $n
    
    ; Get IIS object
    $oIISPath = ObjGet($sIISPath)
    If IsObj($oIISPath) Then
        $sMsg &= "Enumerate objects visible in: " & $sIISPath & @CRLF
        ; Get Schema
        $sIISSchema = $oIISPath.Schema
        $oIISSchema = ObjGet($sIISSchema)
        If IsObj($oIISSchema) Then
            $sMsg &= "Got schema object..." & @CRLF
            
            ; List mandatory properties in the schema
            $colIISManProp = $oIISSchema.MandatoryProperties
            $sMsg &= "Mandatory schema object properties..." & @CRLF
            For $sPropName In $colIISManProp
                $sMsg &= $sPropName & @CRLF
            Next
            $sMsg &= @CRLF
            
            ; List optional properties in the schema
            $colIISOptProp = $oIISSchema.OptionalProperties
            $sMsg &= "Optional schema object properties..." & @CRLF
            For $sPropName In $colIISOptProp
                $sMsg &= $sPropName & @CRLF
            Next
            $sMsg &= @CRLF
            
            ; List selected string properties
            Local $sStringVal, $avStringProps[5] = ["KeyType", "AppRoot", "AppFriendlyName", "AnonymousUserName", "AnonymousUserPass"]
            $sMsg &= "Selected string property values..." & @CRLF
            For $n = 0 To UBound($avStringProps) - 1
                $sStringVal = $oIISPath.Get ($avStringProps[$n])
                $sMsg &= $avStringProps[$n] & " : " & $sStringVal & @CRLF
            Next
            $sMsg &= @CRLF

            ; List selected boolean properties
            Local $bBoolVal, $sBoolVal, $avBoolProps[9] = ["AuthAnonymous", "AccessRead", "AccessScript", "EnableDirBrowsing", "DirBrowseShowDate", "DirBrowseShowLongDate", "DirBrowseShowTime", "DirBrowseShowSize", "DirBrowseShowExtension"]
            $sMsg &= "Selected boolean property values..." & @CRLF
            For $n = 0 To UBound($avBoolProps) - 1
                $sBoolVal = "False"
                $bBoolVal = $oIISPath.Get ($avBoolProps[$n])
                If $bBoolVal Then $sBoolVal = "True"
                $sMsg &= $avBoolProps[$n] & " : " & $bBoolVal & " : " & $sBoolVal & @CRLF
            Next
            $sMsg &= @CRLF
            
            ; Enumerate the data paths
            Local $ChildObject, $ChildObjectName
            $sMsg &= "Enumerated path objects..." & @CRLF
            For $ChildObject In $oIISPath
                $ChildObjectName = $ChildObject.AdsPath
                $sMsg &= "Untrimmed path : " & $ChildObjectName & @CRLF
                $ChildObjectName = StringTrimLeft($ChildObjectName, 6)
                $ChildObjectName = StringTrimLeft($ChildObjectName, StringInStr($ChildObjectName, "/"))
                $sMsg &= "Trimmed path   : " & $ChildObjectName & @CRLF
            Next
            $sMsg &= @CRLF
            
            ; Return results
            Return $sMsg
        Else
            SetError(1, 2, 0)
        EndIf
    Else
        SetError(1, 1, 0)
    EndIf
EndFunc   ;==>_IIS_Enum


;--------------------------------------
; Function _ComErrFunc()
;   Custom COM object error handler
;--------------------------------------
Func _ComErrFunc()
    Local $HexNumber = Hex($oComError.number, 8)
    MsgBox(16, "Test", "AutoIT COM Error Occured!" & @CRLF & _
            @TAB & "Error Number: " & $HexNumber & @CRLF & _
            @TAB & "Line Number: " & $oComError.scriptline & @CRLF & _
            @TAB & "Description: " & $oComError.description & @CRLF & _
            @TAB & "WinDescription: " & $oComError.windescription)
    SetError(1, -1, ""); something to check for when this function returns
EndFunc   ;==>_ComErrFunc

Thanks for the help!

:)

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
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...