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.

4
  • Can you share your output? Commented Dec 28, 2012 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" Commented Dec 28, 2012 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. Commented Dec 28, 2012 at 11:53
  • So, I can see that ... is appended in the output only at the beginning... is that so? or it reappears? Commented Dec 28, 2012 at 12:05

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

The safest way to redirect output and error streams is to use script from How to capture process output asynchronously in powershell?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.