Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBSCRIPT with IIS7

Status
Not open for further replies.

ajtsystems

IS-IT--Management
Jan 15, 2009
80
GB
Hi,

I am trying to query an install of IIS 7 using VBscript and WMI but am having some problems. From what I can see there are e ways this is possible
1: IIS 7 WMI using XML
2: WMI using IIS 6
3: IIS 7 WMI

I'd like to use WMI IIS 7 and XML as the servers I will be working on only have this component installed and not any IIS 6 backward compatibility.

I need to query each site for the default path i.e "C:\inetpub\test" and pull this into to afile...


Can anyone point me int he right direction?

Thanks

James
 
this is in the registry and should be easily obtainable.

32-bit: HKLM\SOFTWARE\Microsoft\InetStp\Path64-bit: HKLM\SOFTWARE\[green]Wow6432Node[/green]\Microsoft\InetStp\Path
Code:
set objShell = CreateObject("WScript.Shell")
strKey = "HKLM\SOFTWARE\Microsoft\InetStp\Path[URL unfurl="true"]WWWRoot"[/URL]
strPath = objShell.RegRead(strKey)

-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
I may have led you astray. The suggestion above seems to get the default doc root for a new site. If the site's physical path was altered, this key would not reflect this. You would have to open the sites web.config file and parse out the physical path. The problem is, this file is located in the path you need to find! But, you can use IIS7 cmdline apps to get this.

This cmdline will copy the config file for the site named "Default Web Site" to config.txt
Code:
c:\> %windir%\system32\inetsrv\AppCmd.exe List Config "Default Web Site" > config.txt

Then, open config.txt and parse out "physicalPath"

perhaps something like.

Code:
set objShell = CreateObject("Wscript.Shell")
set objFSO = CreateObject("Scripting.FileSystemObject")

strSiteName = "MySiteName"
objShell.Run "%comspec% /c %windir%\system32\inetsrv\AppCmd.exe List Config """ & strSiteName & """ > c:\config.txt", 1, true

set objFile = objFSO.OpenTextFile("c:\config.txt", 1, true, 0)

do while NOT (objFile.AtEndOfStream)
   strLine = objFile.ReadLine
   intPos = inStr(strLine, "physicalPath=")
   if (intPos) then
      strPath = mid(strLine, intPos + 14, inStrRev(strLine, chr(34)) - intPos - 14)
      exit do
   end if
loop

msgbox strPath

-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Hi Geates,

I have amde some changes as I can't really have and text files kicking around..it's still using appcmd.exe:

set objShell = CreateObject("Wscript.Shell")
set objFSO = CreateObject("Scripting.FileSystemObject")


strSiteName = "jamess"


'Run appcmd and list complete config for the site (Strsitename)
Set objExecObject=objShell.Exec("%comspec% /c %windir%\system32\inetsrv\AppCmd.exe List Config """ & strSiteName)

'Look through the output for the Physical Path
Do While Not objExecObject.StdOut.AtEndOfStream
strText = objExecObject.StdOut.ReadLine()
intPos = inStr(strText, "physicalPath=")
if (intPos) then
strPath = mid(strText, intPos + 14, inStrRev(strText, chr(34)) - intPos - 14)
exit do
end if
loop


msgbox strPath

Ideally I need to use the scriot below to get the websites in my IIS instance to pass through to the code you wrote to you variable: strSiteName = "jamess". Once I have the path for a specific site I can then try to modify the path and back it up.


Set objExecObject=objShell.Exec("%comspec% /c %windir%\system32\inetsrv\AppCmd.exe List site """ & strSiteName)
'Look through the output for the Physical Path
Do While Not objExecObject.StdOut.AtEndOfStream
strTextSite = objExecObject.StdOut.ReadLine()

Set re = new RegExp 'new regular expressions object
re.IgnoreCase = true 'ignore case when matching
re.Global = false 'stop after first match
re.Pattern = """(.*)?""" 'pattern to match
set matches = re.Execute(strTextSite) 'match with contents of foo variable
wscript.echo matches.item(0) 'contents of title tag


loop

It uses regular expressions to find the site name which is between speech marks.

Would I need a function to pass the site through to a Sub so I can loop through and get the paths?

TNKs

James


 
I'm not familiar with regex syntax, only concept. I'm not sure I follow your question.

However, the code below assume I do

Code:
function getPath(strSite)
    set objShell = CreateObject("WScript.Shell")
    set objExecObject = objShell.Exec("%comspec% /c %windir%\system32\inetsrv\AppCmd.exe List site """ & strSite & """")

    [green]'No need to reinitialize what doesn't change.[/green]
    set re = new RegExp 'new regular expressions object
    re.IgnoreCase = true 'ignore case when matching
    re.Global = false 'stop after first match
    re.Pattern = """(.*)?""" 'pattern to match

    [green]'Look through the output for the Physical Path[/green]
    do while not objExecObject.StdOut.AtEndOfStream
        strLine = objExecObject.StdOut.ReadLine()
        set objMatches = re.Execute(strLine) 'match with contents of foo variable
    loop
    getPath = objMatches.item(0) 'contents of title tag
end function

msgbox getPath("MySite")

-Geates

PS. StdOut is a good alternative!

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Hi,
I have managed to get it to work but want to get the code to do remote machines from a text file using an array, this code works OK locally How hard would this be?

Set oWebAdmin = GetObject("winmgmts:root\WebAdministration")
Set paths = oWebAdmin.ExecQuery( "SELECT * FROM application" )

For Each sitepath In paths
wscript.echo sitepath.sitename
wscript.echo sitepath.path

next
 
Probably not to hard. Although, I'm not sure this works. No need for an array. This also assumes a simple list of computer names, "file.txt"

file.txt
--------
ComputerA
ComputerB
ComputerC
Server1
Server2


Code:
set objFSO = CreateObject("Script.FileSystemObject")
set objList = objFSO.OpenTextFile("file.txt", 1, true, 0)

do while NOT (objList.AtEndOfFile)
   strComputer = objList.ReadLine
   set objWebAdmin = GetObject("winmgmts:[green]{impersonationLevel=impersonate}!\\" & strComputer & "\[/green]root\WebAdministration")
   set colSites = objWebAdmin.ExecQuery( "SELECT * FROM application" )
   for each objSite in colSites
      msgbox "Site: " & objSite.sitename & vbNewLine & "Path: " & objSite.path
   next 
loop

-Geates

"I hope I can feel and see the change - stop the bleed inside a feel again. Cut the chain of lies you've been feeding my veins; I've got nothing to say to you!"
-Infected Mushroom

"I do not offer answers, only considerations."
- Geates's Disclaimer
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top