Powershell 如何在导出到CSV之前向每行添加日期/时间

Powershell 如何在导出到CSV之前向每行添加日期/时间,powershell,powershell-3.0,Powershell,Powershell 3.0,我有一个很好的powershell函数,名为PSO。 PSO函数根据给定的输入/参数搜索一个或多个特定的windows进程。当procesname与searchparameter匹配时,它会将进程所有者和更多信息导出/附加到csv文件中。到目前为止一切都很好 现在,我非常喜欢将日期和时间信息添加到PowerShell写入csv的每一行。我已经挣扎了一段时间了。我在VBScript中使用了一些simular,但我真的开始喜欢powershell,并希望看到和了解更多 在PSO函数下面,我假设这是应

我有一个很好的powershell函数,名为PSO。 PSO函数根据给定的输入/参数搜索一个或多个特定的windows进程。当procesname与searchparameter匹配时,它会将进程所有者和更多信息导出/附加到csv文件中。到目前为止一切都很好

现在,我非常喜欢将日期和时间信息添加到PowerShell写入csv的每一行。我已经挣扎了一段时间了。我在VBScript中使用了一些simular,但我真的开始喜欢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
}
当我运行函数时;获取PSO记事本*|导出CSV$env:userprofile\desktop\Export.CSV-Append)我得到以下结果:

姓名;手柄所有者;类型

记事本+++.exe;7736;戴夫

我喜欢添加日期/时间,结果应该如下所示:

姓名;手柄所有者;日期;时间日期时间;类型

记事本+++.exe;7736;戴夫;5-4-2015;20:07;5-4-2015 20:07;


有人吗?非常感谢您的帮助或想法。

这就是您想要的吗

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 -InputObject $_ -MemberType NoteProperty -Name Owner -Value ($_.GetOwner().User) -PassThru
                Add-Member -InputObject $_ -MemberType NoteProperty -Name Date -Value $date -PassThru
                Add-Member -InputObject $_ -MemberType NoteProperty -Name Time -Value $time -PassThru
                Add-Member -InputObject $_ -MemberType NoteProperty -Name DateTime -Value $datetime -PassThru
                Add-Member -InputObject $_ -MemberType NoteProperty -Name Type -Value "" -PassThru
            } |
            select Name, Handle, Owner, Date, Time, DateTime, Type
    }
}
每个
addmember
命令都会向单个结果添加一个属性。
Name
参数将是导出CSV中的列名,
Value
参数将是值。
select
命令(实际上是
select Object
)将结果过滤到列出的属性子集

或者,您可以只使用选择对象和


大卫!“选择对象”选项非常有效!technet的文章对你来说非常有用!!
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 ) | 
            select-Object Name, Handle, 
                @{Name='Owner'; Expression={$_.GetOwner().User}}, 
                @{Name='Date'; Expression={$date}}, 
                @{Name='Time'; Expression={$time}}, 
                @{Name='DateTime'; Expression={$datetime}}, 
                @{Name='Type'; Expression={''}}
    }
}