Jijo Nair
Jijo Nair

Reputation: 838

MSDEPLOY using CMD with C# script in Visual Studio

I am tryin got execute a msdeploy.exe command using cmd from visual studio with c# as scripting language

 string filename = @"C:\Deploy\Test\Test.zip";
        string servername = @"PADEVSPTAPP";
        string compname = @"IIS Web Application Name";
        string appvalue = @"Test";
        string strCmdText;
        strCmdText = "msdeploy.exe -verb:sync -source:package=" + filename + " -dest=auto,computerName=" + servername + " -setParam=name=" + compname + ",value=" + appvalue + " -allowUntrusted";
        //System.Diagnostics.Process.Start("CMD.exe", strCmdText);
        try
        {
            System.Diagnostics.ProcessStartInfo procStartInfo =
            new System.Diagnostics.ProcessStartInfo("cmd", "/c " + strCmdText);


            procStartInfo.RedirectStandardOutput = true;
            procStartInfo.UseShellExecute = false;

            procStartInfo.CreateNoWindow = true;

            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo = procStartInfo;
            proc.Start();

            string result = proc.StandardOutput.ReadToEnd();

            proc.WaitForExit();

            Console.WriteLine(result);



        }
        catch (Exception objException)
        {
            Console.WriteLine(objException.ToString());
        }

the string outcome is

msdeploy.exe -verb:sync -source:package="C:\\Deploy\\Test\\Test.zip"
-dest=auto,computerName="PADEVSPTAPP" -setParam=name="IIS Web Application Name",value="Test" -allowUntrusted

but this does not work due to \\ in the command.

How should i execute this command.

I even tried with powershell script ,which also did not work

string PS_script = @"$msdeploy = ""C:\\Program Files\\IIS\\Microsoft Web Deploy V3\\msdeploy.exe""
                $package = """;
PS_script = PS_script + Row.deployfile;
 PS_script = PS_script + @"""
                    $compname = ""PADEVSPTAPP""
                    $appname = ""IIS Web Application Name""
                    $appvalue = """;
PS_script = PS_script + changetype[0];

 PS_script = PS_script + @"""
            $md = $(""`""{0}`"" -verb:sync -source:package=`""{1}`"" -dest=auto,ComputerName=`""{2}`"" -setParam=name=`""{3}`"",value=`""{4}`"" -allowUntrusted"" -f $msdeploy, $package, $compname, $appname, $appvalue)
            cmd.exe /C ""`""$md`""""";

I have no clue where I am going wrong.

Upvotes: 0

Views: 356

Answers (1)

You are using an equals sign where it should be colon.

It's supposed to be -dest: and not -dest=

Same with setParam, it's supposed to be -setParam: not -setParam=

I suspect you don't actually have double backslashes \\ in your string it will just look like that if you inspect via the debugger - I suspect thats whats throwing you off.

Since you have spaces in your compname variable you need double quotes in your arguments string (probably around all your variables would be a good idea).

Also try running msdeploy.exe directly instead of via cmd.exe /c.

I assumed your msdeploy.exe is located in C:\Program Files (x86)\IIS\Microsoft Web Deploy V3

The string outcome is:

-verb:sync -source:package="C:\Deploy\Test\Test.zip" -dest:auto,computerName="PADEVSPTAPP" -setParam:name="IIS Web Application Name",value="Test" -allowUntrusted

Put it all together:

string filename = @"C:\Deploy\Test\Test.zip";
string servername = @"PADEVSPTAPP";
string compname = @"IIS Web Application Name";
string appvalue = @"Test";
string strCmdText;
strCmdText = "-verb:sync -source:package=\"" + filename + "\" -dest:auto,computerName=\"" + servername + "\" -setParam:name=\"" + compname + "\",value=\"" + appvalue + "\" -allowUntrusted";
//System.Diagnostics.Process.Start("CMD.exe", strCmdText);
try
{
    System.Diagnostics.ProcessStartInfo procStartInfo =
    new System.Diagnostics.ProcessStartInfo(@"C:\Program Files (x86)\IIS\Microsoft Web Deploy V3\msdeploy.exe");
    procStartInfo.Arguments = strCmdText;


    procStartInfo.RedirectStandardOutput = true;
    procStartInfo.UseShellExecute = false;

    procStartInfo.CreateNoWindow = true;

    System.Diagnostics.Process proc = new System.Diagnostics.Process();
    proc.StartInfo = procStartInfo;
    proc.Start();

    string result = proc.StandardOutput.ReadToEnd();

    proc.WaitForExit();

    Console.WriteLine(result);



}
catch (Exception objException)
{
    Console.WriteLine(objException.ToString());
}

BONUS INFO

If you need a more bulletproof way of determining where msdeploy.exe is located maybe have a look at these links:

https://gist.github.com/SergeyAxenov/15cf008531e6d0741533

How to find out what version of webdeploy/msdeploy is currently installed?

Upvotes: 2

Related Questions