Install IIS, ASP.NET, .NET Framework 3.5, SQL Server 2005 Express /w Reporting Services, a Web Application, create the Web Application database, and Deploy Report files using a batch script... Possible? Yes!!!
I often like to explore possibility and this was my challenge. So here's what I did...
I knew I wanted to have a batch file to start the installation and get everything installed, but I wasn't quite sure how to accomplish such a feat. So after some Google searches I started to put the batch script together. Here is what I found:
IIS and ASP.NET magic command: sysocmgr /i /u
http://support.microsoft.com/kb/222444
.NET Framework 3.5 magic command: dotnetfx35.exe /qb /norestart
http://msdn.microsoft.com/en-us/library/cc160716.aspx
SQL Server 2005 magic command: SQLEXPR_ADV.exe /qb /settings
http://msdn.microsoft.com/en-us/library/ms144259.aspx
Deploy Report .rdl files magic command: rs.exe -i -s
http://msdn.microsoft.com/en-us/library/ms160854.aspx
Virtual Directory created programmatically using .NET:
System.EnterpriseServices.Internal.IISVirtualRoot
System.EnterpriseServices.Internal.IISVirtualRoot.Create()
Web Application Deployment magic command: xcopy /C /E /H /R /Y
Create the Web Application Database magic command: osql -E -S -i
Extras
Modify SQL Server 2005 Surface Area Configuration magic command: sac in -F -DE -BS -N
http://msdn.microsoft.com/en-us/library/ms162800.aspx
Using the commands above I was able to deploy all of the required components for my ASP.NET Web Application. This seems to work well for Windows XP and Windows Server 2003.
This is great if you have a clean installation of XP or Server 2003 with the latest services packs and windows installer 3.1. However if the installation has other services running on it or other installations of SQL Server, more care will need to be taken into the creation of the this script and the answer files that support it. For example, by default SQL Server installs to C:\Program Files\Microsoft SQL Server\MSSQL.#\.... If there are other instances of SQL Server installed, then you'll need to know what the exact installation path is. As an added side effect of running this from a batch command, the environment variables for SQL installation locations are not set until after SQL Server is installed. Similar problems appear with the deployment location for the web app and there are no checks to determine if the components are already installed.
Another important caveat is that batch scripts, when starting an external program, do not automatically wait for the program to finish before continuing. For that problem I added some delay loops that look for the application name in the computers running programs list and ping localhost as a delay between checks.
Now that we know this works, but requires a bit more diligence if the circumstances are not perfect, lets look at exactly what I did.
First, I install IIS using sysocmgr.exe and I pass it 2 parameters
/i:%windir%\inf\sysoc.inf // this is required
/u:"%CD%\iis\iis_install.txt" // This is the path to my answer file that tells sysocmgr what to install.
That iis_install.txt simply contains tags set to on or off and the file looks like this:
[Components]
aspnet=on
iis_asp=on
iis_serversideincludes=on
iis_common=on
iis_www=on
iis_inetmer=on
fp_extensions=on
iis_doc=on
Second, I install the .NET Framework 3.5 using the switches:
/qb // Shows the progress, but does not require user input.
/norestart // Keeps the computer from reboot after installation.
Third, I installed SQL Server 2005 Express /w Advanced Services passing 2 parameters:
/qb // Shows the progress, but does not require user input
/settings "%CD%\SQL\sql_install.ini" // This is the path to the answer file that tells SQLEXPR_ADV.exe what I want to install.
That sql_install.ini file looks like this:
[Options]
INSTANCENAME=SQLExpress
ADDLOCAL=SQL_Engine,SQL_Data_Files,SQL_SSMSEE,RS_Server,RS_Web_Interface,Client_Components,Connectivity
SQLAUTOSTART=1
AGAUTOSTART=1
RSAUTOSTART=1
SECURITYMODE=SQL
SAPWD=password
RSCONFIGURATION=Default
RSSQLLOCAL=1
DISABLENETWORKPROTOCOLS=0
Fourth, I modified the install SQL Surface Area configuration using a previously exported configuration from my test deployment:
"%programfiles%\Microsoft SQL Server\90\Shared\sac.exe" in "%CD%\SQL\sqlsac.out" -F -DE -BS -N // This imports the configuration and the switches determine which components to import.
Fifth, I use osql to connect to the SQL Server instance and run my transact SQL script to create the database.
Sixth, I have to call my custom .NET Console application VirtualDirectoryInstaller.exe to create the folder in the wwwroot directory and create the virtual directory in IIS. For easy modification later I created an XML document that contains the parameters for the Create() method and the folder path. That source code looks like this:
XmlDocument xDoc = new XmlDocument();
xDoc.Load("InstallPaths.xml");
// Load the required information to create the Virtual directory
string folderPath = xDoc.SelectSingleNode("//iisFolder").Attributes["value"].Value;
string virtualDirectory = xDoc.SelectSingleNode("//iisVirtualDirectory").Attributes["value"].Value;
string iisRoot = xDoc.SelectSingleNode("//iisDefaultSiteRoot").Attributes["value"].Value;
// Create the folder
System.IO.Directory.CreateDirectory(folderPath);
// Create the virtual Directory
System.EnterpriseServices.Internal.IISVirtualRoot vr = new System.EnterpriseServices.Internal.IISVirtualRoot();
string sError;
vr.Create(iisRoot, folderPath, virtualDirectory, out sError);
Console.WriteLine(sError.ToString());
Seventh, I used xcopy to deploy the web application to the virtual directory:
xcopy "WebApp\*" "C:\inetpub\wwwroot\WebApp\*" /C /E /H /R /Y
Eigth and finally, I create the required data source and deploy the report files using rs.exe utility that is install with Reporting Services, passing it 2 parameters:
-i "%CD%\Reports\ReportsDeploy.rss" // This is the path to my report deployment script, which is basically a VB.NET code file with a .rss extension.
-s http://localhost/reportserver$sqlexpress // This is the path to the server where these reports should be deployed
That ReportsDeploy.rss file looks similiar to this:
'=============================================================================
' File: ReportsDeply.rss
'
'
' 1.3.1 Use the script to publish the reports to a report server.
'
' rs -i ReportsDeply.rss -s http://localhost/reportserver$sqlexpress
'
Dim definition As [Byte]() = Nothing
Dim warnings As Warning() = Nothing
Dim parentFolder As String = ""
Dim parentPath As String = "/" + parentFolder
Dim filePath As String = "WebAppReports\"
Public Sub Main()
rs.Credentials = System.Net.CredentialCache.DefaultCredentials
'Create the WebApp shared data sources
CreateSampleDataSource("dsWebApp", "SQL", "data source=(local)\SQLExpress;initial catalog=WebAppDatabase;User Id=webappusr;Password=password")
'Publish the reports
PublishReport("Report 1")
PublishReport("Report 2")
PublishReport("Report 3")
PublishReport("Report 4")
PublishReport("Report 5")
End Sub
'==================================================================
The CreateSampleDataSource() and PublishReport() functions are from the PublishSampleReports.rss, which you can either install with SQL Server installation or Reporting Services Samples from CodePlex here:
http://www.codeplex.com/SqlServerSamples#ssrs
And that is it. If you were ever wondering how you can install all of the necessary components for a Web application using the command line, here it is.
Below you will find the full batch script that makes it all happen.
**************************************
The Full Batch Script
**************************************
@echo # Installing IIS
sysocmgr /i:%windir%\inf\sysoc.inf /u:"%CD%\iis\iis_install.txt"
echo off
:loopIIS
tasklist | find /i "sysocmgr.exe" >nul
if %errorlevel% EQU 0 (
ping 127.0.0.1 -n 3 >nul
goto loopIIS
)
echo on
@echo # Installing .NET Framework 3.5
DotNet\dotNetFx35.exe /qb /norestart
echo off
:loopDOTNET
tasklist | find /i "dotnetfx35.exe" >nul
if %errorlevel% EQU 0 (
ping 127.0.0.1 -n 3 >nul
goto loopDOTNET
)
echo on
@echo # Installing SQL Server 2005 Express w/ Reporting Services
SQL\SQLEXPR_ADV.exe /settings "%CD%\SQL\sql_install.ini" /qb
echo off
:loopSQL
tasklist | find /i "setup.exe" >nul
if %errorlevel% EQU 0 (
ping 127.0.0.1 -n 3 >nul
goto loopSQL
)
echo on
@echo # Modify the Surface Area Configuration to allow remote connections
"%programfiles%\Microsoft SQL Server\90\Shared\sac.exe" in "%CD%\SQL\sqlsac.out" -F -DE -BS -N
@echo # Grant NT Authority\Network Service RSExecRole
"%programfiles%\Microsoft SQL Server\90\Tools\Binn\osql" -E -S .\SQLExpress -i "%CD%\DatabaseScripts\GrantNTAuthoritySQLRS.sql"
:loopNETWORKSERVICE
tasklist | find /i "osql.exe" >nul
if %errorlevel% EQU 0 (
ping 127.0.0.1 -n 3 >nul
goto loopNETWORKSERVICE
)
@echo # Installing Database
"%programfiles%\Microsoft SQL Server\90\Tools\Binn\osql" -E -S .\SQLExpress -i "%CD%\DatabaseScripts\install.sql"
:loopDATABASE
tasklist | find /i "osql.exe" >nul
if %errorlevel% EQU 0 (
ping 127.0.0.1 -n 3 >nul
goto loopDATABASE
)
@echo # Installing Application Files
@echo # Create the Virtual Directory in IIS
VirtualDirectoryInstaller.exe
echo off
:loopWebApp
tasklist | find /i "VirtualDirectoryInstaller.exe" >nul
if %errorlevel% EQU 0 (
ping 127.0.0.1 -n 3 >nul
goto loopWebApp
)
echo on
@echo # Copy the web application files to the new virtual directory
xcopy "WebApp\*" "C:\inetpub\wwwroot\WebApp\*" /C /E /H /R /Y
@echo # Deploy the Reports
"%programfiles%\Microsoft SQL Server\90\Tools\Binn\rs.exe" -i "%CD%\Reports\ReportsDeploy.rss" -s http://localhost/reportserver$sqlexpress
@echo # Restart SQL Server for changes to take effect
net stop "SQL Server (SQLEXpress)" && net start "SQL Server (SQLExpress)"