【问题标题】:Powershell redirect write-host output into new filePowershell 将写入主机输出重定向到新文件
【发布时间】:2021-06-10 17:09:06
【问题描述】:

在执行此命令创建空白文件的命令后,我正在尝试将 Powershell write-host 输出发送到新文件中

gci -r | % { Write-Host $_.Name,$_.FullName,$_.LastWriteTime } |ft  -wrap | out-file output.txt

我想要FileName 和File Full Path 和Last Write Time 在这样的输出文件

170801072740IMG-20170622-WA0012 (1).jpg O:\170801072740IMG-20170622-WA0012 (1).jpg 1/14/2021 4:3
170801072756IMG-20170622-WA0014.jpg O:\170801072756IMG-20170622-WA0014.jpg 1/14/2021 4:35:22 PM
170801072818IMG-20170624-WA0028.jpg O:\170801072818IMG-20170624-WA0028.jpg 1/14/2021 4:35:22 PM

排除属性名称:

Mode                LastWriteTime     Length Name
----                -------------     ------ ----

还检查了另一个命令 Write-Output

gci -r | % { Write-Output $_.Name,$_.FullName,$_.LastWriteTime } >> output2.txt

此命令正在重定向输出,但输出的格式不正确

输出文件:

Tuesday, March 9, 2021 1:14:42 AM
151104051548IMG_20151103_112015.jpg
O:\151104051548IMG_20151103_112015.jpg
Friday, March 12, 2021 8:40:15 PM
151104051558IMG_20151103_123234.jpg
O:\151104051558IMG_20151103_123234.jpg
Thursday, January 14, 2021 4:35:16 PM
151104051610IMG_20151103_123249.jpg
O:\151104051610IMG_20151103_123249.jpg
Thursday, January 14, 2021 4:35:16 PM

我也尝试过Start-Transcript 方法,但输出格式不正确

Start-Transcript 输出文件

有没有办法解决这个问题?

【问题讨论】:

  • 为什么不完全删除Write-Host,直接将值写入文件? gci -r | % { $_.Name,$_.FullName,$_.LastWriteTime -join ' ' } |Out-File .\path\to\file.txt
  • 非常感谢您的帮助
  • gci -r | ft 名称、全名、LastWriteTime >> result.txt
  • @FletcherF1 我只需要没有属性名称的值 Name 和 Full Name 和 Last Write Time

标签: powershell powershell-2.0 powershell-3.0 powershell-4.0


【解决方案1】:
gci -r -file | % { "$($_.Name) $($_.FullName) $($_.LastWriteTime)" >> G:\Test\Output.txt } 

我添加了 -file 参数,因为不需要目录,因为它们包含在完整路径中。

样本输出:

Get-FileMetaDataReturnObject.ps1 G:\BEKDocs\Scripts\Functions\Get-FileMetaDataReturnObject.ps1 09/20/2018 20:03:57
Get-InstalledSoftware.ps1 G:\BEKDocs\Scripts\Functions\Get-InstalledSoftware.ps1 05/02/2020 12:24:47
Invoke-ExerciseTimer.ps1 G:\BEKDocs\Scripts\Functions\Invoke-ExerciseTimer.ps1 01/19/2017 15:01:49
New-Shortcut.txt G:\BEKDocs\Scripts\Functions\New-Shortcut.txt 01/15/2017 11:50:33
Test-RoboForm-Function.ps1 G:\BEKDocs\Scripts\Functions\Test-RoboForm-Function.ps1 02/24/2021 20:32:25
Get-DisabledServicesV1-00.ps1 G:\BEKDocs\Scripts\Get-DisabledServices\Get-DisabledServicesV1-00.ps1 01/13/2018 16:39:09
Get-DisabledServicesV2-00.ps1 G:\BEKDocs\Scripts\Get-DisabledServices\Get-DisabledServicesV2-00.ps1 01/14/2018 12:31:56
Get-WinVer-V-2-0.ps1 G:\BEKDocs\Scripts\Get-WinVer\Get-WinVer-V-2-0.ps1 03/12/2019 21:14:37
Get-WinVer-V-3-0.ps1 G:\BEKDocs\Scripts\Get-WinVer\Get-WinVer-V-3-0.ps1 12/10/2019 20:03:38
Get-WinVer-V-3-1.ps1 G:\BEKDocs\Scripts\Get-WinVer\Get-WinVer-V-3-1.ps1 12/11/2019 09:17:17
Get-WinVer-V-3-2.ps1 G:\BEKDocs\Scripts\Get-WinVer\Get-WinVer-V-3-2.ps1 01/15/2020 20:53:11

【讨论】:

  • 这就是我想要的但不明白-file参数
  • 我建议将 `>> G:\Test\Output.txt }` 更改为 } | Set-Content G:\Test\Output.txt,除非您确实需要附加数据。否则,PowerShell 必须在 ForEach-Object 循环的每次迭代中打开文件以写入、写入数据并关闭文件(每个传递给它的文件一次)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-28
  • 1970-01-01
  • 1970-01-01
  • 2015-03-24
  • 1970-01-01
  • 2012-12-13
相关资源
最近更新 更多