无法从Powershell导出到CSV

时间:2019-11-28 19:29:13

标签: powershell

请在这里帮助可怜的纽比...下面是我的脚本,脚本底部有错误。主要问题是我无法导出到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 get-help -detailed

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

1 个答案:

答案 0 :(得分:1)

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

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

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