powershell - 如何在导出为 CSV 之前向每一行添加日期/时间

标签 powershell powershell-3.0

我有一个很好用的 powershell 函数,叫做 PSO。 PSO 函数根据给定的输入/参数搜索一个或多个特定的窗口进程。当流程名称与搜索参数匹配时,它会将流程所有者和更多信息导出/附加到 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) 我得到以下结果:

名称;句柄;所有者;类型

notepad++.exe;7736;戴夫;

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

名称;句柄;所有者;日期;时间;日期时间;类型

notepad++.exe;7736;Dave;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
    }
}

每个 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={''}}
    }
}

关于powershell - 如何在导出为 CSV 之前向每一行添加日期/时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29460853/

相关文章:

powershell - 是否有等待 "break"从 powershell 中的 Get-Content 等待循环?

powershell - 使用Powershell在两个不同的屏幕上打开两个Chrome网站

visual-studio-2010 - Powershell 运行时

.NET 字符串拆分()

powershell - ffmpeg在编码时保持完整标题

powershell - 在 Pester 中测试集合的相等性或等价性

powershell - Invoke-WebRequest 是否支持数组作为 POST 表单参数?

powershell - Powershell脚本到电子邮件的最新修改日期

json - 解析JSON时“Invalid array passed”

powershell - 表达式或语句中的意外标记 - powershell