MSDeploy API Scenarios

This C# code given below will demonstrate how the MSDeploy API can accomplish some common scenarios that users may want to perform.

The following examples are documented below in no particular order:

I. Server Sync IIS6.0 to IIS6.0 with ‘whatif’,deleting allowed and no content

II. Sync with tracing ( verbose is demonstrated )

III. Skip directive usage

IV. Replace Rule + Skip Rule ( using skipAction ) usage

V. Sync a site from iis7 to Hostable web core ( iis7 )

VI. SetAcl on a directory ( setting provider settings )

If you would like other examples posted or have a request please post below and it could be submitted in the future.

I. Server Sync IIS6.0 to IIS6.0 with ‘whatif’,deleting allowed and no content –  Setup a server with no content, just the settings and configuration moving from one IIS6.0 box to another IIS6.0 box allowing deletion

 

msdeploy –verb:sync –source:metakey,computername=computer,username=user,password=password –whatif –disableLink:ContentExtension –disableRule:DoNotDeleteRule

using System;
using Microsoft.Web.Deployment;

namespace MSDeploy.Web.Deployment
{
    class Program
    {
        static void Main(string[] args)
        {
            DeploymentBaseOptions sourceBaseOptions = new DeploymentBaseOptions();
            DeploymentBaseOptions destBaseOptions = new DeploymentBaseOptions();
            DeploymentSyncOptions syncOptions = new DeploymentSyncOptions();

            // setup remote credential information to source machine
            sourceBaseOptions.ComputerName = "computer";
            sourceBaseOptions.UserName = @"user";
            sourceBaseOptions.Password = "password";

            // setup remote credential information to destination machine
            destBaseOptions.ComputerName = "computer";
            destBaseOptions.UserName  = @"user";
            destBaseOptions.Password = "password";

            // use whatif to avoid any operations from happening, but set delete destination to true to see how much
            // will be deleted if this sync operation occurred
            syncOptions.WhatIf = true;
            syncOptions.DeleteDestination = true;

            // remove these rules to allow deletions of destination
            syncOptions.Rules.Remove("BlockHarmfulDeleteOperations");
            syncOptions.Rules.Remove("BlockUnsupportedDeleteOperations");

            // disableLink:contentExtension, must be done for both source and destination base options
            foreach (DeploymentLinkExtension extension in sourceBaseOptions.LinkExtensions)
            {
                if (extension.Name == "ContentExtension")
                {
                    extension.Enabled = false;
                    break;
                }
            } 

            foreach (DeploymentLinkExtension extension in destBaseOptions.LinkExtensions)
            {
                if (extension.Name == "ContentExtension")
                {
                    extension.Enabled = false;
                    break;
                }
            }

            // perform a sync using webserver60 provider, if doing iis7 to iis7 this should be apphostconfig provider
            DeploymentObject deploymentObject = DeploymentManager.CreateObject("webserver60", "", sourceBaseOptions);

            // collect and report all the changes that would happen
            DeploymentChangeSummary changes = deploymentObject.SyncTo(destBaseOptions, syncOptions);

            Console.WriteLine("BytesCopied:       " + changes.BytesCopied.ToString());
            Console.WriteLine("Added:             " + changes.ObjectsAdded.ToString());
            Console.WriteLine("Updated:           " + changes.ObjectsUpdated.ToString());
            Console.WriteLine("Deleted:           " + changes.ObjectsDeleted.ToString());
            Console.WriteLine("Errors:            " + changes.Errors.ToString());
            Console.WriteLine("Warnings:          " + changes.Warnings.ToString());
            Console.WriteLine("ParametersChanged: " + changes.ParameterChanges.ToString());
            Console.WriteLine("TotalChanges:      " + changes.TotalChanges.ToString());
        }
    }
}
 

II. Sync with verbose tracing -  Enable tracing of verbose events during a sync operation by adding into their event handlers.  This may allow you to troubleshoot or simply confirm certain operations take place after a sync

 

