0

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.

1 Answer 1

2

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?

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you Jonas...your answer worked like a charm. I also replaced the string with value coming form DB. All i had to do is change @"C:\Deploy\Test\Test.zip" to ""+db.valueabc+""

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.