0

First time I've used Invoke-Sqlcmd from Powershell and I've hit an annoying challenge. The script works fine but I need to run it from a shortcut with a "do something to continue" pause after it. Should be simple? Normally I do a Read-Host "Press enter to continue" but neither that, nor anything else from How do you do a 'Pause' with Powershell, works.

The output from the invoke-sqlcmd doesn't hit the screen until after the pause, so disappears instantly. Almost like it was acting asynchronously.

I've tried endless variations on:

Write-Host "Query Result:"
Invoke-Sqlcmd -ServerInstance $SQLServer -Database $db3 -Query $qcd -Verbose
Read-Host "Press enter etc..."

The only thing I've found that works is to pipe to a loop that iterates through the resulting Datarow object - that just seems overkill

PerryW
  • 1,426
  • 1
  • 15
  • 25
  • I really trying to follow you here, but, FYI, Read-ost and Pause is to pause the howel script, not just an interation. Have you already looked at a Do/Until or Do/While for your use case? – postanote May 31 '21 at 08:52
  • Have you tried putting the invoke into a variable and then calling it? – alexzelaya May 31 '21 at 13:23
  • @alexzelaya Yes, that works - I can loop through the variable. I guess this has become a bit of a matter of pride :) everything tells me that I should be able to do this - simply emulate the good old pause command that I've used for more than 30 years. – PerryW May 31 '21 at 23:02
  • You can always call cmd via command if you really miss it `cmd /c 'pause'` – alexzelaya Jun 01 '21 at 02:04
  • @alexzelaya Doesn't work - it fires before the output from the query is displayed - that's the issue will all the normal methods – PerryW Jun 01 '21 at 06:20
  • Try getting rid of the `$out =` and assign the variable via cmdlet parameter using `-Variable $out` and the run the cmd pause command. – alexzelaya Jun 01 '21 at 06:29

1 Answers1

0

Having spent more time on this than I'd care to admit to, the best I can come up with is:

Write-Host "Query Result:"
$out = Invoke-Sqlcmd -ServerInstance $SQLServer -Database $db3 -Query $qcd -Verbose
Write-Host ($out | Format-Table | Out-String)

Read-Host "Press enter etc..."

I feel that's more of a workaround than an answer though

PerryW
  • 1,426
  • 1
  • 15
  • 25