get-process for current user
I have the follow VBS, I would like this in powershell. However, would it be at
all possible to have it read from a list of computers instead of hard coding?
Please assist. So what I am looking for is to pull the current domain and user
name, then search for the current user and once found from a server.txt file, it
would then kill that users winword.exe application.
 
Current VB Script

---------------------
 
option explicit
'
' Kill all
processes by the name of strProcessName running under the current user
'

dim strComputer,strProcessName, strOwner, strUserName
dim objWMI,
objNetwork, colProcesses, dummy, objProcess
 
strComputer = "."

strProcessName = "winword.exe"
 
set objWMI =
GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set
objNetwork = CreateObject("WScript.Network")
set colProcesses =
objWMI.ExecQuery("Select * from Win32_Process Where Name = '"&
strProcessName & "'")
 
strUserName = objNetwork.UserName
for
each objProcess in colProcesses
    dummy = objProcess.GetOwner(strOwner)

    if (strOwner = strUserName) then
    objProcess.Terminate()
   
end if
next
 
set objWMI = nothing
set objNetwork = nothing

set objProcess
October 3rd, 2013 6:18pm

Yes you can read from a file or an array.

Start here:http://technet.microsoft.com/library/ee176977.aspx

Free Windows Admin Tool Kit Click here and download it now
October 3rd, 2013 7:07pm

I have tried, but for some odd reason, it will not work for reading from the text file. Here is what I have;