msdeploy –verb:sync –source:metaKey=“Default Web Site”,computername=sourcemachine –dest:metaKey=”Default Web Site”,computername=destMachine –verbose –whatif

 

        static void Main(string[] args)
        {
            DeploymentBaseOptions sourceBaseOptions = new DeploymentBaseOptions();
            DeploymentBaseOptions destBaseOptions = new DeploymentBaseOptions();
            DeploymentSyncOptions syncOptions = new DeploymentSyncOptions();
    
            sourceBaseOptions.Trace += new EventHandler<DeploymentTraceEventArgs>(Source_Trace);
            sourceBaseOptions.TraceLevel = System.Diagnostics.TraceLevel.Verbose;
            sourceBaseOptions.ComputerName = "sourcemachine";

            destBaseOptions.Trace += new EventHandler<DeploymentTraceEventArgs>(Dest_Trace);
            destBaseOptions.TraceLevel = System.Diagnostics.TraceLevel.Verbose;
            destBaseOptions.ComputerName = "destmachine";

            syncOptions.WhatIf = true;

            using (DeploymentObject deploymentObject =
                   DeploymentManager.CreateObject(DeploymentWellKnownProvider.MetaKey, "Default Web Site", sourceBaseOptions))
            {
                deploymentObject.SyncTo(DeploymentWellKnownProvider.MetaKey, "AnotherDefaultSite3",
                              destBaseOptions, syncOptions);
            }
        }

        static void Dest_Trace(object sender, DeploymentTraceEventArgs e)
        {
            string name = System.Reflection.MethodInfo.GetCurrentMethod().Name;
            Console.WriteLine(name + ": " + e.Message);
        }

        static void Source_Trace(object sender, DeploymentTraceEventArgs e)
        {
            string name = System.Reflection.MethodInfo.GetCurrentMethod().Name;
            Console.WriteLine(name + ": " + e.Message);
        }
 

III. Skip machine and root configuration settings during a sync from IIS6 to IIS6

 

msdeploy –verb:sync –source:webserver60,computername=source,username=user,password=password –dest:webserver60,computername=dest,username=user,password=password –skip:objectName=machineConfig,attributes.path=”/” –skip:objectName=rootWebConfig,attributes.path=”/” –verbose

 

        static void Main(string[] args)
        {
            DeploymentBaseOptions sourceBaseOptions = new DeploymentBaseOptions();
            DeploymentBaseOptions destinationBaseOptions = new DeploymentBaseOptions();
            DeploymentSyncOptions syncOptions = new DeploymentSyncOptions();

            sourceBaseOptions.ComputerName = "source";
            sourceBaseOptions.UserName = "user";
            sourceBaseOptions.Password = "password";

            destinationBaseOptions.ComputerName = "dest";
            destinationBaseOptions.UserName = "user";
            destinationBaseOptions.Password = "password";

            using (DeploymentObject deploymentObject = 
                   DeploymentManager.CreateObject(DeploymentWellKnownProvider.WebServer60, "", sourceBaseOptions))
            {
                DeploymentSkipDirective skipMachineConfig = 
                   new DeploymentSkipDirective("skipMachineConfig",@"objectName=machineConfig,attributes.path=""/""",true);
                DeploymentSkipDirective skipRootWebConfig = 
                   new DeploymentSkipDirective("skipRootWebConfig",@"objectName=rootWebConfig,attributes.path=""/""",true);

                sourceBaseOptions.SkipDirectives.Add(skipMachineConfig);
                sourceBaseOptions.SkipDirectives.Add(skipRootWebConfig);

                destinationBaseOptions.SkipDirectives.Add(skipMachineConfig);
                destinationBaseOptions.SkipDirectives.Add(skipRootWebConfig);

                sourceBaseOptions.TraceLevel = System.Diagnostics.TraceLevel.Verbose;
                destinationBaseOptions.TraceLevel = System.Diagnostics.TraceLevel.Verbose;

                sourceBaseOptions.Trace += new EventHandler<DeploymentTraceEventArgs>(sourceBaseOptions_Trace);
                destinationBaseOptions.Trace += new EventHandler<DeploymentTraceEventArgs>(destinationBaseOptions_Trace);

                deploymentObject.SyncTo
                    (
                        DeploymentWellKnownProvider.WebServer60,
                        "",
                        destinationBaseOptions,
                        syncOptions
                    );
            }
        }

        static void destinationBaseOptions_Trace(object sender, DeploymentTraceEventArgs e)
        {
            Console.WriteLine("dest: " + e.Message);
        }

        static void sourceBaseOptions_Trace(object sender, DeploymentTraceEventArgs e)
        {
            Console.WriteLine("source: " + e.Message);
        }

IV. Replace the name of DefaultAppPool with NewDefaultAppPool when performing a sync with AppPoolConfig provider from IIS7 to IIS7 and skip deleting the destination’s DefaultAppPool by applying a skip rule to ignore Delete operations

 

