2

I'm using the Exec Powershell provider for Puppet to run some powershell scripts. I'm having an issue with the way it executes my script. The method of execution causes the script to run over, and over. I'm forced to kill the process myself, but the script itself seems to run normally.

I've been able to put together a simple example, and narrow down the cause.

Steps to Reproduce

  1. Create C:\backups\scripts\test.ps1 with the following:

I'm pretty sure the issue is related to the way I'm executing SQLCMD.exe, or to do with how long it takes to execute (about one second)

    $instance = '';
    $latest_output = "Hi";

    $cmd = "sqlcmd.exe -E -S .$instance -h -1 -m 1 -Q `"SET NOCOUNT ON; USE [master]; SELECT @@VERSION;`"";

    $output = [string](cmd.exe /C "$cmd");
    $output = $output.Trim(' ');

    if ($output -eq $latest_output)
    {
        Write-Host "Up to date.";
    }
    else
    {
        Write-Host "Updating...";
    }
  1. Open CMD.exe, run the following command, which works as intended:

    C:\Users\Administrator>powershell.exe -Command C:\backups\scripts\test.ps1
    Updating...
    
    C:\Users\Administrator>
    
  2. Now instead execute the script like this, which executes infinitely. This is the problematic syntax that the Exec provider in Puppet uses:

    C:\Users\Administrator>powershell.exe -Command - < C:\backups\scripts\test.ps1
    Updating...
    Updating...
    Updating...
    Updating...
    
  3. If I add exit; to the end of test.ps1, it only executes once.

My original question was going to be, how can I avoid this? But adding exit does that for me. The only remaining question is: Why does this happen?

EDIT: As soon as you bring a syntax error/exception into the script (eg. missing comma) the script will infinitely execute, throwing the exception each time. So while exit is a workaround, it's still dangerous as it doesn't work against exceptions.

Environment

  • Windows 2008 R2 Server Standard
  • Microsoft SQL Server Express 10.50.2500 with Advanced Services
  • Powershell 2.0

1 Answer 1

2

Powershell seems to be sensitive to what it receives as standard input (using < to redirect stdin). It may be receiving characters it's not expecting. I would recommend removing all newlines and saving your script in notepad. So your script would look something like this:

$instance = ''; $latest_output = "Hi"; $cmd = "etc...

With the few tests I did I received different behavior according to the encoding, whitespace and characters used. May be just the way cmd redirects input.

You may also wish to post your issue to the powershell puppet project in github.

Also, you might not want to use write-host since it is a stdout to the console and may be issues as well.

On a slightly unrelated note try this:

$psi = New-Object System.Diagnostics.ProcessStartInfo;
$psi.FileName = "powershell.exe"
$psi.UseShellExecute = $false
$psi.RedirectStandardInput = $true
$p = [System.Diagnostics.Process]::Start($psi)

Hit ctrl-c then you should see something like this:

PS C:\> PS C:\>

Powershell definitely exhibits wierd behavior when it comes to input redirection.

1
  • Thanks! I'll look into your point about unexpected characters, and see if I can come up with anything. Appreciate the pointers.
    – Geekman
    Jan 3, 2014 at 13:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .