Search code examples
powershellmessageboxcountdowntimersystem-administration

Get a process's owners, send them messagebox and then kill the processes


I trying to get a list of users, who are running an application, for instance chrome.exe. Then send them a message and set the countdown timer for 300 seconds and after this I need to stop these processes. I have tried the following PowerShell.

$owners = @{} 
gwmi win32_process | % {
    $owners[$_.handle] = $_.getowner().user
}
get-process | select processname,Id,@{l="Owner";e={$owners[$_.id.tostring()]}}

Answer was closed. below you can see a solution:

$USERLIST = (Get-WmiObject -Class Win32_Process -Filter "Name='tsappldr.exe'" -ComputerName "SRUS270137C").getowner().user
ForEach ($computer in $USERLIST) {
$CmdMessage = {C:\windows\system32\msg.exe   $computer  /v 'Cancel your session'} $CmdMessage | Invoke-Expression 
}
Start-Sleep -m 5  #time-out for 5 minutes

get-process tsappldr, tpedrte  | stop-process -force

Solution

  • Thanks for everyone, who helped me, so i wrote current code:

    $USERLIST = (Get-WmiObject -Class Win32_Process -Filter "Name='tsappldr.exe'" -ComputerName "SRUS270137C").getowner().user
    ForEach ($computer in $USERLIST) {
    $CmdMessage = {C:\windows\system32\msg.exe   $computer  /v 'Cancel your session'} $CmdMessage | Invoke-Expression 
    }
    Start-Sleep -m 5  #time-out for 5 minutes
    
    get-process tsappldr, tpedrte  | stop-process -force
    

    And it works correct!