Friday, August 7, 2009

Handy VBScript for STSADM looping

Before getting into PowerShell, I used to do a lot of administrative scripting for SharePoint using VBScript.  In fact we still use a vbscript based automated farm installation that a consultant assembled for us (Thanks V-man!) – I intend on switching over to powershell in future.

The vbscripts are tried-and-true though.  Here’s a little one I like to use when I need to loop through an stsadm command multiple times.  I use it for things like adding multiple managed paths, renaming sites to clear orphans, and running database repair to clear up list and document library orphans on multiple databases, etc.

'script to add managed paths based on values in paths.csv file stored on same drive as script

'paths.csv file entry example:





Const ForReading = 1

Const cStsadmPath = """C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN\stsadm.exe"""

Dim retval
Dim objshell
Dim cmd

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile(".\paths.csv", ForReading)

Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
    arrPath = Split(strNextLine, ",")

    Set objShell = CreateObject("WScript.Shell")


    'grab webapp from CSV which is the 1st value and the managed path from 2nd value in the base 0 array to run addpath command

    cmd = cStsadmPath & " -o addpath -url http://" & arrPath(0) & arrPath(1) & " -type wildcardinclusion"

    retval =, 1, True)
    Wscript.Echo cmd

    set retval = objShell.exec(cmd)

    'Wait until command is done running before running next command

    Do Until retval.Status

          Wscript.Sleep 250


    set retval = nothing

    Set objShell = Nothing   

I have a script that enumerates the sites and throws the output to an xml file – and then the vbsscipt uses the xmldom to read in values but the output gets corrupted (proabably because I’m enumerating close to 10,000 site collections) and can’t reliably be read.  I intend on using Powershell and getting the sites through the SharePoint Object model – look for that in a future post.