【问题标题】:Powershell Kill all processes except systemPowershell 杀死除系统之外的所有进程
【发布时间】:2014-11-04 08:03:15
【问题描述】:

在powershell中,我想杀死所有用户的所有进程,除了资源管理器和系统使用的进程

这是我包含给出的错误的地方:

$Cred = Get-Credential;
Invoke-Command -ComputerName localhost -Credential $Cred -ScriptBlock { Get-Process $env:ALLUSERSPROFILE | Where-Object -FilterScript {$_.Name -ne "SYSTEM, NETWORK SERVICE, LOCAL SERVICE"} | Where-Object -filterscript {$_.Name -ne "explorer"} | Stop-Process -WhatIf }
Cannot find a process with the name "C:\ProgramData". Verify the process name and call the cmdlet again.
    + CategoryInfo          : ObjectNotFound: (C:\ProgramData:String) [Get-Process], ProcessCommandException
    + FullyQualifiedErrorId : NoProcessFoundForGivenName,Microsoft.PowerShell.Commands.GetProcessCommand
    + PSComputerName        : localhost

【问题讨论】:

    标签: powershell powershell-2.0 powershell-3.0 powershell-remoting powershell-ise


    【解决方案1】:

    这里,这应该适合你。

    Function Stop-UserProcesses{
    Param([string]$Computer = "localhost")
        $Cred = Get-Credential
        Invoke-Command -ComputerName $Computer -Credential $Cred -ScriptBlock { 
            Get-Process -IncludeUserName | Where{!($_.UserName -match "NT AUTHORITY\\(?:SYSTEM|(?:LOCAL|NETWORK) SERVICE)") -and !($_.ProcessName -eq "explorer")}|Stop-Process -WhatIf
        }
    }
    

    一旦您确信它可以正常工作,请删除 -WhatIf。然后将其称为 Stop-UserProcesses 以在本地结束所有内容,或将其称为 Stop-UserProcesses SomeComputer01 以在远程系统上结束所有内容(假设您在您的环境中启用了远程会话)。

    编辑: 那么,显然 -IncludeUserName 开关是 v4 中的新选项。所以,为了做你想做的事,我们必须跳过箍并在 win32_process 类上使用 Get-WMIObject,然后为每个进程执行 GetOwner() 方法。可能想要过滤它,这样我们就不会在没有所有者的情况下出现 Idle 抛出错误之类的问题,因此我们将确保 CommandLine 属性存在。

    Function Stop-UserProcesses{
    Param([string]$Computer = "localhost")
        $Cred = Get-Credential
        Invoke-Command -ComputerName $Computer -Credential $Cred -ScriptBlock { 
            #Get all processes
            $Processes = get-wmiobject win32_process|Where{![string]::IsNullOrEmpty($_.commandline)}|Select *,@{l='Owner';e={$_.getowner().user}}
            #Filter out System and service processes
            $Processes = $Processes | Where { !($_.Owner -match "(?:SYSTEM|(?:LOCAL|NETWORK) SERVICE)") }
            #Get processes and filter on the Process ID and name = explorer, then pipe to stop-process
            Get-Process | Where { $Processes.ProcessID -contains $_.id -and $_.name -ne "explorer" } | Stop-Process -WhatIf
        }
    }
    

    【讨论】:

    • 好的,我刚刚将它复制到我的 ISE,运行它,然后输入 Stop-UserProcesses,它首先提示我输入凭据(我输入了我的域凭据),它告诉我一个关于它会停止的 40 个进程。你是如何运行它的?
    • 感谢您的回复!现在我得到了无法找到 IncludeUserName 参数的错误。
    • 抱歉,我没有意识到 -IncludeUserName 是 PowerShell v4 的东西。更新了答案,并在我的笔记本电脑上使用 PS v3 进行了测试,速度较慢,但​​运行良好。
    • 老实说,我刚刚修改了Get-Help Get-Process -detailed 中的示例 8,并添加了一些过滤并将其通过管道传递给停止进程。然后把它变成一个方便使用的函数。很高兴它对你有用。
    • 哇,我浏览了帮助,但没有看到。不过谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多