17

Im am trying to get MSDeploy to execute a PowerShell script on a remote server. This is how i execute MSDeploy:

msdeploy \
  -verb:sync \ 
  -source:runCommand='C:\temp\HelloWorld.bat', \
  waitInterval=15000,waitAttempts=1 \
  -dest:auto,computername=$WebDeployService$Credentials -verbose

HelloWorld.bat contains:

echo "Hello world!"
powershell.exe C:\temp\WebDeploy\Package\HelloWorld.ps1
echo "Done"

The HelloWorld.ps1 only contains:

Write-Host "Hello world from PowerShell!"

However, it seems like PowerShell never terminates. This is the output from running the msdeploy:

Verbose: Performing synchronization pass #1.
Verbose: Source runCommand (C:\temp\HelloWorld.bat) does not match destination (C:\temp\HelloWorld.bat) differing in attributes (isSource['True','False']). Update pending.
Info: Updating runCommand (C:\temp\HelloWorld.bat).
Info:

Info: C:\temp>echo "Hello world!"
"Hello world!"

C:\temp\WebDeploy>powershell.exe C:\temp\HelloWorld.ps1

Info: Hello world from Powershell!
Info:

Warning: The process 'C:\Windows\system32\cmd.exe' (command line '/c "C:\Users\peter\AppData\Local\Temp\gaskgh55.b2q.bat
"') is still running. Waiting for 15000 ms (attempt 1 of 1).
Error: The process 'C:\Windows\system32\cmd.exe' (command line '/c "C:\Users\peter\AppData\Local\Temp\gaskgh55.b2q.bat"'
) was terminated because it exceeded the wait time.
Error count: 1.

Anyone knows a solution?

Peter Moberg
  • 3,098
  • 4
  • 29
  • 35

2 Answers2

21

Your scenario and problem look similar to this reported issue: PowerShell.exe can hang if STDIN is redirected

If this is the case then try this workaround: use -inputformat none:

powershell.exe -inputformat none C:\temp\WebDeploy\Package\HelloWorld.ps1

I have tried this with "a fake msdeploy" program that calls the .bat file like this:

using System.Diagnostics;
class Program
{
    static void Main(string[] args)
    {
        ProcessStartInfo si = new ProcessStartInfo();
        si.FileName = "cmd.exe";
        si.Arguments = "/c " + args[0];
        si.RedirectStandardInput = true;
        si.UseShellExecute = false;
        var process = Process.Start(si);
        process.WaitForExit();
    }
}

This demo does have the same problem that you describe and the workaround helps. If msdeploy calls the .bat file in the same or similar way then hopefully this is a solution.

Roman Kuzmin
  • 40,627
  • 11
  • 95
  • 117
  • 1
    According to the Powershell 2.0 and 3.0 [documentation](http://technet.microsoft.com/en-us/library/hh847736.aspx) `None` is not a valid `-InputFormat` argument, so this workaround may be relying on undefined behavior. – sourcenouveau Nov 14 '14 at 14:49
2
powershell.exe -file ScriptFile.ps < CON

This solve the problem without resorting to undocumented features.

Baz
  • 36,440
  • 11
  • 68
  • 94
Greg B
  • 21
  • 1