脚本不会将信息放在Out-File中

时间:2017-04-25 09:22:14

标签: windows powershell

我创建了以下脚本:

#This scripts finds if 2 LST files were created today.

#What date are we in
$today = Get-Date

#Are there two lst files?
$Lst = @(Get-ChildItem 'PATH' -recurse -include @("*.lst") | Where-Object { $_.CreationTime -gt (Get-Date).Date })

if ($Lst.length -eq 2) {

$LstStatus =  "Files OK"
$filename = (Get-ChildItem 'PATH' -recurse -include @("*.lst") | Where-Object { $_.CreationTime -gt (Get-Date).Date } | Select-Object Name)
}
else { $LstStatus =  "LST ERROR" }

#What do I want in the file
$output = @(Write-Host -NoNewline @($today.ToShortDateString()) @($filename.name) $LstStatus)
$output | Out-File PATH\lst_check.txt`

$output包含我需要的信息并创建了lst_check.txt,但它是空的。

我错过了什么?是否有其他命令我应该将Write-Host替换为?

1 个答案:

答案 0 :(得分:1)

Write-Host将您的输出直接写入主机应用程序,在您的情况下可能是控制台。

要输出到文件,请将值存储为字符串:

$output = "$($today.ToShortDateString()) $($filename.name) $LstStatus"
Write-Host "This is the output: $output"
$output | Out-File PATH\lst_check.txt