简体   繁体   English

写主机 => 导出到文件

[英]Write-Host => Export to a file

I have got a script with some commands such as Write-Host "Server1" .我有一个带有一些命令的脚本,例如Write-Host "Server1" How can I export it to a file?如何将其导出到文件?

When I tried with script > export.txt it didn't work.当我尝试使用script > export.txt它不起作用。

Write-Host redirects the output only to the console. Write-Host仅将输出重定向到控制台。

You can use Write-Output and redirect to a file ( > export.txt or pipe to Out-File export.txt )您可以使用Write-Output并重定向到文件( > export.txt或管道到Out-File export.txt

In the extreme case when you absolutely need to redirect all output from a script, take a look to this cmdlet:在绝对需要重定向脚本的所有输出的极端情况下,请查看此 cmdlet:

Start-Transcript
Get-Help Start-Transcript -full

In PowerShell script > export.txt is syntactic sugar for script | Out-File -path export.txt在 PowerShell script > export.txtscript | Out-File -path export.txt语法糖script | Out-File -path export.txt script | Out-File -path export.txt . script | Out-File -path export.txt

Write-Host sends the objects to the host, and it does not return any objects. Write-Host 将对象发送到主机,并且不返回任何对象。 This means no objects are piped to the Out-File cmdlet and nothing is written to the export.txt file.这意味着没有对象通过管道传输到 Out-File cmdlet,并且没有任何内容写入 export.txt 文件。 A workaround (in case you don't want to change your script) is to open a cmd console and redirect the PowerShell output using cmd console redirection.一种解决方法(以防您不想更改脚本)是打开cmd控制台并使用 cmd 控制台重定向来重定向 PowerShell 输出。

C:\> powershell .\script.ps1 > .\export.txt

The usefulness of different approaches is largely going to be based on your use case, of course, but...当然,不同方法的用处很大程度上取决于您的用例,但是……

The "right" way to do this, I believe, if you have control of the scripts (and this is what I usually do, though admittedly I was looking for a shortcut today), is to "overload" Write-Host , so to speak, and then send everything you would've sent to Write-Host to this new function.我相信,执行此操作的“正确”方法是,如果您可以控制脚本(这就是我通常所做的,尽管我今天正在寻找捷径)是“重载” Write-Host ,以便说话,然后将您将发送到Write-Host所有内容发送到这个新函数。

Just to stay with valid PowerShell verbs, I call mine Write-Feedback .为了保持有效的 PowerShell 动词,我将我的称为Write-Feedback

function Write-Feedback()
{
    param
    (
        [Parameter(Position=0,ValueFromPipeline=$true)]
        [string]$msg,
        [string]$BackgroundColor = "Yellow",
        [string]$ForegroundColor = "Black"
    )

    process {
        $msg | ForEach-Object {
            Write-Host `
                -BackgroundColor $BackgroundColor `
                -ForegroundColor $ForegroundColor `
                $_;
        }
    }
}

So now you have a function that operates essentially identically to Write-Host , but you can easily control where the output goes.因此,现在您拥有一个与Write-Host基本相同的功能,但您可以轻松控制输出的位置。 If you need to write to a file, you can edit Write-Feedback so that all of its calls now do whatever you need.如果您需要写入文件,您可以编辑Write-Feedback以便它的所有调用现在可以执行您需要的任何操作。

You could simply change the line to Write-Output , depending on if you're doing anything else down the pipeline...您可以简单地将该行更改为Write-Output ,具体取决于您是否在管道中执行任何其他操作...

...
Write-Output $_;

You could send the output to the same file that you're piping the rest of the command to, but within the Write-Feedback function, even keeping the Write-Host too:您可以将输出发送到将命令的其余部分通过管道传送到的同一文件,但在Write-Feedback函数中,甚至还保留Write-Host

function Write-Feedback()
{
    param
    (
        [Parameter(Position=0,ValueFromPipeline=$true)]
        [string]$msg,
        [string]$BackgroundColor = "Yellow",
        [string]$ForegroundColor = "Black"
    )

    process {
        $msg | ForEach-Object {
            Write-Host `
                -BackgroundColor $BackgroundColor `
                -ForegroundColor $ForegroundColor `
                $_;

            $_ | Out-File "./export.txt" -Append; # <<< or add a param to location
        }
    }
}

Or if you have a few outliers where you don't want the Write-Feedback content to be piped to a file, you could add a new optional parameter that asks what to do with each specific Write-Feedback call that you switch off of -- and send to file, to Write-Host , to Write-Output , etc -- changing the default to what you usually want, and explicitly switch ing off of the new parameter where it's explicitly set.或者,如果你有一些异常值,你不想Write-Feedback的内容通过管道输送到一个文件中,可以添加一个询问如何每个具体做了新的可选参数, Write-Feedback来电时switch的关闭- -发送到文件,以Write-Host ,来Write-Output ,等等-更改默认你平时想要什么,并明确switch荷兰国际集团在那里的明确设置新的参数了。

Etc., etc. It's just soooo much easier to route all of your calls into a centralized clearinghouse for output in PowerShell.等等等等。将所有呼叫路由到集中式票据交换所以在 PowerShell 中输出要容易得多。 Then when you do change your mind, it's not a huge search and replace -- and replace back -- task.然后,当改变你的想法,这是不是一个巨大的搜索和替换-和替换回来-任务。

I think the only pain here would be if you didn't want to send things down the pipeline, so Write-Output is out, but did want to ensure the file Write-Feedback wrote to was the same as what you've specified in the > export.txt from your example without editing Write-Feedback each time.我认为这里唯一的痛苦是如果您不想将内容发送到管道中,因此Write-Output已退出,但确实想确保Write-Feedback的文件Write-Feedback与您在中指定的文件相同您的示例中的> export.txt无需每次都编辑Write-Feedback I'm not sure if there's an easy way to do that.我不确定是否有一种简单的方法可以做到这一点。

But since you'd then already be one step removed from your "pipe step", that's probably not a legitimate use case.但是,既然您已经从“管道步骤”中移除了一步,那么这可能不是一个合法的用例。

Anyhow, the bottom line is to overload Write-Host and do whatever you want by editing in one place.无论如何,底线是重载Write-Host并通过在一个地方进行编辑来做任何您想做的事情。

Using *> instead of > works for me.使用 *> 而不是 > 对我有用。 It redirects all output to a file;它将所有输出重定向到一个文件; nothing is displayed on the screen.屏幕上不显示任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM