5

I have a strange issue on a Windows Server 2012 R2 here that just does not make any sense to me at all.

I've got a PowerShell script that basically has the following structure:

Function Something-Different
{
    [...]
}

Function Start-Some-Batch
{
    Start-Process "K:\Path\To\Batch.cmd" 
}

Function Something-Else
{
    [...]
}

Something-Different
Start-Some-Batch
Something-Else

However the batch file K:\Path\To\Batch.cmd never ever gets executed!

Some more information and facts:

  • All actions are performed with the same domain user
  • PowerShell version is 4.0
  • The batch file can be executed without any issues when double-clicking it within Windows Explorer
  • The PowerShell script does not provide any error output
  • Executing Start-Process "K:\Path\To\Batch.cmd"directly from a PowerShell console does also not provide any output nor does it kick of the batch script
  • $error[0] is empty after the execution
  • The function Something-Else is executing just fine (which means Start-Some-Batch is not preventing the PS-script from finishing its execution)
  • Running the PowerShell script elevated does not change a thing
  • Calling the batch script from a cmd.exe prompt does also not start it (elevated or not)
  • I am suspecting any of the hundred Security Settings that come via group-policy

What could cause such a strange behaviour? I am running out of ideas here.

Matthias Güntert
  • 2,438
  • 12
  • 39
  • 59
  • 1
    "Calling the batch script from a cmd.exe prompt does also not start it" -- There's your problem. What's the .cmd file's real name? What are its contents? What are the relevant security settings applied? – jscott Dec 11 '14 at 15:50
  • Have you tried renaming it to .bat instead of .cmd? – Get-HomeByFiveOClock Dec 11 '14 at 15:52
  • Have you tried `Start-Process cmd "/c K:\Path\To\Batch.cmd"`? – Mathias R. Jessen Dec 12 '14 at 10:36
  • 1
    Thanks all for replying. I managed to solve this by supplying the `-WorkingDirectory` parameter. Sometimes I miss the forest for the trees... – Matthias Güntert Dec 12 '14 at 10:40
  • 3
    @Matze I curious how that resolves the issue if you're unable to run the batch file from a `cmd` prompt. Are you unable to run it in `cmd` because you're not in the same directory as the .cmd file? You should post your resolution as an answer below so you can [mark it as accepted](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work). [Answering your own questions is strongly encouraged](http://serverfault.com/help/self-answer). – jscott Dec 12 '14 at 11:12

2 Answers2

1

Here is what works for me, using Powershell v4.

PS C:\Scripts> Start-Process cmd -ArgumentList "/c 1.cmd" -WorkingDirectory c:\test4

Where 1.cmd lives in c:\test4

maxflipz
  • 21
  • 1
0

The best workaround I've found for this issue comes from an example by Chocolatey for their Start-ChocolateyProcessAsAdmin method.

$somePath = "C:\Path with Spaces" # Like $env:ProgramFiles
$batFile= "$someVar\myfile.bat"
Start-Process -ArgumentList "/c `'$batFile`'" -FilePath 'cmd.exe'

Note it can be single or double quotes inside the -ArgumentList, they just need escaped when inside other quotes.

This works because the first "pass" where PowerShell evaluates the Start-Process line will resolve the $batFile to it's correct value, it then passes the /c 'C:\Path with Spaces\myfile.bat' to the cmd.exe.

dragon788
  • 806
  • 8
  • 10