0

I use powershell to invoke a sql stored procedure, now I want to redirect the complete set of the output into a .ps1 file, because the the output line is executable in powershell.

I'm trying to use >output.ps1, it works, but I'm checking the output file, it contains a lot of '...' to replace real output.

How to export the complete output? also stripe the header off?

Thanks.

Apriljuly
  • 51
  • 2
  • 5
  • 13
  • Can you share your output? – Arpit Dec 28 '12 at 11:34
  • Here's just a short sample of my output...$application = New-Object -ComObject Visio.Application $documents = $application.Documents $document = $documents.Add("AMSGantt.vst") $pages = $application.ActiveDocument.Pages $page = $pages.Item(1) $shape500 = $page.DrawLine(2,7.9,11,7.9) $shape500.TextStyle = "Title" $shape500.LineStyle = "Title" $shape500.Text = "Assignation de Barrières - Monday, December 17, 2012" – Apriljuly Dec 28 '12 at 11:52
  • It actually use powershell to open visio and start drawing shape on the predefined template. So what I want is actually store these output into a .ps1, so that I can invoke the ps1 file from powershell to execute these output. – Apriljuly Dec 28 '12 at 11:53
  • So, I can see that `...` is appended in the output only at the beginning... is that so? or it reappears? – Arpit Dec 28 '12 at 12:05

1 Answers1

0

It depends how you invoke the stored procedure. If you're invoking it within PowerShell, you should be able to collect the output, so I assume you're starting it as a separate task its own window. Without your actual example, here's a way to collect the output from the tasklist.exe command. You may find it applicable.

cls
$exe = 'c:\Windows\System32\tasklist.exe'
$processArgs = '/NH'
try {
    Write-Host ("Launching '$exe $processArgs'")
    $info = New-Object System.Diagnostics.ProcessStartInfo
    $info.UseShellExecute = $false 
    $info.RedirectStandardError = $true 
    $info.RedirectStandardOutput = $true 
    $info.RedirectStandardInput = $true 
    $info.WindowStyle = [System.Diagnostics.ProcessWindowStyle]::Hidden
    $info.CreateNoWindow = $true
    $info.ErrorDialog = $false
    $info.WorkingDirectory = $workingDir
    $info.Filename = $exe
    $info.Arguments = $processArgs

    $process = [System.Diagnostics.Process]::Start($info)
    Write-Host ("Launched $($process.Id) at $(Get-Date)")
    <# 
    $process.StandardOutput.ReadToEnd() is a synchronous read. You cannot sync read both output and error streams. 
    $process.BeginOutputReadLine() is an async read. You can do as many of these as you'd like. 
    Either way, you must finish reading before calling $process.WaitForExit()
    http://msdn.microsoft.com/en-us/library/system.diagnostics.processstartinfo.redirectstandardoutput.aspx
    #>

    $output = $process.StandardOutput.ReadToEnd()

    $process.WaitForExit() | Out-Null 

    Write-Host ("Exited at $(Get-Date)`n$output")

} catch {
    Write-Host ("Failed to launch '$exe $processArgs'")
    Write-Host ("Failure due to $_")
}
$output
codepoke
  • 1,272
  • 1
  • 22
  • 40
  • The safest way to redirect output and error streams is to use script from [How to capture process output asynchronously in powershell?](http://stackoverflow.com/q/24370814) – Michael Freidgeim Apr 08 '16 at 08:26