我非常确定这很简单,但由于我是PowerShell的新手,我无法想办法。以下命令:
Get-Service | Sort-Object -Property status -Descending
在PC上显示服务,按状态对其进行排序,以便在列表的开头显示正在运行的进程。启动服务后立即停止服务我可以进行任何过滤,这样就不会显示已停止的服务吗?
在本帖子和所有其他帖子的在线论坛上发布代码时,请养成不使用缩写的习惯。
谢谢!
答案 0 :(得分:4)
Get-Service |
Where-Object { $Psitem.Status -ne 'Stopped' } |
Sort-Object -Property status -Descending
这有效地按状态过滤。
答案 1 :(得分:1)
取决于PowerShell版本($ PSVersionTable.PSVersion) 您可以运行两个命令:
PS版本2.0 get-service | where {$_.status -eq 'running'}
PS 3.0或更高版本get-service | where status -eq 'running'
PS 3.0或更高版本get-service | ? status -eq 'running'
两个版本之间的巨大差异是{Curly brackets}
,您还可以使用别名Get-Alias
以下功能将允许您只选择一个要停止的服务。在你选择的时间目前5分钟。该服务将重新开始。 GUI选项允许您在选择时启动和停止。
Function ManageService{
$Service=Get-Service | where {$_.status -eq 'running'} | Out-GridView -OutputMode Single
$GUI = new-object -comobject wscript.shell
if ($Service.Status -eq "Running"){
$ServiceName=$Service.DisplayName
$Yes_Cancle = $GUI.popup("Do you want to turn stop $ServiceName ?", `
0,"Manage Service",3)
If ($Yes_Cancle -eq 6) {
Stop-Service $ServiceName
$GUI.popup("$ServiceName service has been STOPPED and will START again in 5 minutes, soon as you hit OK")
start-Sleep -s 300
$GUI.popup("Time is up! $ServiceName service will now start as soon as you hit OK")
Start-Service $ServiceName
cls
}
else {
$GUI.popup("You decided to cancel. $ServiceName will not be stopped")
cls
}}}
cls
ManageService
Function ManageProcess{
$Process=Get-Process | Out-GridView -OutputMode Single
$GUI = new-object -comobject wscript.shell
if ($Process.processname -ne "$False"){
$ProcessName=$Process.processname
$Yes_Cancle = $GUI.popup("Do you want to turn kill $ProcessName ?", `
0,"Manage Process",3)
If ($Yes_Cancle -eq 6) {
Stop-Process -processname $ProcessName
$GUI.popup("$ProcessName has been killed as soon as you hit OK")
cls
}
else {
$GUI.popup("You decided to cancel. $ProcessName will not be killed")
cls
}}}
cls
ManageProcess