msdeploy –verb:sync –source:appPoolConfig,computername=computer,username=user,password=password –dest:appPoolConfig,computername=computer,username=user,password=password –replace:objectName=add,targetAttributeName=name,match=DefaultAppPool,replace=NewDefaultAppPool –skip:skipAction=Delete,objectname=add

 

        static void Main(string[] args)
        {
            DeploymentBaseOptions sourceBaseOptions = new DeploymentBaseOptions();
            DeploymentBaseOptions destinationBaseOptions = new DeploymentBaseOptions();
            DeploymentSyncOptions syncOptions = new DeploymentSyncOptions();

            sourceBaseOptions.ComputerName = "computer";
            sourceBaseOptions.UserName = "user";
            sourceBaseOptions.Password = "password";

            destinationBaseOptions.ComputerName = "computer";
            destinationBaseOptions.UserName = "user";
            destinationBaseOptions.Password = "password";

            using (DeploymentObject deploymentObject = 
                   DeploymentManager.CreateObject(DeploymentWellKnownProvider.AppPoolConfig, "", sourceBaseOptions))
            {
                DeploymentReplaceRule replaceAppPoolName = 
                   new DeploymentReplaceRule("replaceDefaultAppPool",
                                             "add",
                                             null,
                                             null,
                                             "name",
                                             "DefaultAppPool",
                                             "NewDefaultAppPool");
                DeploymentSkipRule skipDelete = new DeploymentSkipRule("skipDeleteOnDestination", "Delete", "add",null,null);
                syncOptions.Rules.Add(replaceAppPoolName);
                syncOptions.Rules.Add(skipDelete);
                deploymentObject.SyncTo(destinationBaseOptions,syncOptions);
            }
        }
 

V. Sync from a live server to Hostable Web Core

       static void Main(string[] args)
        {
            DeploymentBaseOptions sourceBaseOptions = new DeploymentBaseOptions();
            DeploymentBaseOptions destinationBaseOptions = new DeploymentBaseOptions();
            DeploymentSyncOptions syncOptions = new DeploymentSyncOptions();
            sourceBaseOptions.ComputerName = "computer";
            sourceBaseOptions.UserName = @"user";
            sourceBaseOptions.Password = "password";

            // core modules      
            destinationBaseOptions.WebServerConfiguration.WebServerDirectory = @"C:\windows\system32\inetsrv";      
            
            // new configuration for webcore
            destinationBaseOptions.WebServerConfiguration.ConfigurationDirectory = @"F:\myconfigpath"; 
            destinationBaseOptions.ComputerName = "computer";
            destinationBaseOptions.UserName = @"user";
            destinationBaseOptions.Password = "password";

            using (DeploymentObject deploymentObject =
                   DeploymentManager.CreateObject(
                       DeploymentWellKnownProvider.AppHostConfig,
                       "Default Web Site",
                       sourceBaseOptions))
            {
                deploymentObject.SyncTo(destinationBaseOptions,syncOptions);
            }
        } 
VI. Set ACLs on a directory for an application

 

msdeploy –verb:sync –source:setacl –dest:setacl=”C:\inetpub\wwwroot2”,setAclUser=”user”,setAclAccess=”Read”,setAclResourceType=”Directory”

 

        static void Main(string[] args)
        {

            DeploymentBaseOptions destination = new DeploymentBaseOptions();
            DeploymentSyncOptions syncOptions = new DeploymentSyncOptions();

            destination.AddDefaultProviderSetting("setAcl", "setAclUser", "user");
            destination.AddDefaultProviderSetting("setAcl", "setAclAccess", "Read");
            destination.AddDefaultProviderSetting("setAcl", "setAclresourceType", "Directory");

            using (DeploymentObject deploymentObject = DeploymentManager.CreateObject("setAcl",@"c:\inetpub\wwwroot2"))
            {
                deploymentObject.SyncTo(destination, syncOptions);
            }
        }
 

More to come in the future..

4 Comments

  • Is there a way i can uninstall an application using this API? I currently install apps on IIS7 in Windows Server 2008 remotely from a 2003 machine, but i can't uninstall them. I am bound on 2003 OS.
    Thank you,
    Cezar.

  • This is just the very meat of the approach but yes you can use this to delete applications as well. Just beware of deleting website root applications. If you delete the site root app, it really is gone making the site a bit unusable, but this works well for situations where your app is not at the root of the site.


    DeploymentBaseOptions baseOptions = new DeploymentBaseOptions();
    DeploymentSyncOptions options = new DeploymentSyncOptions();
    options.DeleteDestination = true;
    DeploymentObject obj = DeploymentManager.CreateObject("apphostconfig", "Default Web Site/application");
    obj.SyncTo("apphostconfig", "Default Web Site/application", baseOptions, options);

  • How would one skip multiple files/folders in an IIS6 sync via the C# API???

    CLI Example:
    msdeploy -verb:sync -source:contentPath="Default Web Site",computername=10.10.10.11 -dest:auto,computername=10.10.10.10 -retryAttempts:5 -retryInterval:120000 -skip:absolutepath=_*.xyz -skip:absolutepath="meetings" -skip:absolutepath="agendas" -whatif

  • Hi ,
    i want to skip web application how can i do it.
    i have command working
    msdeploy -verb:sync -source:package="c
    :\test.zip" -dest:auto -skip:WebApplication=test

    need equivalent C# code.

    your help is appreciated.

    thanks
    amar

Comments have been disabled for this content.