通过Starttime PowerShell过滤进程

时间:2016-01-21 18:09:40

标签: .net c powershell

我有一个应用程序应该通过Processname过滤Process(这很好)和starttime。

但我无法通过startfine进行过滤。

我正在尝试使用下面的代码。任何人都可以帮助我吗?

#Process DropDown Choose 
    $Choice = $DropDown.SelectedItem.ToString()
    #Write-Host $Choice
    $MyChoice = $Choice
    #Write-Host $MyChoice
    $Script:Choice = $DropDown.SelectedItem.ToString()

#Date DropDown Choose
    $DtChoice = $DtDropDown.SelectedItem.ToString()

    #Write-Host $Choice
    $MyDtChoice = $DtChoice
    #Write-Host $MyChoice
    $Script:DtChoice = $DtDropDown.SelectedItem.ToString()

#Fill Datagrid

$gps = Get-Process |  Where-Object {($_.ProcessName -like "$choice" ) -and ($_.ProcessName -like "$Dtchoice" ) } |Select-Object id,name,cpu,starttime  |Sort-Object -Descending starttime  
$list = New-Object System.collections.ArrayList
$list.AddRange($gps)`

$DtDropDown的工作原理如下:

#region Seleção de Data
[array ]$DtConsulta   = @(0,0,0,0,0,0,0,0)

$DtDropDown = new-object System.Windows.Forms.ComboBox
$DtDropDown.Location = new-object System.Drawing.Size(60,80)
$DtDropDown.Size = new-object System.Drawing.Size(240,30)
$DtDropDown.DropDownStyle = "DropDownList" # This style of combobox will prevent blank
# item selection.

#Preenchimento da lista de seleção de datas permitidas para busca de historico
ForEach ($i in -7..0) 
{
    $d = Get-Date 
    $DtConsulta[$i] = $d.AddDays($i)
    $DtDropDown.Items.Add( $DtConsulta[$i].ToLongDateString()) | Out-Null
}

如何通过startfine进行过滤?

1 个答案:

答案 0 :(得分:0)

您在ProcessName声明中使用的是StartTime而非Where-Object($_.ProcessName -like "$Dtchoice" )

此外,您正在尝试将表示日期的字符串与DateTime - 对象进行比较。

PS > $t.StartTime
torsdag 14. januar 2016 17.30.57

PS > $dtchoice
torsdag 14. januar 2016

您需要将StartTime日期时间转换为类似的格式:

PS > $t.StartTime.ToLongDateString()
torsdag 14. januar 2016

PS > $dtchoice
torsdag 14. januar 2016

我建议where - 测试($_.StartTime -and ($_.StartTime.ToLongDateString() -eq "$dtchoice"))。实施例

$gps = Get-Process |
Where-Object {($_.ProcessName -like "$choice" ) -and ($_.StartTime -and ($_.StartTime.ToLongDateString() -eq "$dtchoice")) } |
Select-Object id,name,cpu,starttime |
Sort-Object -Descending starttime

我还在那里添加了$_.StartTime支票,以便跳过您无法读取StartTime - 属性的流程,例如Idle - 进程。