我有一个应用程序,它应该按进程名称(这工作正常)和启动时间过滤进程
。
但是我无法通过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进行过滤?
最佳答案
您在 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
测试,例如 ($_.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
>-过程。
关于.net - 按启动时间 PowerShell 过滤进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34931155/