Publishing what I learn

SharePoint and other geekoid tidbits

Get the ID of an IIS 6.0 site from a script

This took me a while to cobble together from various sources. It’s VBScript that takes either the path of a site or its description (technically, its ‘ServerComment’, but it’s what you see in the ‘Description’ column of IIS Manager or the ‘Description’ box of a site’s properties) and returns its Identifier.

Save this as GetSiteID.vbs:

Function GetServerComment(SiteNumber)
	Set IISWebSite = GetObject("IIS://localhost/w3svc/" & SiteNumber)
	GetServerComment = IISWebSite.ServerComment
	Set IISWebSite = Nothing
End Function

Function GetPath(SiteNumber)
	Set IISWebSiteRoot = GetObject("IIS://localhost/w3svc/" & SiteNumber & "/root")
	GetPath = IISWebSiteRoot.Path
	Set IISWebSiteRoot = Nothing
End Function

Function GetSiteIDByDescription(ServerComment)
	Set IISObj = GetObject("IIS://localhost/w3svc")
	For Each Web In IISObj
		If (Web.Class = "IIsWebServer") Then
			If (GetServerComment(Web.Name) = ServerComment) Then
				GetSiteIDByDescription = Web.Name
			End If
		End If
	Next
	Set IISObj = Nothing
End Function

Function GetSiteIDByPath(Path)
	Set IISObj = GetObject("IIS://localhost/w3svc")
	For Each Web In IISObj
		If (Web.Class = "IIsWebServer") Then
			If (GetPath(Web.Name) = Path) Then
				GetSiteIDByPath = Web.Name
			End If
		End If
	Next
	Set IISObj = Nothing
End Function

Set colNamedArguments = WScript.Arguments.Named

If colNamedArguments.Exists("description") Then
	returnValue = GetSiteIDByDescription(colNamedArguments("description"))
ElseIf colNamedArguments.Exists("d") Then
	returnValue = GetSiteIDByDescription(colNamedArguments("d"))
End If

If colNamedArguments.Exists("path") Then
	returnValue = GetSiteIDByPath(colNamedArguments("path"))
ElseIf colNamedArguments.Exists("p") Then
	returnValue = GetSiteIDByPath(colNamedArguments("p"))
End If

Set colNamedArguments = Nothing

If len(returnValue) > 0 Then
	WScript.Echo returnValue
	WScript.Quit returnValue
Else
	WScript.echo "Usage: GetSiteID.vbs [/description:] [/path:]"
	WScript.Quit -1
End If

You can use this batch file to call the VBScript file if it’s in the same directory:

@Echo off
SetLocal
Set siteName=mysite.co.uk
GetSiteID /description:%siteName%
REM If that way doesn't work do it this way:
REM cscript GetSiteID.vbs /description:%siteName%
Set siteID=%ErrorLevel%
REM Now do something with that Identifier
Echo The ID of the site named '%siteName%' is %siteID%
EndLocal

I expect to use this when I’ve created a site with the Iisweb.vbs script and then need to make further changes to it with Adsutil.vbs.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: