【发布时间】:2020-07-11 09:26:48
【问题描述】:
我正在尝试使用 export-csv -append 将命令输出捕获到 CSV 文件,并使用 out-file 将日期命令输出添加到同一文件。我想使用相同的 csv 文件并每小时运行一次命令/脚本并附加日期命令输出。我明白,因为 get-date 输出列数据与原始 get-service 列不同,所以它会抛出错误。有没有办法实现这个功能。这主要是为了在 CSV 文件中每小时报告一次服务
示例:file1.csv
07/07/2020
Status Name DisplayName
------ ---- -----------
Stopped AarSvc_3e53ef98 Agent Activation Runtime_3e53ef98
Running AdobeARMservice Adobe Acrobat Update Service
Running AdobeUpdateService AdobeUpdateService
Running AGMService Adobe Genuine Monitor Service
07/07/2020
Status Name DisplayName
------ ---- -----------
Stopped AarSvc_3e53ef98 Agent Activation Runtime_3e53ef98
Running AdobeARMservice Adobe Acrobat Update Service
Running AdobeUpdateService AdobeUpdateService
Running AGMService Adobe Genuine Monitor Service
错误:
PS C:\WINDOWS\system32> Get-Service | export-csv -Append -path 'D:\laptop backup\test.csv'
PS C:\WINDOWS\system32> get-date
Friday, July 10, 2020 5:10:37 PM
PS C:\WINDOWS\system32> get-date | out-file -Append -path 'D:\laptop backup\test.csv'
Out-File : A parameter cannot be found that matches parameter name 'path'.
At line:1 char:30
+ get-date | out-file -Append -path 'D:\laptop backup\test.csv'
+ ~~~~~
+ CategoryInfo : InvalidArgument: (:) [Out-File], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.OutFileCommand
【问题讨论】:
-
对于
Out-File,参数是-FilePath而不是-Path。 -
@AdminOfThings,
-Path是-FilePath在Out-File上下文中的别名(遗憾的是,这两种形式都将参数解释为通配符表达式;使用-LiteralPath确保逐字使用)。 -
@mklement0,无论出于何种原因,
-Path对我不起作用。但是,-LiteralPath确实如此。 -
感谢@AdminOfThings 的跟进:我忘记了
-FilePath的别名-Path仅在PS [Core] v6+ 中可用。
标签: powershell