Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8246091
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:23:43+00:00 2026-06-07T22:23:43+00:00

I have created the following custom task for Cruise Control .net, but I can’t

  • 0

I have created the following custom task for Cruise Control .net, but I can’t find a single reference of how you add a custom task to the project configuration. Does anyone have a helpful example of how to add a reference to a custom task in cc.net?

(below is my new task)

public class RestoreDb : TaskBase
{
    #region Parameters

    [Required]
    public string ServerName { get; set; }
    [Required]
    public string DbName { get; set; }

    public string BackupFileName { get; set; }

    #endregion

    protected override bool Execute(ThoughtWorks.CruiseControl.Core.IIntegrationResult result)
    {
        bool returnResult = false;
        try
        {
            SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder();
            connectionStringBuilder.IntegratedSecurity = true;
            connectionStringBuilder.DataSource = ServerName;
            SqlConnection connection = new SqlConnection(connectionStringBuilder.ToString());
            connection.Open();

            Server server = new Server(new ServerConnection(connection));

            if (server.Databases[DbName] != null)
            {
                Log.Info("Dropping existing " + DbName + " on " + ServerName);
                server.Databases[DbName].Drop();
            }
            else
            {
                Log.Info(DbName + " on " + ServerName + " doesn't exist.");
            }

            Log.Info("Restoring " + DbName + " on " + ServerName);
            Database newDb = new Database(server, DbName);

            Restore rs = new Restore();
            rs.NoRecovery = false;

            FileInfo fi = new FileInfo(server.Settings.BackupDirectory + "\\" + BackupFileName);
            rs.Devices.AddDevice(fi.FullName, DeviceType.File);
            rs.Database = DbName;
            rs.Action = RestoreActionType.Database;
            rs.ReplaceDatabase = true;

            DataTable fileContents = rs.ReadFileList(server);
            string originalDbName = fileContents.Rows[0][0].ToString();
            string originalLogFileName = fileContents.Rows[1][0].ToString();
            rs.RelocateFiles.Add(new RelocateFile(originalDbName, 
            string.Format(@"C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\{0}.mdf", DbName)));

            rs.RelocateFiles.Add(new RelocateFile(originalLogFileName,
            string.Format(@"C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\{0}_1.ldf", DbName)));

            rs.SqlRestore(server);
            Log.Info("Restoring done.");
            returnResult = true; // success!
        }
        catch (Exception ex)
        {
            Log.Error(ex);
            returnResult = false;
        }
        return returnResult;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T22:23:44+00:00Added an answer on June 7, 2026 at 10:23 pm

    So I basically tossed this because there are no examples anywhere of how to add a custom task to the cc.net config. below is the powershell script I wrote, that does the exact same steps.

    $ServerName = 'servernamehere';
    $DatabaseName = 'newDbNameHere';
    $BackupFile = 'C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\Backup\backupFileHere.bak';
    if($args[0])
    {
        $ServerName = $args[0];
    }
    if($args[1])
    {
        $DatabaseName = $args[1];
    }
    if($args[2])
    {
        $BackupFile = $args[2];
    }
    
    [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.Smo")
    [reflection.assembly]::LoadWithPartialName("Microsoft.SqlServer.SmoExtended")
    try 
    {
        $Server = new-object ("Microsoft.SqlServer.Management.Smo.Server") $ServerName
    
        If ($Server.Databases.Contains($DatabaseName))
        {
            write-host "Dropping existing " $DatabaseName " on " $ServerName;
            #$Server.Databases[$DatabaseName].SetOffline();
            $Server.Databases[$DatabaseName].Drop();
        }
        else
        {
            write-host $DatabaseName " on " $ServerName " doesn't exist.";
        }
        $Database = new-object ("Microsoft.SqlServer.Management.Smo.Database") ($Server, $DatabaseName);
    
        $restore = new-object ('Microsoft.SqlServer.Management.Smo.Restore');
        $restore.NoRecovery = $false;
    
        $fil=new-object "Microsoft.SqlServer.Management.Smo.BackupDeviceItem";
        $fil.DeviceType='File';
        $fil.Name= $BackupFile;
        $restore.Action="Database";
        $restore.Devices.Add($fil);
        $restore.Database=$DatabaseName;
        $restore.ReplaceDatabase = $true;
    
        #retrieving the backup file's original database values so we can replace them
        $FileContents = $restore.ReadFileList($Server);
        $originalDbName =  $FileContents.Rows[0][0].ToString();
        $originalLogFileName = $FileContents.Rows[1][0].ToString();
    
        #replacing the bak files original file names with the new file names
        $mdf = new-object "Microsoft.SqlServer.Management.Smo.RelocateFile" ($originalDbName,
            "C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\$DatabaseName.mdf");
        $restore.RelocateFiles.Add($mdf);
        $ldf = new-object "Microsoft.SqlServer.Management.Smo.RelocateFile" ($originalLogFileName,
            "C:\Program Files\Microsoft SQL Server\MSSQL10.MSSQLSERVER\MSSQL\DATA\$DatabaseName_Log.mdf");
        $restore.RelocateFiles.Add($ldf);
    
    
        write-host "Restoring database $DatabaseName  on  $ServerName from file $BackupFile";
    
            $restore.SqlRestore($Server);
            write-host "Restore Complete";
    }
    catch [System.Exception] {
      # $_ is set to the ErrorRecord of the exception
      $currentException = $_.Exception;
      write-host "Root Exception: " $currentException.Message;
      While($currentException.InnerException)
      {
        $currentException = $_.Exception.InnerException;
        write-host "Inner Exception: " $currentException.Message;
      }
      write-host "Restore Failed.";
      exit 0; #failure
    }    
    exit 1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a custom control using VB.NET in Visual Studio 2008 that gives
friends, i have created custom title bar using following titlebar.xml file with code <?xml
I created a custom module for Orchard following this wonderful guide . I have
I have created the following project structure for my new asp.net mvc project any
I have created a custom alertdialog by the following code: AlertDialog.Builder builder; AlertDialog alertDialog;
I have created a custom form class and template for my form by following
I have created a custom data grid control. I dragged it on windows form
I have the following simple custom control that I have defined for a Windows
Unfortunately I am still working on .NET 2.0. I have not created a custom
I have created the following custom attribute to assist me with validating a required

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.