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
- 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...";
}
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>
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...
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