2016-01-21 90 views
0

我有一个应用程序应该过滤 Processname(这工作正常)和开始时间。通过Starttime PowerShell筛选过程

但我无法通过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过滤器?

回答

0

您在Where-Object声明中使用的是ProcessName而不是StartTime($_.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 -test像($_.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检查有太多可以跳过你不能像Idle - 方法阅读StartTime - 属性的过程。