【问题标题】:Redirect Powershell output into .ps1将 Powershell 输出重定向到 .ps1
【发布时间】:2012-12-13 15:09:41
【问题描述】:

我使用 powershell 调用一个 sql 存储过程,现在我想将完整的输出集重定向到一个 .ps1 文件中,因为输出行在 powershell 中是可执行的。

我正在尝试使用 >output.ps1,它可以工作,但我正在检查输出文件,它包含很多“...”来替换实际输出。

如何导出完整的输出?还要去掉标题?

谢谢。

【问题讨论】:

  • 你能分享你的输出吗?
  • 这只是我输出的一个简短示例...$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 = "标题" $shape500.LineStyle = " Title" $shape500.Text = "Assignation de Barrières - 2012 年 12 月 17 日,星期一"
  • 它实际上是使用powershell打开visio并开始在预定义的模板上绘制形状。所以我想要的实际上是将这些输出存储到 .ps1 中,这样我就可以从 powershell 调用 ps1 文件来执行这些输出。
  • 所以,我可以看到 ... 仅在开头附加在输出中......是这样吗?还是再次出现?

标签: powershell-2.0


【解决方案1】:

这取决于您如何调用存储过程。如果您在 PowerShell 中调用它,您应该能够收集输出,所以我假设您将它作为一个单独的任务启动它自己的窗口。如果没有您的实际示例,这是一种从 tasklist.exe 命令收集输出的方法。您可能会发现它适用。

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

【讨论】:

猜你喜欢
  • 2015-06-22
  • 1970-01-01
  • 2012-05-26
  • 2015-03-24
  • 1970-01-01
  • 1970-01-01
  • 2012-07-04
  • 2021-06-10
  • 2020-08-14
相关资源
最近更新 更多