stsadm operation automation question
I want to automate some stsadm tasks - some manually initiated, some I might put on a schedule.I'm hoping someone can help me out here.Here's what I want toaccomplish:1) Retrieve a list of web apps from a farm2) enum site collections in each web app3) Run stsadm operations on each site collection in the resulting listI am willing to manually update a file (not sure what file type or format) with the web apps list.I know I can dump the results of an enumsites to an xml file, but don't know how to retrieve that info and insert it into a stsadm command.I would prefer suggestions using eitherVBScript orPowerShell as I don't have great skill at C# or VB.Net and want to be able to expand on these ideas in the future. Anyone want to take a crack at this one for me?Thanks.Kevin HughesSharePoint Database AdministratorHNTB Companies
August 22nd, 2008 4:39pm
To me... this is sounding more like a windows service than a script. The code to do this is actually quite simple if you have even a little C# or VB.NET skills.You can probably find great examples on the following objects to help along the way: SPSite, SPWeb, SPListThese objects are pretty simple to use. If you decide to go this route and need a couple of resources, let me know.Shannon Bray - MCT, MCPD, MCTS, MCITP
Free Windows Admin Tool Kit Click here and download it now
August 23rd, 2008 4:09am
It really just depends on what your goals are (what is that you are wanting to do with each site collection?). PowerShell may be the way to go as it's a lot easier to build and customize and deploy than a windows service (not that a windows service is difficult but if you already have powershell experience that will get you going quicker). Another option would be to build your own stsadm command (would require some c# experience) - there's a codeplex project that I've got that will help get you started if you go this route: http://www.codeplex.com/customstsadmtemplate or you can download my extensions from here: http://stsadm.blogspot.com- it's possible I may already have something that does what you need.
August 23rd, 2008 9:22pm
Hi,
Here are some useful PowerShell articles and a command pack for your reference:
1. Managing SharePoint with PowerShell, Part 1 (http://sharepoint.microsoft.com/blogs/zach/Lists/Posts/Post.aspx?ID=7)
2. Managing SharePoint with PowerShell: Part 2 (http://sharepoint.microsoft.com/blogs/zach/Lists/Posts/Post.aspx?ID=21)
3. PowerShell Pack for SharePoint (http://code.msdn.microsoft.com/psp4sp)
However, this is not the best place to discuss programming issues, so If the information cannot satisfy you, I can help you move to the SharePoint - Development and Programming.
Hope the information can be helpful.
-lambertPosting is provided "AS IS" with no warranties, and confers no rights.
Free Windows Admin Tool Kit Click here and download it now
August 25th, 2008 10:34am
Kevin,
Please refer to this blog post
http://spadmin.spaces.live.com/Blog/cns!F030C52B8E5517C3!347.entry
This shows you how to make use of BAT file which takes input from text files and then does some processing using stsadm for each line of input from text file.
This is very poweful and i have used it for lot of automation tasks.
Sameer Dhoot | http://sharemypoint.in
August 25th, 2008 5:13pm
Hi Kevin,I posted a script a few weeks ago that might be useful:You can download it from here:http://james.milne.com/scripts/SiteWalker.zipInside you will find a script called WalkSites.vbs. In a nutshell it does a STSAdm command to dump the site collections to a sites.xml file then it loops through each one and then executes a STSAdm Export command on each site collection. You can tweak the vbs to get it to run pretty much any command against each site.Hope this helps,Regards,James.
Free Windows Admin Tool Kit Click here and download it now
August 25th, 2008 6:38pm
Gary,I use the STSADM extensions you built very often. Thank you so much for all the work put into them. I didn't see anything in your selection, though, that would do what I needed.I am a poor coder when it comes to C# and VB.Net, but do quite well with VBScript and PowerShell (long-time sysadmin who liked to automate). I also need to be able to pass this on to other members of our SharePoint Ops team that are even less savy with coding than I am. I understand the code you wrote, but could not duplicate it.
August 27th, 2008 6:09pm
Lambert,Thank you for the PowerShell links. I love PowerShell, so will probably use as much of what you've refereced as I can.The only issue with it is that my manager doesn't know PowerShell, so not sure how he'll receive it if I work in it. Oh well....will make life easier for a bit.
Free Windows Admin Tool Kit Click here and download it now
August 27th, 2008 6:29pm
Kevin, Just being curious, how do u retrieve a list of web apps from a farm?
Use the following example to read from an XML.
Thanks, Ruben
Option Explicit
Const SITE_URL = "http://myurl/"
Const STSADM = "C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN\stsadm"
dim objShell
dim objExec
dim strResult
dim objXml
dim objSc
dim objUrl
dim strCmd
Set objShell = CreateObject("WScript.Shell")
Set objExec = objShell.Exec(STSADM & " -o enumsites -url " & SITE_URL)
strResult = objExec.StdOut.ReadAll
Set objXml = CreateObject("MSXML2.DOMDocument")
objXml.LoadXML(strResult)
For Each objSc in objXml.DocumentElement.ChildNodes
strUrl = objSc.Attributes.GetNamedItem("Url").Text
‘now u have the value from the node “Url” into strUrl ;)
Next
October 16th, 2009 5:41pm
I really like this script / solution above all others that I have found I added some code to get some logging, and enumerate alternate domains (web apps)
The only thing that needs to be specified is a backup location, and maybe the retention (set for 28 days);
On Error Resume Next
Dim boolLoggingEnabled
strBackupLocation = "F:\backup\Sharepoint"
Dim strLogFileName : strLogFileName = strBackupLocation & "\Backup.log"
boolLoggingEnabled = True
'Get Date
str1 = FormatDateTime(Date, 2)
strdate = Replace(str1, "/", Chr(46))
'Dim BackupFileName
Dim strFileName
Dim strBackupFileName
Dim strBackupLocation
Dim sites, site
Dim siteCount
Dim stsPath
Dim strdate
Dim strWebApp
stsPath = "C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN"
WScript.Echo "Enumerating Web Apps..."
Log "write", strLogFileName, "Enumerating Web Apps..."
WScript.Echo " "
Set oShell = CreateObject("WScript.Shell")
Set oWshScriptExec = oShell.Exec(stsPath & "\stsadm.exe -o enumalternatedomains")
Set oStdOut = oWshScriptExec.StdOut
sOutput = oStdOut.ReadAll
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.loadXML(sOutput)
CreateFile XMLFileName(SiteURL), xmlDoc.xml
WScript.Echo sOutput
Set domain = xmlDoc.documentElement.selectSingleNode("//IncomingUrl")
domain = Int(siteCount.attributes.getNamedItem("IncomingUrl").text)
'WScript.Echo "SiteCount: " & siteCount
Set domains = xmlDoc.documentElement.selectNodes("//IncomingUrl")
For Each domain In domains
url = domain.text
WScript.Echo "Domain: " & url
'Get BackupFileName
BackupFileName(url)
'Perform Site (Web Application) Backup
Backupsite (url)
'Fetch Site Collections
WScript.Echo "Fetching Site Collections for " & url
Log "write", strLogFileName, "Fetching Site Collections for " & url
Set xmlDoc = CreateObject("Microsoft.XMLDOM")
xmlDoc.async = "false"
Set stylePI = xmlDoc.createProcessingInstruction("xml-stylesheet", "type=""text/xsl"" href=""sites.xsl""")
'xmlDoc.appendChild(stylePI)
xmlDoc.loadXML(GetSitesXML())
xmlDoc.insertBefore stylePI, xmlDoc.firstChild
'xmlDoc.(GetSitesXML())
CreateFile "Default.xml", xmlDoc.xml
WScript.Echo xmlDoc.xml
Set siteCount = xmlDoc.documentElement.selectSingleNode("//Sites")
siteCount = Int(siteCount.attributes.getNamedItem("Count").text)
Set sites = xmlDoc.documentElement.selectNodes("//Sites/Site")
If (sites.length = siteCount) Then
WScript.Echo "Found [" & siteCount & "] Site Collections."
Log "write", strLogFileName, "Found [" & siteCount & "] Site Collections."
Else
WScript.Echo "Fatal Error: Unable to determine the number of site collections; " & sites.length & " does not equal " & siteCount & "."
WScript.Quit
End If
For Each site In sites
Dim SubSiteName
SubSiteName = site.attributes.getNamedItem("Url").text
ProcessSubSite(SubSiteName)
Next
Next
CleanUp
Log "write", strLogFileName, "EOF..."
'End
' --------------------
' - Helper Functions -
' ----------------------------------------------------------------------
' GetSitesXML()
' This function calls STSAdm to find the Sites Collections listed on http://localhost
Function GetSitesXML()
Set oShell = CreateObject("WScript.Shell")
Set oWshScriptExec = oShell.Exec(stsPath & "\stsadm.exe -o enumsites -url "& url)
Set oStdOut = oWshScriptExec.StdOut
GetSitesXML = oStdOut.ReadAll
End Function
' Backup()
' This function calls STSAdm to find the Sites Collections listed on http://localhost
Function ProcessSubSite(SiteURL)
'WScript.Echo "Processing Site: " & Chr(34) & SiteURL & Chr(34)
'Log "write", strLogFileName, "Processing Site: " & Chr(34) & SiteURL & Chr(34)
WScript.Echo " "
Set oShell = CreateObject("WScript.Shell")
Set oWshScriptExec = oShell.Exec(stsPath & "\stsadm.exe -o enumsubwebs -url " & Chr(34) & SiteURL & Chr(34))
Set oStdOut = oWshScriptExec.StdOut
sOutput = oStdOut.ReadAll
Set xmlDoc=CreateObject("Microsoft.XMLDOM")
xmlDoc.loadXML(sOutput)
CreateFile XMLFileName(SiteURL),xmlDoc.xml
WScript.Echo sOutput
Set siteCount=xmlDoc.documentElement.selectSingleNode("//Subwebs")
siteCount=Int( siteCount.attributes.getNamedItem("Count").text)
WScript.Echo "SiteCount: " & siteCount
If (siteCount > 0) Then
Set SubWebs = xmlDoc.documentElement.selectNodes("//Subwebs/Subweb")
For Each SubWeb In SubWebs
WScript.Echo "Subweb: " & SubWeb.text
If InStr(SubWeb.text, "http://") Then
BackupFileName (Subweb.text)
Log "write", strLogFileName, "Processing: " & SubWeb.text
Log "write", strLogFileName, "Exporting subweb: " & SubWeb.text & " to: " & strBackupLocation & Chr(92) & strBackupFileName
Exportsite Subweb.text
End If
ProcessSubSite( SubWeb.text )
Next
End If
ProcessSubSite = oWshScriptExec.Status
End Function
Function Backupsite(strURL)
'Shell "cmd /c stsadm.exe -o setsitelock -url " & strURL & " -lock readonly"
Shell "cmd /c stsadm.exe -o backup -url " & strURL & " -filename " & strBackupLocation & Chr(92) & strBackupFileName
'Shell "cmd /c stsadm.exe -o setsitelock -url " & strURL & " -lock none"
End Function
Function Exportsite(strURL)
Shell "cmd /c stsadm.exe -o export -url " & strURL & " -includeusersecurity -filename " & strBackupLocation & Chr(92) & strBackupFileName
End Function
' FileName ( strURL )
' Used to create a _nice_ filename for the backup file.
' Take the strURL and remove bad characters from the string.
Function XMLFileName(strURL)
XMLFileName= Replace(Replace(Replace(Replace( _
strURL,_
"http://", ""),_
":", ""),_
" ", "_"),_
"/", "_") &_
".xml"
End Function
Function BackupFileName(strURL)
strBackupFileName = Replace(Replace(Replace(Replace( _
strURL, _
"http://", ""), _
":", "_"), _
" ", "_"), _
"/", "_") & _
Chr(95) & strdate & ".cab"
End Function
Function CleanUp()
WScript.Echo " "
WScript.Echo "Cleaning Up: " & strBackupLocation & ".."
Dim Fso
Dim Directory
Dim Modified
Dim Files
Set Fso = CreateObject("Scripting.FileSystemObject")
Set Directory = Fso.GetFolder(strBackupLocation)
Set Files = Directory.Files
For Each Modified In Files
If DateDiff("D", Modified.DateLastModified, Now) > 28 Then Delete Modified
Next
'CreateObject("WScript.Shell").Run("forfiles /P "&strBackupLocation&" /D -"&PurgeDays&" /C "&Chr(3)4&"cmd /c del @Path" & Chr(34))
End Function
Sub CreateFile( file, myContent )
Set oFSO = CreateObject("Scripting.FileSystemObject")
path = Mid( file, 1, InStrRev( file, "\" ) )
Set write_file = oFSO.CreateTextFile( file, True)
write_file.Write myContent
Set write_file = Nothing
End Sub
Function Shell(command)
On Error Resume Next
Log "write", strLogFileName, "Running command: " & command
Err.Clear
Set wshShell = CreateObject("Wscript.Shell")
wshShell.Run command, 0, 1
If err.number <> 0 Then
Log "write", strLogFileName, "Shell error: " & err.Description
End If
Set wshShell = Nothing
End Function
Function Delete(targetfile)
On Error Resume Next
Log "write", strLogFileName, "Deleting file " & targetfile
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
objFSO.DeleteFile(targetfile), 1
If err.number <> 0 Then
Log "write", strLogFileName, "Could not delete file " & targetfile & ", Error: " & Err.Description
End If
Set objFSO = Nothing
End Function
Function Log(operation, filename, message)
On Error Resume Next
Dim objFSO
Dim File
Dim TextStream
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0
Set objFSO = CreateObject("Scripting.FileSystemObject")
If operation = "delete" Then
If objFSO.FileExists(filename) Then
objFSO.DeleteFile(filename)
End If
End If
If operation = "write" Then
If boolLoggingEnabled Then
If Not objFSO.FileExists(filename) Then
objFSO.CreateTextFile(filename), 1
End If
Set File = objFSO.GetFile(filename)
Set TextStream = File.OpenAsTextStream(ForAppending, TristateUseDefault)
TextStream.WriteLine Date & " " & Time & ": " & message
Set File = Nothing
Set TextStream = Nothing
Set objFSO = Nothing
End If
End If
End Function
Free Windows Admin Tool Kit Click here and download it now
April 4th, 2011 2:34pm


