2

arg1I have some code that creates an empty powershell array, then adds items to this array in a do..while loop. For some reason, no matter what I do, the first element in the array is "true". Why is this?

In my code below, you'll notice the loop adds output a line at a time from an external exe. The exe never returns "true", and you can see I'm doing Write-Host for each it that gets added to the array, and there's never a "true" output from this Write-Host call. There is no other code anywhere in my script that adds any elements to the array. This is frustrating because the index of the array is messed up. This essentially makes this array 1-indexed instead of 0-indexed. Does anyone have any ideas about what's happening here?

Also, one more thing to notice, after I initialize the array, i Write-Host the length, and it's 0, then after I'm dont adding all items, the length is what I would expect it to be if "true" were not the first element. So if there's 4 lines returned from the external app, then the second Write-Host $output.Length call will output 4.

Here is my code:

$output = @()
Write-Host "OUTPUT LENGTH START: $($output.Length)" -ForegroundColor Yellow
$continue = $true
do {
    $tempoutput = $Process.StandardOutput.ReadLine()
    if(($tempoutput -ne $null) -and ([string]::IsNullOrWhiteSpace($tempoutput) -ne $true)) {
        Write-Host $tempoutput -ForegroundColor DarkGray
        $output += $tempoutput
    } else {
        $continue = $false
    }
}
while($continue -eq $true)
Write-Host "OUTPUT LENGTH END: $($output.Length)" -ForegroundColor Yellow

Also, at the end of my function, i'm returning the output array like this:

$output

And then in my code that calls my function, if I do a foreach over the returned array's elements, "True" will always be the first item in this array.

EDIT TO INCLUDE FULL FUNCTION Here is my full function with all the Write-Host calls removed:

function Execute-HttpManager($arguments, $filepath){
    $ProcessInfo = New-Object System.Diagnostics.ProcessStartInfo 
    $ProcessInfo.FileName = $filepath 
    $ProcessInfo.RedirectStandardError = $true 
    $ProcessInfo.RedirectStandardOutput = $true 
    $ProcessInfo.RedirectStandardInput = $true
    $ProcessInfo.UseShellExecute = $false 
    $ProcessInfo.CreateNoWindow = $true
    $ProcessInfo.Arguments = $arguments
    $Process = New-Object System.Diagnostics.Process 
    $Process.StartInfo = $ProcessInfo

    $Process.Start() 
    $Process.WaitForExit() 

    $output = @()
    $continue = $true
    do {
        $tempoutput = $Process.StandardOutput.ReadLine()
        if(($tempoutput -ne $null) -and ([string]::IsNullOrWhiteSpace($tempoutput) -ne $true)) {
            $output += $tempoutput
        } else {
            $continue = $false
        }
    }
    while($continue -eq $true)

    $deployError = $Process.StandardError.ReadToEnd()

    $exitcode = $Process.ExitCode

    if($exitcode -eq 4) {
        $deployagainresponse = Read-Host
        if($deployagainresponse -eq 'y') {
            Deploy-Database $localapp $localsqlinstance $localdatabasename $localbackup $localsnapshot
        } elseif($deployagainresponse -ne 'bypass') {
            throw "http interaction failed with error.  Check the output."
        }
    } elseif ($exitcode -ne 0) {
        throw "Database deploy failed."
    }

    return $output
}

And here is the code that calls my function:

$tokenoutput = Execute-HttpManager @("arg1", "arg2", "arg3", "arg4") $pathtoexecutable  

It is this $tokenoutput variable that has the "True" added to the beginning of it's array.

7
  • Show the entire definition of your function and the code that calls it. On a hunch, try commenting out all calls to Write-Host and see if you still have this problem. Commented Oct 30, 2014 at 22:22
  • Without seeing the entire function we can't give you an accurate response. While you may be outputting $Output at the end, the function will pass anything that has an output along with it that is not specifically direced someplace (such as Write-Host directing things to the host). Commented Oct 30, 2014 at 22:40
  • okay, i removed all of the write-host calls, and that didn't change anything. I added the full function and the code that calls it to my question. Commented Oct 30, 2014 at 22:42
  • I'm betting $Process.Start() outputs True. try changing that line to [void]$Process.Start() and see if that doesn't fix your issue. Commented Oct 30, 2014 at 22:45
  • $Process.Start(), $Process.WaitForExit(), and the call to Deploy-Database may be outputting something. Either precede it with [void] or pipe it to out-null ( something | Out-Null ). Commented Oct 30, 2014 at 22:46

1 Answer 1

0

Make sure to check for any calls that are returning a value (such as $Process.Start()), and either precede them with [void] or pipe to out-null: $Process.Start() | Out-Null.

Be aware of this for any function! Anything the function outputs is part of the return value.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.