dim strComputer,strProcessName, strOwner, strUserName
dim objWMI, objNetwork, colProcesses, dummy, objProcess
arrComputers = Array("radfa0211v10317", radfa0211v10318", radfa0211v10319", radfa0211v10320", radfa0211v10321", radfa0211v10322", radfa0211v10323", radfa0211v10324", radfa0211v10325", radfb9211V69561")
for i = 0 to UBound(arrComputers)-1

strProcessName = "winword.exe"

Set objWMIService = GetObject("winmgmts:" _
   & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 if i mod 2 = 0 Then
 
 strComputer = arrComputers(i)

set colProcesses = objWMI.ExecQuery("Select * from Win32_Process Where Name = '"& strProcessName & "'")
 
strUserName = objNetwork.UserName
for each objProcess in colProcesses
    dummy = objProcess.GetOwner(strOwner)
    if (strOwner = strUserName) then
    objProcess.Terminate()
    end if
next
 
set objWMI = nothing
set objNetwork = nothing
set objProcess = nothing

October 3rd, 2013 7:22pm

Here is a short example that demonstrates how to read text from a file in a VBScript script:

Free Windows Admin Tool Kit Click here and download it now
October 3rd, 2013 7:39pm

Bill,

Could you assist in creating this for me if at all possible?

Thanks!!!

October 3rd, 2013 7:41pm

Hi,

Sorry, but I just don't have the resources to write scripts on demand for free.

If you're not familiar with VBScript, I recommend the primer in the Windows 2000 Scripting Guide.

Rather than VBScript, I would recommend PowerShell. You can do more in less code with PowerShell and there are plenty of learning resources to help you get started (for example, the Learn link right at the top of this forum).

Bill

Free Windows Admin Tool Kit Click here and download it now
October 3rd, 2013 7:56pm

I want to try to get a powershell script to list the current process by the current user. Please assist.

Than

October 3rd, 2013 8:21pm

Then kill a process for the current user

Free Windows Admin Tool Kit Click here and download it now
October 3rd, 2013 8:22pm

Use Get-Process and Stop-Process.

http://ss64.com/ps/get-process.html

http://ss64.com/ps/stop-process.html

October 3rd, 2013 8:25pm

Hi Imossolle,

You can't get the process for the current user  in both  Powershell V2 and  V 3.

but YES

in PowerShell V 4.0  it is possible  becouse it has a new switch parameter, IncludeUserName.

otherwise , You have Get-process and Stop-Process to  use  in Powershell V2 and V 3.

http://technet.microsoft.com/en-us/library/hh857339.aspx  => new features in PowerShell V 4.0

Free Windows Admin Tool Kit Click here and download it now
October 3rd, 2013 8:36pm

So if I wanted to kill only my current winword process only for me, with v 3.0, it will not work?
October 3rd, 2013 8:40pm

yes it will work 

get-process winword|stop-process

Get-process will show all process running in your Computer  but you won't able to see the user name in PS V3 or V 2. but in PS V 4  you can.

Free Windows Admin Tool Kit Click here and download it now
October 3rd, 2013 8:41pm

Tarique I have a VB Script, for current user, but it is not working properly. do you see something wrong with it?

option explicit
'
' Kill all processes by the name of strProcessName running under the current user
'
dim arrComputers,strProcessName, strOwner, strUserName
dim objWMI, objNetwork, colProcesses, dummy, objProcess
 
strComputer = "."

strProcessName = "winword.exe"
 
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set objNetwork = CreateObject("WScript.Network")
set colProcesses = objWMI.ExecQuery("Select * from Win32_Process Where Name = '"& strProcessName & "'")
 
strUserName = objNetwork.UserName
for each objProcess in colProcesses
    dummy = objProcess.GetOwner(strOwner)
    if (strOwner = strUserName) then
    objProcess.Terminate()

    end if
next

October 3rd, 2013 9:06pm

Sorry , I don't know VB script ......... I am really sorry.....

Free Windows Admin Tool Kit Click here and download it now
October 3rd, 2013 9:08pm

So if I wanted to kill only my current winword process only for me, with v 3.0, it will
October 3rd, 2013 9:08pm

Ah yes - it is very frustrating because Admins do not know Windows Insides at all.  Even most programmers to day do not truly understand how Windows works.

The method is as old as NT.  Get the users sessio0n and get Explorer.  You can then list all process whose parent is the Explorere you have found.  NOt just filter on the process you want to kill.

Method #2:

Get all copies of WinWord.Exe then check the owner of the parent process.

Current user is easy on a WorkStation.  It is much more ambiguous on a Terminal Server.

On a workstation the console user is:
gwmi win32_computersystem | select username -computer somepc

Free Windows Admin Tool Kit Click here and download it now
October 4th, 2013 1:16am

option explicit
'
' Kill all processes by the name of strProcessName running under the current user
'
dim strComputer,strProcessName, strOwner, strUserName
dim objWMI, objNetwork, colProcesses, dummy, objProcess
 
strComputer = WScript.Arguments(0)
'strComputer = WScript.Arguments
WScript.Echo "Running Against Remote Computer Named: " & strComputer
Dim oShell
Dim UserName

Set oShell = Wscript.CreateObject("Wscript.Shell")
UserName = oShell.ExpandEnvironmentStrings("%USERNAME%")

WScript.Echo "Looking for Microsoft Word for the following loged in user: " &  UserName


strProcessName = "WINWORD.EXE"
 
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set objNetwork = CreateObject("WScript.Network")
set colProcesses = objWMI.ExecQuery("Select * from Win32_Process Where Name = '"& strProcessName & "'")
 
strUserName = objNetwork.UserName
for each objProcess in colProcesses
    dummy = objProcess.GetOwner(strOwner)
    if (strOwner = strUserName) then
    objProcess.Terminate()
    end if
next
 
set objWMI = nothing
set objNetwork = nothing
set objProcess = nothing

February 26th, 2014 5:57pm

The following will get the current users process within powershell without requiring elevation.  The '-includeusername' parameter of Get-Process requires elevation.

&tasklist/FI"USERNAME eq $Env:UserDomain\$env:Username"


Free Windows Admin Tool Kit Click here and download it now
February 8th, 2015 2:58pm

The following will get the current users process within powershell without requiring elevation.  The '-includeusername' parameter of Get-Process requires elevation.

&tasklist/FI"USERNAME eq $Env:UserDomain\$env:Username"


February 8th, 2015 10:57pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics