【问题标题】:unable to export from powershell to CSV [duplicate]无法从 powershell 导出到 CSV [重复]
【发布时间】:2019-11-29 12:02:54
【问题描述】:

请在这里帮助一个可怜的新手...下面是我的脚本,脚本底部有错误。主要问题是我无法导出到 CSV

PS C:\Users\LUPUWANA> $logs = get-eventlog system -ComputerName cafeserver -source Microsoft-Windows-Winlogon -After (Get-Date).AddDays(-7);
$res = @(); ForEach ($log in $logs) {if($log.instanceid -eq 7001) {$type = "Logon"} Elseif ($log.instanceid -eq 7002){$type="Logoff"} Else {Continue} $res += New-Object PSObject -Property @{Time = $log.TimeWritten; "Event" = $type; User = (New-Object System.Security.Principal.SecurityIdentifier $Log.ReplacementStrings[1]).Translate([System.Security.Principal.NTAccount])}};
Export-Csv -Path C:\users\lupuwana\desktop\events.csv

oss 获取帮助-详细

Supply values for the following parameters:
InputObject: 
oss : Cannot process argument transformation on parameter 'Width'. Cannot convert value "get-help" to type "System.Int32". Error: "Input string was not in a 
correct format."
At line:4 char:5
+ oss get-help -detailed
    + CategoryInfo          : InvalidData: (:) [oss], ParameterBindingArgumentTransformationException
    + FullyQualifiedErrorId : ParameterArgumentTransformationError,oss

【问题讨论】:

  • 至于oss get-help -detailed:不清楚你想做什么;如果您需要oss 的帮助,请使用get-help oss -detailed。如果您想将输出从get-help -detailed 发送到oss,请使用get-help -detailed | oss,尽管在这种情况下使用ossOut-String -Stream 的函数包装器)将不起作用。

标签: powershell


【解决方案1】:

您的循环构建了一个名为 $res 的对象,因此假设这是您希望导出为 CSV 的对象,您需要将该对象提供给 Export-Csv Cmdlet:

Export-Csv -InputObject $res -Path C:\users\lupuwana\desktop\events.csv

您可能还希望添加-NoTypeInformation 选项,否则您的文件将包含描述对象类型的额外标题行。

【讨论】:

  • 感谢您的反馈,非常感谢,但我现在担心的是;在 powershell 上我得到了正确的结果,见下文...但是当转换为 csv 时,模板为空白
  • Count,"Length","LongLength","Rank","SyncRoot","IsReadOnly","IsFixedSize","IsSynchronized" 13,"13","13","1" ,"System.Object[]","False","True","False"
猜你喜欢
  • 2016-04-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-13
  • 2021-05-01
  • 2017-01-02
  • 2020-01-15
  • 2021-01-04
相关资源
最近更新 更多