首页 \ 问答 \ 如何在导出为CSV之前为每行添加日期/时间(How to Add Date/Time to Each Line before exporting to CSV)

如何在导出为CSV之前为每行添加日期/时间(How to Add Date/Time to Each Line before exporting to CSV)

我有一个很棒的工作PowerShell函数,叫做PSO。 PSO功能基于给定的输入/参数搜索一个或多个特定的窗口过程。 当一个进程名与searchparameter匹配时,它会将进程所有者和更多信息导出/附加到csv文件。 到目前为止都很好。

现在我真的想在PowerShell写入csv的每一行中添加日期和时间信息。 我一直在苦苦挣扎一段时间。 我在VBScript中有类似的工作,但我真的开始喜欢powershell,喜欢看到和了解更多。

在PSO函数下面,我假设这是应该生成和添加日期和时间的区域......对吗?

Function Get-PSO($searchString)
# http://powershell.com/cs/blogs/tips/archive/2009/12/17/get-process-owners.aspx
# Usage: Get-PSO * OR Get-PSO exp*,*paint*,calc*,notepad*
# Sample Export2csv: Get-PSO notepad* | Export-CSV $env:userprofile\desktop\export.csv -Append 

# A few date/time formats for later use... i hope
$date=(Get-Date -format "yyyy-MM-d")
$time=(Get-Date -format "HH:mm:ss")
$datetime=(Get-Date -format "yyyy-MM-d HH:mm")

$foundProcess = ps $searchString -ea silentlycontinue
if($foundProcess -eq $null) { return; }
$foundprocess | % {
gwmi Win32_Process -Filter ("Handle={0}" -f $_.id ) | 
 % { Add-Member -TypeName Type -InputObject $_ -MemberType NoteProperty -Name Owner -Value ($_.GetOwner().User) -PassThru } |
# See below my last attempt to get this tot work
# % { Add-Member -TypeName Type -InputObject $_ -MemberType NoteProperty -Name Owner -Value ($_.GetOwner().User) -MemberType NoteProperty -Name Date -Value $date -PassThru } |
  select Name, Handle, Owner, Type
}

当我运行该功能; Get-PSO记事本* | Export-CSV $ env:userprofile \ desktop \ export.csv -Append)我得到以下结果:

名称;处理;所有者;类型

。记事本++ exe文件; 7736;戴夫;

我喜欢添加日期/时间,结果应该类似于:

名称;处理;所有者,日期,时间,日期时间,类型

notepad ++。exe; 7736; Dave; 5-4-2015; 20:07; 5-4-2015 20:07;

任何人? 一些帮助或想法将非常感激。


I've got a nice working powershell function called PSO. The PSO function searches for one or more specific windows processes based on the given input/parameter. When a procesname matches the searchparameter it exports/appends the process owner and more info to a csv file. All good so far.

Now I really like to add date and time info to each line PowerShell writes to the csv. I've been struggling with it for a while now. I've got somehting simular working in VBScript, but i'm really starting to like powershell and like to see and learn more.

Below The PSO Function, I assume this is the area where Date and Time should be generated and added... right?

Function Get-PSO($searchString)
# http://powershell.com/cs/blogs/tips/archive/2009/12/17/get-process-owners.aspx
# Usage: Get-PSO * OR Get-PSO exp*,*paint*,calc*,notepad*
# Sample Export2csv: Get-PSO notepad* | Export-CSV $env:userprofile\desktop\export.csv -Append 

# A few date/time formats for later use... i hope
$date=(Get-Date -format "yyyy-MM-d")
$time=(Get-Date -format "HH:mm:ss")
$datetime=(Get-Date -format "yyyy-MM-d HH:mm")

$foundProcess = ps $searchString -ea silentlycontinue
if($foundProcess -eq $null) { return; }
$foundprocess | % {
gwmi Win32_Process -Filter ("Handle={0}" -f $_.id ) | 
 % { Add-Member -TypeName Type -InputObject $_ -MemberType NoteProperty -Name Owner -Value ($_.GetOwner().User) -PassThru } |
# See below my last attempt to get this tot work
# % { Add-Member -TypeName Type -InputObject $_ -MemberType NoteProperty -Name Owner -Value ($_.GetOwner().User) -MemberType NoteProperty -Name Date -Value $date -PassThru } |
  select Name, Handle, Owner, Type
}

When I run the function ; Get-PSO notepad* | Export-CSV $env:userprofile\desktop\export.csv -Append) I get the following result:

Name;Handle;Owner;Type

notepad++.exe;7736;Dave;

I like to Add Date/Time and the result should look something like:

Name;Handle;Owner;Date;Time;DateTime;Type

notepad++.exe;7736;Dave;5-4-2015;20:07;5-4-2015 20:07;

Anyone? some help or ideas would be very much appreciated.


原文:https://stackoverflow.com/questions/29460853
更新时间:2024-05-20 13:05

最满意答案

您可以使用:has for it,它检查匹配的元素是否包含某些元素等。

$('table:has(input, section)')

小提琴


You can use :has for that, it checks if the matching elements contains certain elements etc.

$('table:has(input, section)')

FIDDLE

相关问答

更多