列出当前用户

时间:2016-02-04 07:43:14

标签: powershell

作为管理员,我可以通过运行此

来获取用户进程

Get-Process -IncludeUserName | Where UserName -match test

但作为非管理员,我无法使用-IncludeUserName因为“IncludeUserName”参数需要提升用户权限“。

因此,如果我以用户测试身份登录,我如何仅列出他的进程而不是所有正在运行的进程?

4 个答案:

答案 0 :(得分:6)

你可以通过WMI做到这一点。以下是您可以找到here的文章的摘录。

user = NotepikUser.objects.get(pk=1)

# Combie the notes reposted and notes created of user
notes = Note.objects.filter(Q(notereposter__user=user) | Q(author=user))
notes = notes.order_by("-notereposter__date_added", "-date_created")

答案 1 :(得分:6)

Get-Process不会向您提供此信息,您需要WMI:

$owners = @{}
gwmi win32_process |% {$owners[$_.handle] = $_.getowner().user}
$ps = get-process | select processname,Id,@{l="Owner";e={$owners[$_.id.tostring()]}}
foreach($p in $ps) {
    if($p.Owner -eq $env:USERNAME) {
        $p
    }
}

答案 2 :(得分:2)

感谢您的代码。基于此,我创建了一个修改版本,允许用户杀死(自己的一部分)自己的进程:

#Script to allow users to kill (a subset of) their own processes 
#Based on : https://stackoverflow.com/questions/35195221/list-process-for-current-user
#Previously we used Task Nanny created by Michel Stevelmans which is a lot faster, but it did not show a process that was causing issues for our users.
$UserProcesses = @()
$Owners = @{}
Get-WmiObject win32_process | Foreach{$owners[$_.handle] = $_.getowner().user}
$Processes = Get-Process | select processname,Description,Id,@{l="Owner";e={$owners[$_.id.tostring()]}}
Foreach($Process in $Processes)
{
    IF($process.Owner -eq $env:USERNAME)
    {
        $UserProcesses += $Process
    }
}
$UserProcessesToExclude = @(
    'concentr', #Citrix Connection Center
    'conhost', #Console Window Host
    'dwm', #Desktop Windows Manager
    'explorer', #Explorer
    'Receiver', #Citrix Receiver Application
    'rundll32', #Windows host process (Rundll32)
    'ssonsvr', #Citrix  Receiver 
    'taskhost' #Host Process for Windows Tasks
    'wfcrun32' #Citrix Connection Manager
    'wfshell' #Citrix wfshell shell
)

$UserProcesses | Where{$_.ProcessName -notin $UserProcessesToExclude} | Out-GridView -Title 'Task killer - Select the process(es) you want to kill. Hold CTRL to select multiple processes.' -PassThru | Foreach{Stop-Process -id $_.Id}

答案 3 :(得分:1)

这更快,只需一行,不需要管理员。

Get-Process | ? {$_.SI -eq (Get-Process -PID $PID).SessionId}