run a command in Do While Loop

I am trying create a Do While loop that will run a cmd /c command and write the content to a out-file text file, from there the i will loop through my do while loop until the string i want appears. However when i try to issue the cmd /c command the out-file remains open and the scripts hangs unable to progress forward. If i comment out the cmd /c the loop moves forward.

$i = 1
$maxout = 4

do {
    if ($i -eq 1){
    cmd /c call "c:\wildfly\jboss.cli" --command="deployment-info" | Out-File output.txt
    $outvalue= Get-Content output.txt | Select-String -Pattern (.?[a-zA-Z].ear)
    }
    else
    {
    Write-Host "server not found"
    }
    $i++
}
while ($i -le $maxout)

I am not sure what i am doing wrong here.

You said you’re running the command and from there looping. That’s not the case. You’re running the command inside your Do loop, and each time, it will overwrite the previous output.txt. Each time, it’s reading in the output.txt and running Select-String. That doesn’t appear to match what you said.

In terms of it hanging, without knowing what JBoss is doing exactly on your system, I honestly have no idea. I would probably insert a breakpoint at line 6 and then manually run the JBoss file in break mode.

That is what i intended to do with each loop overwrite the output.txt each time. As for jboss it self it runs a query to show me what files are active this works (i am waiting for a file to appear), but the loop does not appear to progress past this point it stops and holds the output.txt file in a open state.

try this & see if it works?

$i = 1
$maxout = 4

do {
    if ($i -eq 1){
    cmd /c call "c:\wildfly\jboss.cli" --command="deployment-info" | Out-File output.txt -Force
    $outvalue= (Get-Content output.txt) | Select-String -Pattern (.?[a-zA-Z].ear)
    }
    else
    {
    Write-Host "server not found"
    }
    $i++
}
while ($i -le $maxout)

I solved my original issue, it appears that running this particular .bat file required a press any key to continue hence holding the output.txt open.

To solve this i piped the running of the command to an echo and solved that issue particular issue.

I know have an issue where my $NewEarFile variable will not pass to the if statement.

do {

Start-Process cmd "/c","echo; | $WildflyPath\bin\jboss-cli.bat","--connect","""deployment-info --server-group=server-group" -RedirectStandardOutput NEW_OUTPUT.txt

$NewEarLoad= Get-Content NEW_OUTPUT.txt | Select-String -Pattern '.+?[a-zA-Z]{3,}.ear' -AllMatches | %{$_.matches[0]} | %{$_.Value}

Write-Host "Max Time out Counter [30sec] Intervals $i of 10"

Start-Sleep -Seconds 10

if($NewEarFile -eq $NewEarLoad) {

#$i -eq 1

$NewEarLoad= Get-Content NEW_OUTPUT.txt | Select-String -Pattern '.+?[a-zA-Z]{3,}.ear' -AllMatches | %{$_.matches[0]} | %{$_.Value}

Write-Host "Expect EAR:$NewEarFile Loaded EAR:$NewEarLoad"

}

else

{

Write-Host "Error Failed to connect to server"

#$NewEarLoad= Get-Content NEW_OUTPUT.txt | Select-String -Pattern '.+?[a-zA-Z]{3,}.ear' -AllMatches | %{$_.matches[0]} | %{$_.Value}

#Write-Host "Expect EAR:$NewEarFile Loaded EAR:$NewEarLoad"  

}

$i++

}

while ($i -le $maxtimeout)