powershell - 接收作业输出到文件

标签 powershell powershell-3.0

如何将接收作业结果输出到文件?

我试过了,但没用:

$log = "C:\springfield\citrix\CitrixAutomation.log"
Get-Job | Receive-Job | Out-File $log

我还尝试保存 Get-Job | 的输出Receive-Job 到一个变量,但它不起作用..

$log = "C:\springfield\citrix\CitrixAutomation.log"
$getjobarr = @()
Function LogWrite
{
   Param ([array]$logstring)
   $logstring | Out-File $log -Append
}

$getjobarr += Get-Job | Receive-Job

LogWrite $getjobarr

我认为 Get-JobReceive-Job 只能输出到控制台,那么我该如何实现呢?

谢谢你的帮助

最佳答案

作业“输出”可以重定向到一个文件,例如:

PS> Start-Job {Get-ChildItem C:\users\keith}

Id     Name            PSJobTypeName   State         HasMoreData     Location             Command
--     ----            -------------   -----         -----------     --------             -------
2      Job2            BackgroundJob   Running       True            localhost            Get-ChildItem C:\users...


PS> Receive-Job -id 2 | Out-File job.log
PS> gc .\job.log


    Directory: C:\users\keith


Mode           LastWriteTime       Length Name
----           -------------       ------ ----
d----     1/21/2014  8:24 PM        <DIR> .ssh
d----      9/9/2014 10:00 PM        <DIR> Bin
d-r--     9/11/2014  9:20 PM        <DIR> Contacts
d-r--     9/11/2014  9:20 PM        <DIR> Desktop

如果您在作业中运行的任何内容都写入主机,那么是的,您就完蛋了。如果您控制它,请使用写输出而不是写主机。此外,请确保等到每个作业完成后再接收其输出,除非您可以坐在循环中等待作业状态更改为“已完成”或“失败”。您可以使用 Wait-Job 等待作业完成,然后再请求其输出。

关于powershell - 接收作业输出到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25811604/

相关文章:

powershell - 如何在脚本中使用 PowerShell 模块而不将模块加载到用户 session 中?

powershell - 在文本文件的两列之一中查找重复值

powershell - 删除项目后强制桌面上的图标刷新,或首先停止添加项目

Powershell Start-Job 无法运行

powershell - 如何让 PowerShell 不将 # 视为注释的开头?

windows - 在 PowerShell 中使用 Git for Windows 时使用插入符号 (^)

powershell - Powershell Function为数组中的每个项目运行(我不希望这种情况发生)

function - Powershell:为什么这个功能不起作用?

powershell - 将Windows服务登录名设置为GMSA帐户

powershell - PowerShell测试连接,如果使用get-service存在服务