2015-04-05 55 views
3

我有一个很好的工作PowerShell函数称为PSO。 PSO函数根据给定的输入/参数搜索一个或多个特定的窗口进程。当一个过程名称与搜索参数相匹配时,它将进程所有者和更多信息导出/附加到一个csv文件。迄今为止都很好。如何在导出为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记事本* |出口CSV $ ENV:USERPROFILE \桌面\ export.csv -append)我得到以下结果:

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

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

我想加入日期/时间,结果应该是这个样子:

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

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

有人吗?一些帮助或想法将非常感激。

回答

4

这是你在找什么?

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 
    } 
} 

每个Add-Member命令都会为单个结果添加一个属性。 Name参数将成为导出的CSV中的列名称,并且Value参数将作为值。 select命令(实际上是Select-Object)将结果过滤到列出的属性子集。

或者,您可以使用Select-Object和Calculated Properties来完成所有操作。

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={''}} 
    } 
} 
+1

Thnx David!选择对象的替代方案效果很好!而technet的文章是非常有用的! – Dave 2015-04-05 20:33:01