[英]How Can I Get the Output Stream in Powershell?
我想知道是否有可能獲得引用 Powershell 控制台窗口(在 Powershell 中)的流,假設存在一個。 例如,在 C# .NET 中,只需通過Console.OpenStandardOutput()
即可完成。 Powershell中是否有等價物?
我要做的是創建一個System.IO.BinaryWriter
來寫入它,而不是使用 Write-Host 等,主要用於實驗。
我試過[Console]::OpenStandardOutput()
,但這給了我一個空流,讓我認為 Powershell 正在使用不同的流。
我正在使用 Powershell V5.0:
Major Minor Build Revision
----- ----- ----- --------
5 0 10240 16384
(注意: PowerShell ISE 不使用控制台,因此寫入流也不會產生任何影響。)
嘗試從標准輸出流中讀取不會讓你在任何地方,它的意思是寫東西。
也不會檢查長度,因為標准輸出編寫器會立即獲取任何輸入並將其寫入屏幕緩沖區。
在基於控制台的主機(即powershell.exe
)中試試這個:
$TestBytes = "Test`n".ToCharArray() -as [byte[]]
$OutStream = [console]::OpenStandardOutput()
$OutStream.Write($TestBytes,0,$TestBytes.Length)
您應該看到字符串Test
被寫入屏幕(以及尾隨換行符)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.